Skip to content

Tune Logging

Wuyi Chen edited this page May 24, 2019 · 26 revisions

Logging Framework

Spring Boot (starter) uses Logback by default and it also supports

Default Configuration for logging

Aspect Default Other Possibles Options
Log Framework Logback Java Util Logging, Commons Logging, Log4J, SLF4J
Log Level INFO ERROR, WARN, INFO, DEBUG, TRACE
Log Output output to console only output to file, output to syslog endpoint and so forth

Modify application configuration

You can change the application configuration for tuning the logging.

application.properties

logging.config=                                      # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback.
logging.exception-conversion-word=%wEx               # Conversion word used when logging exceptions.
logging.file=                                        # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.
logging.file.max-history=0                           # Maximum of archive log files to keep. Only supported with the default logback setup.
logging.file.max-size=10MB                           # Maximum log file size. Only supported with the default logback setup.
logging.group.*=                                     # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`.
logging.level.*=                                     # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.
logging.path=                                        # Location of the log file. For instance, `/var/log`.
logging.pattern.console=                             # Appender pattern for output to the console. Supported only with the default Logback setup.
logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS   # Appender pattern for log date format. Supported only with the default Logback setup.
logging.pattern.file=                                # Appender pattern for output to a file. Supported only with the default Logback setup.
logging.pattern.level=%5p                            # Appender pattern for log level. Supported only with the default Logback setup.
logging.register-shutdown-hook=false                 # Register a shutdown hook for the logging system when it is initialized.

Output log to different destination

In the resources directory of your application, you can add the logback-spring.xml file to acquire advanced logging options with Logback. Spring Boot will scan that file when it is starting.

This is the basic template for logback-spring.xml:

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender>
        <!-- appender parameters -->
    </appender>
    <root level="INFO">
        <!-- appender references -->
    </root>
</configuration>

Output to console

After adding the logback-spring.xml file, the default logging configuration of Spring Boot will be disabled partially and the logging information will not be outputted to console. You need to change the logback-spring.xml file for outputting the logging information to console.

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>    <!-- load Spring default values for logback  -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">           <!-- output the log to console (standard output) -->
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>                              <!-- use Spring default log pattern -->
            <charset>utf8</charset>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Output to log file

The logging information can be exported to log files too.

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>             <!-- load Spring default values for logback  -->
    <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>mylog.txt</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">               
            <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>                  <!-- rollover daily -->
            <maxFileSize>100MB</maxFileSize>                                                <!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
            <maxHistory>60</maxHistory>
            <totalSizeCap>20GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>                                       <!-- use Spring default log pattern -->
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="ROLLING" />
    </root>
</configuration>

Output to syslog endpoint (UDP)

You can also send the logging information to a syslog endpoint, that syslog server can be local or remote (in cloud). This is the example for sending logging to Papertrail syslog endpoint:

syslog://logs6.papertrailapp.com:50395

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">     <!-- redirect the log to syslog endpoint -->
        <syslogHost>logs6.papertrailapp.com</syslogHost>
        <facility>SYSLOG</facility>
        <port>50395</port>
        <suffixPattern> [%thread] %logger %msg</suffixPattern>
    </appender>
    <root level="INFO">
        <appender-ref ref="SYSLOG" />
    </root>
</configuration>

Load parameters from application configuration (.yml or .properties) into logback-spring.xml

You can use values in the application configuration (.yml or .properties) into logback-spring.xml.

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <springProperty scope="context" name="appName" source="spring.application.name"/>  <!-- load application name from application configuration -->
    <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
        <syslogHost>127.0.0.1</syslogHost>                                             <!-- Splunk server is running in local  -->
        <facility>SYSLOG</facility>
        <port>50000</port>                                                             <!-- use the port number when you set up the UDP data input point  -->
        <suffixPattern>${appName} [%thread] %logger %msg</suffixPattern>               <!-- use ${appName} as application name -->
    </appender>
    <root level="INFO">
        <appender-ref ref="SYSLOG" />
    </root>
</configuration>
Clone this wiki locally