In log4j tutorial, we saw how to use log4j xml based configuration. But log4j.xml is verbose, so log4j framework provide option to read configuration from properties file too. Since properties file don’t have any defined schema to validate, we have to be more careful with it. Today we will see how XML configurations can be converted to properties based configuration.
Root logger is used when there is no match with a logger. It’s defined like below in XML.
<root>
<priority value="DEBUG" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
It can be defined in properties file as below.
log4j.rootLogger=DEBUG, file, console
The first value in comma separated list is the root logging level value. All other values are appenders.
We can have multiple appenders in log4j. Below are two appenders, one for console logging and another to file.
<!-- console appender -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
</layout>
</appender>
<!-- rolling file appender -->
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/main.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
In log4j.properties file, they will be defined as below.
#Define console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
#Define rolling file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/main.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
So the format for log4j properties file appender is log4j.appender.{appender_name}.{appender_config}
. Notice that the appenders configurations such as MaxFileSize
, MaxBackupIndex
are same as in XML configuration file.
Just like appenders, we can have multiple loggers. For example of xml based configuration;
<logger name="com.journaldev.log4j" additivity="false">
<level value="WARN" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</logger>
<logger name="com.journaldev.log4j.logic" additivity="false">
<level value="DEBUG" />
<appender-ref ref="file" />
</logger>
They will be defined in properties file as log4j.logger.{logger_name}
. The values contain logging level and list of appenders to use.
#Define loggers
log4j.logger.com.journaldev.log4j=WARN, file, console
log4j.logger.com.journaldev.log4j.logic=DEBUG, file, console
Additivity usage is shown in above logger xml configuration, it’s the attribute of logger element. Below is the way to use it in log4j properties file configuration as log4j.additivity.{logger_name}
.
#setting additivity
log4j.additivity.com.journaldev.log4j=false
log4j.additivity.com.journaldev.log4j.logic=false
Based on above, below is my final log4j.properties file.
#Define root logger options
log4j.rootLogger=DEBUG, file, console
#Define console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
#Define rolling file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/main.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
#Define loggers
log4j.logger.com.journaldev.log4j=WARN, file, console
log4j.logger.com.journaldev.log4j.logic=DEBUG, file, console
#setting additivity
log4j.additivity.com.journaldev.log4j=false
log4j.additivity.com.journaldev.log4j.logic=false
PropertyConfigurator
is used to configure log4j settings. It’s optional if the file name is log4j.properties and it’s in the project classpath. We have to configure it before using the logger. Here is a simple program showing how to configure and use log4j logging.
package com.journaldev.log4j.main;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.journaldev.log4j.logic.MathUtils;
public class Log4jExample {
static{
init();
}
private final static Logger logger = Logger.getLogger(Log4jExample.class);
public static void main(String[] args) {
logger.debug("My Debug Log");
logger.info("My Info Log");
logger.warn("My Warn Log");
logger.error("My error log");
logger.fatal("My fatal log");
}
/**
* method to init log4j configurations
*/
private static void init() {
PropertyConfigurator.configure("log4j.properties");
}
}
When it’s executed, you will get below in console log.
WARN Log4jExample - My Warn Log
ERROR Log4jExample - My error log
FATAL Log4jExample - My fatal log
At the same time, you will see logs getting generated in main.log file as below.
2016-05-14 00:34:11,994 +0530 [main] WARN (Log4jExample.java:20) - My Warn Log
2016-05-14 00:34:11,995 +0530 [main] ERROR (Log4jExample.java:21) - My error log
2016-05-14 00:34:11,995 +0530 [main] FATAL (Log4jExample.java:22) - My fatal log
Notice that debug and info logger are not getting printed because the minimum logging level is set as WARN. That’s all for log4j properties file example usage. Happy Logging!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
My log files look like 2021-11-18 00:34:11,994 +0530 [main] WARN (?:?) instead of 2021-11-18 00:34:11,994 +0530 [main] WARN (Log4jExample.java:20), the class file name is getting replaced by ? in Linux, works fine on Windows.
- Deon
i have a doubt. can u help me sir
- rajashree g.s.
Hi Pankaj I am trying to configure log4j2 using properties file configuration. How can I load the properties file in log4j2 similar to PropertyConfigurator.configure in log4j.
- Neethu
Hello Pankaj I want to have my own defined format for logs and then the logs should be pushed to kafka. can you help me please? The log format should be look LIKE THIS. | | Hostname | | | | | 14-OCT-2019 18:01:00 | Payment Module | 172.19.1.201 | Payment.java | processpayment | 130 | 199917238390090 | this is a sample log
- Shailender kumar
Hi pankaj, we have a requirement to generate log files based on the country the user is accessing the application(i.e. error_IND.log, error_SL.log). Is it possible to achieve the same using Log4j. Thanks
- deva
Sir! Thanku so much for this… But I cannot find “MathUtils” Please Do Help!
- Shefali Goyal
Hello Pankaj, let us say we have two Classes like Class_1 and Class_2 my qustion how can i create für each class a loggerFile and send the LogInformation from Class_1 to LoggerFile_1 and LogInformation from Class_2 to LoggerFile_2 ? if i use log4j.properties File thanks, Raed
- Raed