Log4j log levels Examples
Every Java application needs logging. It could be that you simply want to log your system’s state or user actions to a file.
In this article, We will see different levels of logging in log4j with example
Log4j logger contains three main components namely logger, appender and layout.
Logger takes care of the logging mechanism and deals with level of logging.
Logging Levels
- DEBUG - designates fine-grained informational events that are most useful to debug an application (what is going on)
- INFO - announcements about the normal operation of the system - scheduled jobs running, services starting and stopping user-triggered processes, and actions.
- WARN - any condition that, while not an error in itself, may indicate that the system is running sub-optimally.
- ERROR - a condition that indicates something has gone wrong with the system. It could be that you need to record error messages whenever an exception happens and then send an e-mail or text message to a human for urgent intervention
- FATAL - Anything at this level means your Java process cannot continue and will now terminate.
- TRACE - This has been recently introduced in version 1.2 and adds more information to debug level logs.
- OFF - Simple enough. NO LOGGING !!
- ALL- This level is used to turn on all levels of logging. Once this is configured and the levels are not considered at all. All appenders will start pouring the log events in log files.
Logging Levels message examples
- logger.debug("the web service url is =[{}]", url);
- logger.trace("exception occured during service call =[{}]", e.printStackTrace());
- logger.warn("Invalid bank account number for record=[{}]", 53);
- logger.info("Application started successfully on port 8080");
- logger.error("Database connection is down", exception);
How to set log levels
Set log level in log4j.properties
log4j.rootLogger=DEBUG, consoleAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
#Log info messages for package 'com.javacodestuffs.controller'
log4j.logger.com.javacodestuffs.controller=INFO, consoleAppender
Set log level in log4j.xml
<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender class="org.apache.log4j.ConsoleAppender" name="console">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="com.javacodestuffs.controller">
<level value="INFO">
<appender-ref ref="console">
</appender-ref></level></logger>
<root>
<level value="DEBUG">
<appender-ref ref="console"></appender-ref></level></root></log4j:configuration>
How log levels print levels
The priority for log levels are.
- ALL - all get printed or logged in file TRACE, DEBUG, INFO, WARN, ERROR, FATAL
- TRACE - only level TRACE, DEBUG, INFO, WARN, ERROR, FATAL get printed or logged in a log file.
- DEBUG - only level DEBUG, INFO, WARN, ERROR, FATAL get printed or logged in a log file.
- INFO - only level INFO, WARN, ERROR, FATAL get printed or logged in a log file.
- WARN - the only level WARN, ERROR, FATAL get printed or logged in a log file.
- ERROR - only level ERROR, FATAL get printed or logged in a log file.
- FATAL - FATAL get printed or logged in a file.
- OFF - None get printed or logged in a log file.
Log4j log level example
We need to add maven dependency in pom.xml as
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
Log4jExample.java
import org.apache.log4j. * ;
public class Log4jExample {
private static Logger logger = Logger.getLogger(Log4jExample.class);
public static void main(String[] args) {
logger.setLevel(Level.DEBUG);
logger.trace("This is a Trace Message!!!");
logger.debug("This is a Debug Message!!!");
logger.info("This is a Info Message!!!");
logger.warn("This is a Warn Message!!!");
logger.error("This is a Error Message!!!");
logger.fatal("This is a Fatal Message!!!");
}
}
output:
$javac Log4jExample.java
$java -Xmx128M -Xms16M Log4jExample
This is a Debug Message!!!
This is a Info Message!!!
This is a Warn Message!!!
This is a Error Message!!!
This is a Fatal Message!!!
In this article, we have seen the Log4j log levels examples using different ways. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment