Below snippet is used to read the logback.xml file from custom location.
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator joranConfigurator = new JoranConfigurator();
try (InputStream inputStream = new FileInputStream("/Users/Shared/logback/logback.xml")) {
joranConfigurator.setContext(loggerContext);
joranConfigurator.doConfigure(inputStream);
}
Find the below working application.
Step 1: Create new maven project ‘customize-logback-loc-via-joran’.
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>customize-logback-loc-via-joran</artifactId>
<version>1</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</project>
Step 3: Create logbac.xml file under /Users/Shared/logback folder.
logback.xml
<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="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Step 4: Define HelloWorld class.
HelloWorld.java
package com.sample.app;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
public class HelloWorld {
private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);
public static void main(String[] args) throws JoranException, IOException {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator joranConfigurator = new JoranConfigurator();
try (InputStream inputStream = new FileInputStream("/Users/Shared/logback/logback.xml")) {
joranConfigurator.setContext(loggerContext);
joranConfigurator.doConfigure(inputStream);
}
logger.trace("Trace message");
logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warning message");
logger.error("Error message");
}
}
Total project structure looks like below.
Run HelloWorld.java class, you will see below messages in the console.
11:07:30.776 [main] WARN com.sample.app.HelloWorld - Warning message 11:07:30.779 [main] ERROR com.sample.app.HelloWorld - Error message
You can download complete working application from below link.
https://github.com/harikrishna553/java-libs/tree/master/logback/customize-logback-loc-via-joran
No comments:
Post a Comment