This example shows how to use SLF4J with log4j2.
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.9.0</version> </dependency>
appenders=xyz appender.xyz.type = Console appender.xyz.name = myOutput appender.xyz.layout.type = PatternLayout appender.xyz.layout.pattern = [%d{yy-MMM-dd HH:mm:ss:SSS}] [%p] [%c{1}:%L] - %m%n rootLogger.level = info rootLogger.appenderRefs = abc rootLogger.appenderRef.abc.ref = myOutput
package com.logicbig.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static Logger LOGGER = LoggerFactory.getLogger(MyClass.class); public static void doSomething() { LOGGER.info("a test message"); } }
package com.logicbig.example; public class ExampleMain { public static void main(String[] args) { MyClass.doSomething(); } }
[17-Sep-21 10:48:30:393] [INFO] [MyClass:11] - a test message
Dependencies and Technologies Used: