Spring Boot Logging: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 42: Line 42:
can be set in [[application.yml]]/[[application.properties]]. This generates a large quantity of logging, but it '''does not''' enable application-level DEBUG logging. If you need that, it will have to be configured specifically as shown below.
can be set in [[application.yml]]/[[application.properties]]. This generates a large quantity of logging, but it '''does not''' enable application-level DEBUG logging. If you need that, it will have to be configured specifically as shown below.


Similar to --debug, --trace can also be enabled.
Similar to --debug, --trace can also be enabled:
 
java -jar app.jar '''--trace'''


=application.yml Logging Configuration=
=application.yml Logging Configuration=
Line 53: Line 55:
     root: WARN
     root: WARN
</syntaxhighlight>
</syntaxhighlight>
Note that even if the root level has low verbosity, a more specific logger configuration enables higher verbosity for that specific logger only.


==Logger Level Configuration==
==Logger Level Configuration==

Latest revision as of 20:39, 23 November 2018

External

SpringBoot Documentation - Logging

Internal

Overview

By default, a Spring Boot application that uses starters has a Logback log runtime: logback-classic-....jar is present in the application JAR. Logging is configured by default to write to the console at INFO level. log4j support is also available (log4j-api-2.11.1.jar, log4j-to-slf4j-2.11.1.jar). The following statements will generate INFO logs at the console without any additional configuration:

@Slf4j
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
        log.info("in main()");
        ...
    }
}

The default log output includes:

  • Date and time with millisecond precision.
  • Log level (ERROR, WARN, INFO, DEBUG or TRACE).
  • Process ID
  • A "---" separator.
  • The thread name enclosed in square brackets and possibly truncated.
  • The logger name, possibly abbreviated.
  • The log message.

The default log configuration sends ERROR/WARN/INFO log messages to the console (standard out). DEBUG for core loggers (the container, Hibernate and Spring Boot) can be enabled globally for the application by specifying --debug on command line:

java -jar app.jar --debug

Alternatively:

debug: true

can be set in application.yml/application.properties. This generates a large quantity of logging, but it does not enable application-level DEBUG logging. If you need that, it will have to be configured specifically as shown below.

Similar to --debug, --trace can also be enabled:

java -jar app.jar --trace

application.yml Logging Configuration

Basic logging configuration can be applied directly in application.yml/application.properties. Root logging level can be controlled as such:

logging:
  level:
    root: WARN

Note that even if the root level has low verbosity, a more specific logger configuration enables higher verbosity for that specific logger only.

Logger Level Configuration

Individual loggers can be configured with entries prefixed by "logging.level", followed by the name of the logger:

logging:
  level:
    playground:
      example: DEBUG

Optionally, the long logger names can be collapsed on a single line, as follows. The above is equivalent with:

logging:
  level:
    playground.example: DEBUG

Setting a logging level on a specific logger configuration applies the same logging level on all logger subpackages.

File Output Configuration

logging:
  path: /var/log/
  file: my-file.log

More details about file output configuration can be found here: Spring Boot Logging - File Output.

logback.xml Logging Configuration

Better control over logging levels and format can be obtained by placing a Logback configuration file logback.xml in src/main/resources. This is an example of a simple logback.xml that will work:

<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>
  <logger name="root" level="INFO"/>
  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Note that the default log formatting uses a fancier, color format, and the above configuration will overwrite the default log formatting.


If the same logger is configured both in application.yml and in logback.xml, application.yml configuration takes precendenc.