Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show logs only from one class using logback

Tags:

logback

I am trying to display logs into console as well as file however in file i want only certain logs that will be displayed from one particular class and i am not sure how to do this. Below is my logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
        </encoder>
    </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file> test.txt </file>
    <append>false</append>
    <encoder>
        <pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>

</configuration>
like image 951
jonny Avatar asked Sep 05 '17 20:09

jonny


People also ask

What is Appender in Logback?

Appenders place log messages in their final destinations. A Logger can have more than one Appender. We generally think of Appenders as being attached to text files, but Logback is much more potent than that. Layout prepares messages for outputting.

Why do we use Logback xml?

Logback executes an async appender in a separate thread to decouple the logging overhead from the thread executing your code. Using the async appender is incredibly easy. Refer the appender that should be asynchronously invoked within an <appender> element.

Which of the following file is used by Logback logging system?

Logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging systems such as log4j or java. util. logging (JUL) introduced in JDK 1.4. The third module called access integrates with Servlet containers to provide HTTP-access log functionality.

Where does Logback xml go in spring boot?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.


1 Answers

Just declare a logger for your "one particular class" and then associate that logger with your FILE appender.

For example:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>test.log</file>
    <encoder>
        <pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
    </encoder>
</appender>

<logger name="your.particular.Class">
    <appender-ref ref="FILE" />
</logger>

<root level="info">
    <appender-ref ref="STDOUT" />
</root>

This will ensure that any log events emitted by your.particular.Class are only directed to the FILE appender and all other logs will be directed only to the STDOUT appender.

like image 106
glytching Avatar answered Sep 18 '22 19:09

glytching