This example shows how to use SLF4J with Logback.
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="stdout"/> </root> </configuration>
Check out Logback log formatting specification here
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(); } }
2017-09-10 15:05:54:704 INFO com.logicbig.example.ExampleMain.main() c.l.e.MyClass:11 - a test message
Dependencies and Technologies Used: