1. What is log4j2 ?
- Improved performance
- More flexible configuration
- Support for multiple logging levels
- Support for multiple logging appenders
- Java
- Scala
- .Net
- Kotlin
2. Java Code to use Log4j2
package org.bigtechtalk.log4jdemo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Loj4j2PropertyFileDemo
{
static Logger logger = LogManager.getLogger(Loj4j2PropertyFileDemo.class);
public static void main( String[] args )
{
logger.debug("Debug Message Logged !!!");
logger.info("Info Message Logged !!!");
logger.error("Error Message Logged !!!", new NullPointerException("NullError"));
}
}
3. Maven Dependency
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
4. Log4J 2 Properties File
By default, Log4J 2 looks for a properties file with the name log4j2.properties in the classpath.
4.1 Config file to write log to console
First we will start with writing the log messages to the console. So we will define our log4j2.properties file like below.
name= BigTechTalk Java Logging
appends = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n
# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.stdout.ref = STDOUT
In the log4j2.properties files we have defined an appender to write the message to the console.
I have set the rootLogger level as debug and stdout.ref is the name of appender.console.name
4.2 Config file to write log to a file
name= BigTechTalk Java Logging
appends = file
# Log files location
property.basePath = c:/temp/logs
# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}/loggerFile.log
appender.rolling.filePattern=${basePath}/loggerFile_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n
appender.rolling.policies.type = Policies
# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = fileLogger
In the above config file we have used ${basePath} which acts as a substitution of property.basePath and appender.rolling.filename to specify the log file name.
Since we are using appender.rolling then we have to define the appender.rolling.layout.pattern and appender.rolling.policies.type.
4.3 Config file to write log to a file and Console
The below log4j2.properties file can be used to write the log message to file and console simultaneously.
name= BigTechTalk Java Logging
appends = console, file
# Log files location
property.basePath = c:/temp/logs
#Console Output
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n
# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}/app.log
appender.rolling.filePattern= ${basePath}/app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n
appender.rolling.policies.type = Policies
# RollingFileAppender rotation policy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 10MB
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
# Configure root logger
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.rolling.ref = fileLogger
We utilized two appenders in the above config file, i.e appender.console and appender.rolling .In contrast to appender.rolling, which is used to write logs to files, appender.console is used to write logs to the console.
We’ve even built up policies for appender.rolling that make it advantageous to use appender.rolling.filePattern to compress the old log file.
Additionally, the pattern can assist in removing files based on size (appender.rolling.policies.size.size) and logs based on time (appender.rolling.policies.time.type). appender.rolling.strategy.delete.ifLastModified.age can be used to delete logs which are older than 30d (days)