Step 1: Create new maven project ‘slf4j-logback-demo’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>slf4j-logback-demo</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
Step 3: Create logback.xml file under src/main/resources folder.
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Step 4: Define hello world application class.
HelloWorld.java
package com.sample.app;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String args[]) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Welcome to SLF4J logging");
}
}
Total project structure looks like below.
Run HelloWorld.java file, you will see below messages in console.
11:57:50.715 [main] INFO com.sample.app.HelloWorld - Welcome to SLF4J logging
You can download complete working application from below link.
https://github.com/harikrishna553/java-libs/tree/master/slf4j/slf4j-logback-demo
No comments:
Post a Comment