A quick code sample demonstrating use of Log4J2, SLF4J and configuring Log4J using a properties file.
The following dependencies are required for using Log4J2 and SLF4J.
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.17.1</version> </dependency> </dependencies>
Log4J configuration is done using a file named log4j2.properties stored in the src/main/resources folder.
# Console logging #appender.console.type=Console #appender.console.name=LogToConsole #appender.console.layout.type=PatternLayout #appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n # Rotate log file appender.rolling.type=RollingFile appender.rolling.name=LogToRollingFile appender.rolling.fileName=logs/app.log appender.rolling.filePattern=logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log appender.rolling.layout.type=PatternLayout appender.rolling.layout.pattern=%d %p %C{1.} [%t] %m%n appender.rolling.policies.type=Policies appender.rolling.policies.time.type=TimeBasedTriggeringPolicy appender.rolling.policies.size.type=SizeBasedTriggeringPolicy appender.rolling.policies.size.size=2KB appender.rolling.strategy.type=DefaultRolloverStrategy appender.rolling.strategy.max=10 # Setup logging for our namespace logger.app.name=com.sbytestream logger.app.level=trace logger.app.additivity=false logger.app.appenderRef.rolling.ref=LogToRollingFile # logger.app.appenderRef.console.ref=LogToConsole # Setup logging for everything else rootLogger.level=trace rootLogger.appenderRef.rolling.ref=LogToRollingFile #rootLogger.appenderRef.stdout.ref=LogToConsole
The application code is in a single file.
package com.sbytestream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { public static void main(String[] args) { int repeats = 1; if (args.length > 0) { repeats = Integer.parseInt(args[0]); } for(int n=0; n < repeats; n++) { logger.trace("this is a trace"); logger.error("this is an error"); logger.warn("this is a warning"); logger.info("this is an info"); logger.debug("this is a debug"); } System.out.println("Logs have been written."); } private static Logger logger = LoggerFactory.getLogger(App.class); }
The sample code is available for download at GitHub. Running the program creates a directory named 'logs' where the log files are written. The sample code is making use of a rolling-file-appender configured to rollover if the size of the logfile reaches 2KB.
The program writes one log line using each of the log-levels. The program accepts the number of times it should write to the logs. The output of the program is shown below:
D:\Work\Github\log4j2-demo\target>java -jar logdemo-1-jar-with-dependencies.jar 10 Logs have been written. D:\Work\Github\log4j2-demo\target>dir logs Volume in drive D is Jerry Volume Serial Number is 3C7F-A2C0 Directory of D:\Work\Github\log4j2-demo\target\logs 25-02-2022 00:15 <DIR> . 25-02-2022 00:15 <DIR> .. 25-02-2022 00:15 <DIR> 2022-02 25-02-2022 00:15 1,056 app.log 1 File(s) 1,056 bytes 3 Dir(s) 218,203,787,264 bytes free D:\Work\Github\log4j2-demo\target>dir logs\2022-02 Volume in drive D is Jerry Volume Serial Number is 3C7F-A2C0 Directory of D:\Work\Github\log4j2-demo\target\logs\2022-02 25-02-2022 00:15 <DIR> . 25-02-2022 00:15 <DIR> .. 25-02-2022 00:15 2,054 app-02-25-2022-1.log 1 File(s) 2,054 bytes 2 Dir(s) 218,203,787,264 bytes free D:\Work\Github\log4j2-demo\target>