#
Logback : Example in Java
This tutorial presents to you a Logback example in Java. This tutorial is using Eclipse and Maven. The application is a simple Java application.
I suppose we have created a Java application using Maven and Logback logging framework is configured.
Now all we have to do is to code and to test our example of using Logback framework. For this I will create 1 java classe:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunTest {
private static final Logger logger = LoggerFactory.getLogger(RunTest.class);
public static void main(String [] args) {
System.out.println("Start logging with Logback in Java.");
logger.trace("This is a TRACE example in Backlog framework");
logger.debug("This is a DEBUG example in Backlog framework");
logger.info("This is an INFO example in Backlog framework");
logger.warn("This is a WARNING example in Backlog framework");
logger.error("This is an ERROR example in Backlog framework");
System.out.println("End of application running.");
}
}
Here is the result of the Logback example into the "stdout":
Start logging with Logback in Java.
2017-04-26 21:20:25 | DEBUG | [main] RunTest:12 - This is a DEBUG example in Backlog framework
2017-04-26 21:20:25 | INFO | [main] RunTest:13 - This is an INFO example in Backlog framework
2017-04-26 21:20:25 | WARN | [main] RunTest:14 - This is a WARNING example in Backlog framework
2017-04-26 21:20:25 | ERROR | [main] RunTest:15 - This is an ERROR example in Backlog framework
End of application running.
And here is the result of the Logback example into the "C:\JAVA\logFile.log":
2017-04-26 21:20:25 | DEBUG | [main] RunTest:12 - This is a DEBUG example in Backlog framework
2017-04-26 21:20:25 | INFO | [main] RunTest:13 - This is an INFO example in Backlog framework
2017-04-26 21:20:25 | WARN | [main] RunTest:14 - This is a WARNING example in Backlog framework
2017-04-26 21:20:25 | ERROR | [main] RunTest:15 - This is an ERROR example in Backlog framework
In logback.xml file we have:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_DIR" value="C:/JAVA" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_DIR}/logFile.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<logger name="myLogger" level="TRACE"/>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Here are some things to retain:
When you log the information (with the standard Layout) you will see the object class which write the log ("Car", "RunTest").
When you log the information (with the standard Layout) you will see the line within the class where the log is generated (Car:21 -> the line is 21).
System.out.println
is writing only to the console and not to the log file.