Create New Project:
Click on finish and project will get created.
No create new file log4j.poperties in src folder of your project.
Click on finish and file will get created.
Place below mentioned code in that file: Here logging is set for both console and file.
# Root logger option log4j.rootLogger=INFO, file, stdout # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=D:\logs\logging.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=0 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Create One new folder in Your project with name lib and place log4j-1.2.16.jar in that folder:
Add jar in classpath:
Now create new java class in your project:
Java Code:
import org.apache.log4j.Logger; public class LoggingExample { private static Logger log = Logger.getLogger(LoggingExample.class); public static void main(String[] args) { log.trace("Trace Log Message"); log.debug("Debug Log Message"); log.info("Info Log Message"); log.warn("Warn Log Message"); log.error("Error Log Message"); log.fatal("Fatal Log Message"); } }
Now run your application and see output in console and file: