How to Print Only Error in Console in Micronaut or Spring Application

To update the logging configuration in your application to avoid displaying info and debug logs in the console, you can modify the logback.xml or logback-spring.xml file. This configuration file allows you to control various aspects of logging, including log levels and log destinations. Here’s a step-by-step guide on how to achieve this:

1. Locate Your Logging Configuration File:

First, you need to find the configuration file responsible for your application’s logging. In many Java-based applications, especially those built with Spring or Micronaut, the default logging configuration file is usually named logback.xml. Sometimes, it’s named logback-spring.xml.

2. Open the Configuration File:

Open the logback.xml or logback-spring.xml file in a text editor or IDE.

3. Find the Root Logger Configuration:

Inside the configuration file, locate the <root> element. This element defines the default logging settings for your application. It typically looks like this:

<root level="error">
    <!-- Define log destination, layout, etc. -->
</root>

4. Adjust the Log Level:

To avoid displaying info and debug logs in the console, you need to set the level attribute of the <root> element to a higher logging level. Common logging levels, from highest to lowest severity, are error, warn, info, debug, and trace. To filter out info and debug logs, set the level to error or warn. For example:

<root level="warn">
    <!-- Define log destination, layout, etc. -->
</root>

5. Save the Configuration File:

Save the changes you made to the configuration file.

6. Restart Your Application:

After modifying the logging configuration, you usually need to restart your application for the changes to take effect.

With this configuration, your application will only log messages with a severity level of warn, error, or higher to the console. Info and debug messages will be omitted.

Related Post