Below snippet can be used to update the logging level of root logger programmatically.
ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory .getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); rootLogger.setLevel(Level.WARN);
Find the below working application.
Step 1: Create new maven project ‘update-root-logger-logging-level-programatically’.
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>update-root-logger-logging-level-programatically</artifactId>
<version>1</version>
<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 logback.xml file under src/main/resources 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="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Step 4: Define HelloWorld application class.
HelloWorld.java
package com.sample.app;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
public class HelloWorld {
private static void sleep(int noOfSeconds) {
try {
TimeUnit.SECONDS.sleep(noOfSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void printLogMessages() {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.trace("Trace message");
logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warning message");
logger.error("Error message");
}
public static void main(String[] args) {
printLogMessages();
System.out.println("\nSleeping for 2 seconds\n");
sleep(2);
ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory
.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.WARN);
System.out.println("\nUpdated root logger logging level to WARN\n");
printLogMessages();
}
}
Total project structure looks like below.
Run HelloWorld.java class, you will see below messages in the console.
10:10:26.422 [main] DEBUG com.sample.app.HelloWorld - Debug message 10:10:26.423 [main] INFO com.sample.app.HelloWorld - Info message 10:10:26.423 [main] WARN com.sample.app.HelloWorld - Warning message 10:10:26.423 [main] ERROR com.sample.app.HelloWorld - Error message Sleeping for 2 seconds Updated root logger logging level to WARN 10:10:28.424 [main] WARN com.sample.app.HelloWorld - Warning message 10:10:28.424 [main] ERROR com.sample.app.HelloWorld - Error message
You can download complete working application from below link.
No comments:
Post a Comment