
- log4j - Home
- log4j - Overview
- log4j - Installation
- log4j - Architecture
- log4j - Configuration
- log4j - Sample Program
- log4j - Logging Methods
- log4j - Logging Levels
- log4j - ConsoleAppender
- log4j - Logging in Database
Log4j - Formatting Layouts
- log4j - Log Formatting
- log4j - CSV Parameter Layout
- log4j - CSV Log Event Layout
- log4j - HTML Layout
- log4j - Pattern Layout
- log4j - Json Template Layout
Log4j - File Appenders
- log4j - Logging in Files
- log4j - FileAppender
- log4j - Separate Folder per Month
- log4j - Daily log File Generation
log4j Resources
log4j - Logging Levels
The org.apache.logging.log4j.Level levels. You can also define your custom levels by sub-classing the Level class.
Level | Description |
---|---|
ALL | All levels including custom levels. |
DEBUG | Designates fine-grained informational events that are most useful to debug an application. |
INFO | Designates informational messages that highlight the progress of the application at coarse-grained level. |
WARN | Designates potentially harmful situations. |
ERROR | Designates error events that might still allow the application to continue running. |
FATAL | Designates very severe error events that will presumably lead the application to abort. |
OFF | The highest possible rank and is intended to turn off logging. |
TRACE | Designates finer-grained informational events than the DEBUG. |
How do Levels Works?
A log request of level p in a logger with level q is enabled if p >= q. This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.
Example - Filtering Messages using LOG Level
The Following example shows how we can filter all our DEBUG and INFO messages. This program uses of logger method setLevel(Level.X) to set a desired logging level:
This example would print all the messages except Debug and Info:
Log4jDemo.java
package com.tutorialspoint; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4jDemo{ /* Get actual class name to be printed on */ private static final Logger LOGGER = LogManager.getLogger(); public static void main(String[] args) { LOGGER.atLevel(Level.WARN); LOGGER.trace("Trace Message!"); LOGGER.debug("Debug Message!"); LOGGER.info("Info Message!"); LOGGER.warn("Warn Message!"); LOGGER.error("Error Message!"); LOGGER.fatal("Fatal Message!"); } }
Output
When you compile and run the LogClass program, it would generate the following result −
2025-09-04T13:41:43.223410900Z main ERROR Error Message! 2025-09-04T13:41:43.227410200Z main FATAL Fatal Message!
Example - Setting Levels using Configuration File
log4j provides you configuration file based level setting which sets you free from changing the source code when you want to change the debugging level.
Following is an example configuration file which would perform the same task as we did using the log.setLevel(Level.WARN) method in the above example.
Log4j2.properties
appender.0.type = Console appender.0.name = CONSOLE appender.0.layout.type = PatternLayout appender.0.layout.pattern = %p - %m%n rootLogger.level = WARN rootLogger.appenderRef.0.ref = CONSOLE
Let us now use our following program −
Log4jDemo.java
package com.tutorialspoint; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4jDemo{ /* Get actual class name to be printed on */ private static final Logger LOGGER = LogManager.getLogger(); public static void main(String[] args) { LOGGER.trace("Trace Message!"); LOGGER.debug("Debug Message!"); LOGGER.info("Info Message!"); LOGGER.warn("Warn Message!"); LOGGER.error("Error Message!"); LOGGER.fatal("Fatal Message!"); } }
Output
Now compile and run the above program and you would get following result in console −
WARN - Warn Message! ERROR - Error Message! FATAL - Fatal Message!