Monday 28 February 2022

logback: get the log files location programmatically

Below snippet prints the log files location programmatically.

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

// Print all the log files location
for (Logger logger : context.getLoggerList()) {
  Iterator<Appender<ILoggingEvent>> appendersIterator = ((ch.qos.logback.classic.Logger) logger)
      .iteratorForAppenders();

  while (appendersIterator.hasNext()) {
    Appender<ILoggingEvent> appender = appendersIterator.next();

    if (appender instanceof FileAppender) {
      FileAppender<?> fileAppender = (FileAppender<?>) appender;

      System.out.println(fileAppender.getFile());
    }
  }

}

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-print-log-files-location’.

 

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>logback-print-log-files-location</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 logback.xml file under src/main/resources folder.

 

logback.xml

<configuration scan="true" scanPeriod="15 seconds">


  <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>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>/Users/Shared/logback/myApp.log</file>

    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
      </pattern>
    </encoder>
  </appender>


  <logger name="com.sample.app" level="WARN">
    <appender-ref ref="FILE" />
  </logger>


  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

 

Step 4: Define HelloWorld class.

 

HelloWorld.java

package com.sample.app;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;

public class HelloWorld {

  public static void main(String[] args) {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

    // Print all the log files location
    for (Logger logger : context.getLoggerList()) {
      Iterator<Appender<ILoggingEvent>> appendersIterator = ((ch.qos.logback.classic.Logger) logger)
          .iteratorForAppenders();

      while (appendersIterator.hasNext()) {
        Appender<ILoggingEvent> appender = appendersIterator.next();

        if (appender instanceof FileAppender) {
          FileAppender<?> fileAppender = (FileAppender<?>) appender;

          System.out.println(fileAppender.getFile());
        }
      }

    }

  }
}

 

Total project structure looks like below.




Run HelloWorld.java application, you will see below message in the console.

 

/Users/Shared/logback/myApp.log

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-print-log-files-location

 

 


  

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment