In this tutorial post, we are going to providing log4j levels example java program to understand log4j levels with java package wise will be providing different levels of logs with different types of outputs like console and file.
Log4j Levels Example
Table of Contents
For this we are going creating following files.
- Log4jExamplePKG1.java under package com.narayanatutorial.log4j.Log4jPKG1
- Log4jExamplePKG2.java under pcakge com.narayanatutorial.log4j.Log4jPKG2
- Log4j.properties under default package i.e inside src folder in Eclipse and inside default package in Netbeans
Log4jExamplePKG1.java
package com.narayanatutorial.log4j.Log4jPKG1; import org.apache.log4j.Logger; public class Log4jExamplePKG1 { private static final Logger logger = Logger.getLogger(Log4jExamplePKG1.class); public void displaLogyMessage() { logger.fatal("fatal message log from Log4jExamplePKG1 class!"); logger.error("error message log from Log4jExamplePKG1 class!"); logger.warn("warn message log from Log4jExamplePKG1 class!"); logger.debug("debug message log from Log4jExamplePKG1 class!"); logger.trace("trace message log from Log4jExamplePKG1 class!"); logger.info("info message log from Log4jExamplePKG1 class!"); } }
Log4jExamplePKG2.java
package com.narayanatutorial.log4j.Log4jPKG2; import org.apache.log4j.Logger; public class Log4jExamplePKG2 { private static final Logger logger = Logger.getLogger(Log4jExamplePKG2.class); public void displaLogyMessage() { logger.info("info message log from Log4jExamplePKG1 class!"); logger.warn("warn message log from Log4jExamplePKG1 class!"); } }
Log4jLevelsExample.java
package com.narayanatutorial.log4j; import org.apache.log4j.Level; import org.apache.log4j.Logger; import com.narayanatutorial.log4j.Log4jPKG1.Log4jExamplePKG1; import com.narayanatutorial.log4j.Log4jPKG2.Log4jExamplePKG2; public class Log4jLevelsExample { private static final Logger logger = Logger.getLogger(Log4jLevelsExample.class); public static void main(String[] args) { Log4jExamplePKG1 logpkg1 = new Log4jExamplePKG1(); Log4jExamplePKG2 logpkg2 = new Log4jExamplePKG2(); logger.debug("DEBUG message from Log4jLevelsExample!"); logpkg1.displaLogyMessage(); logpkg2.displaLogyMessage(); } } /** * TRACE --display INFO,DEBUG,WARN,ERROR,TRACE and FATAL * DEBUG -- display INFO,DEBUG,WARN,ERROR and FATAL * INFO -- display INFO,WARN,ERROR and FATAL * WARN --display WARN,ERROR and FATAL * ERROR --display ERROR and FATAL * FATAL --display only FATAL */
Log4j.properties
log = C:/logs/ log4j.rootLogger = INFO,stdout, FILE, # Define the file appender log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n # Redirect log messages to console 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 # Log4jPKG1 package log4j.logger.com.narayanatutorial.log4j.Log4jPKG1.Log4jExamplePKG1=FATAL, # Log4jPKG2 package log4j.logger.com.narayanatutorial.log4j.Log4jPKG2.Log4jExamplePKG2=WARN, stdout
Output
2016-02-07 18:26:41 FATAL Log4jExamplePKG1:10 - fatal message log from Log4jExamplePKG1 class! 2016-02-07 18:26:41 WARN Log4jExamplePKG2:11 - warn message log from Log4jExamplePKG1 class! 2016-02-07 18:26:41 WARN Log4jExamplePKG2:11 - warn message log from Log4jExamplePKG1 class!
Let’s quickly examine what we get
Above program producing two types of output such as 1. Console and 2. File because we have configuration as follows. This configuration is applicable to entire project by default. We can specify package wise types of output. Then we will get duplicate output i.e one from rootLogger and package wise logger.
log4j.rootLogger = INFO,stdout, FILE,
stdout providing log in the console.
FILE providing log in the file.
INFO level log will be producing for the entire project and we can provide log4j level in the package wise as follows.
Please refer Log4j Level hierarchical
If we do not want console log then we need to remove stdout from the above line.
If we do not want file log then we need to remove FILE from the above line.
# Log4jPKG1 package log4j.logger.com.narayanatutorial.log4j.Log4jPKG1.Log4jExamplePKG1=FATAL,
Logj4ExamplePKG1 package will be providing only FATAL level log.
FATAL level log hierarchy
FATAL
Log4jExamplePKG2 package will be providing WARN,ERROR and FATAL level log.
# Log4jPKG2 package log4j.logger.com.narayanatutorial.log4j.Log4jPKG2.Log4jExamplePKG2=WARN, stdout
WARN Level hierarchy is
WARN > ERROR > FATAL
If we want stop logging entire project, just add the following log4j level OFF to log4j properties file below the log4j.rootLogger=INFO,stdout,FILE
log4j.rootLogger=OFF
If we want start logging entire project, just add the following log4j level ALL to log4j properties file below the log4j.rootLogger=OFF OR comment the above line like #log4j.rootLogger=OFF
log4j.rootLogger=ALL
Output File
References
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.