Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Logging is a critical component of any application, offering insights into its behavior and health. However, excessive logging can clutter output and obscure useful information, especially when verbose logs come from specific classes.

In this tutorial, we’ll explore how to disable logging from a specific class in Logback.

2. Why Disable Logging?

Disabling logging for specific classes can be beneficial in various scenarios:

  • Reducing Log Volume: reducing the volume of logs can help us focus on relevant information and reduce storage/processing costs.
  • Security: Some classes may log sensitive information inadvertently; silencing them can mitigate this risk.
  • Performance: Excessive logging can impact performance; disabling verbose loggers can help maintain optimal application performance.

3. Understanding Logback Configuration

Firstly, the Logback configuration is managed through an XML file, typically named logback.xml. This file defines loggers, appenders, and their formatting, allowing developers to control what gets logged and where.

A typical configuration includes one or more appenders and a root logger. Appenders define output destinations like the console or a file.

Here’s a simple example:

<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n</pattern>
        </encoder>
    </appender>

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

This configuration directs INFO level (and higher) logs to the console, formatted with a date, thread name, log level, and log message.

4. Disabling Logging From a Specific Class

To disable logging from a specific class in Logback, we can define a logger for that class with the level set to OFF. This will silence all logging calls from the class.

4.1. Our VerboseClass

Let’s create our example VerboseClass to illustrate this tutorial:

public class VerboseClass {

    private static final Logger logger = LoggerFactory.getLogger(VerboseClass.class);

    public void process() {
        logger.info("Processing data in VerboseClass...");
    }

    public static void main(String[] args) {
        VerboseClass instance = new VerboseClass();
        instance.process();
        logger.info("Main method completed in VerboseClass");
    }
}

Then we can run it to see the log output:

17:49:53.901 [main] INFO  c.b.l.disableclass.VerboseClass - Processing data in VerboseClass... 
17:49:53.902 [main] INFO  c.b.l.disableclass.VerboseClass - Main method completed in VerboseClass 

4.2. Disabling Logging for VerboseClass

To disable its logs, add a logger entry in logback.xml:

<logger name="com.baeldung.logback.disableclass.VerboseClass" level="OFF"/>

Here’s how the logback.xml would look with this logger added:

<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n</pattern>
        </encoder>
    </appender>

    <logger name="com.baeldung.logback.disableclass.VerboseClass" level="OFF"/>

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

With this configuration, VerboseClass will no longer output logs, while other classes will continue logging at the INFO level or above.

Finally, we can run this class again and see that no logs are displayed.

5. Conclusion

In summary, disabling logging from specific classes in Logback is a powerful feature that helps manage the signal-to-noise ratio in application logs. Setting the logging level to OFF for verbose or non-essential classes ensures that logs remain clear and meaningful. This also affects the overall performance and security of the application.

The example code from this article can be found over on GitHub.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.