Easy learning with example program codes

Logging levels in log4j

Logging levels:

All logging levels are defined in the org.apache.log4j.Level class. We can also create our own levels.

Logging levels are given below:

1. ALL: All levels including custom levels also.

2. DEBUG: It helps developers in application debugging.

3. INFO: It gives the information about the progress of application and its states.

4. WARN: It gives warning for unexpected events.

5. ERROR: It provides the information about error events.

6. FATAL: It provides the information about application abort.

7. OFF: It turns off all the logging. It is opposite to the ALL level.

Standard order of all logging levels:


i.e. If logging level set to FATAL then only FATAL messages will be displayed, if logging level set to ERROR then ERROR and FATAL messages will be displayed and so on.

In the below example as we set the logging level to WARN so only WARN, ERROR and FATAL messages will be displayed. The DEBUG and INFO message will not display.


import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 * This class is used to show the use of 
 * logging levels in Log4j with the BasicConfigurator.
 * @author codesjava
public class Log4jTest {
	//Get the Logger object.
	private static Logger log = Logger.getLogger(Log4jTest.class);
	public static void main(String[] args) {
		//Configuring Log4j, It will log all messages on console.
		//BasicConfigurator use ConsoleAppender and PatternLayout 
		//for all loggers.
		//Set logging level
		//logger messages
		log.debug("Log4j debug message test.");"Log4j info message test.");
		log.warn("Log4j warn message test.");
		log.error("Log4j error message test.");
		log.fatal("Log4j fatal message test.");


0 [main] WARN 
 - Log4j warn message test.
1 [main] ERROR 
 - Log4j error message test.
1 [main] FATAL 
 - Log4j fatal message test.

Download this example.
Next Topic: Log4j file appender.
Previous Topic: Log4j example using log4j xml file.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Sign Up/ Sign In
Ask a Question

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki