Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

A common logging configuration is a log file per day. Building off of Logback Configuration File Change, let’s determine what it would take to also add a daily rolling file. How do you do this with logback?

In this example, I turned on the automatic scanning of the logback configuration file and set it to check every 5 minutes. This is a good compromise between being able to change the logging configuration on the file and also preventing increased load on the production file system.

I also created several variables to make customizing the configuration to individual developers workstation’s easy.

In this example, I used a TimeBasedRollingAppender to roll the file once per day. Logback keeps 30 days of files around and doesn’t use compression. To enable compression, change the fileNamePattern inside the rollingPolicy to end in .gz or .zip instead of .log.

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 minutes">

    <property name="contextName" value="bonus-services"/>
    <!--
        The directory that the log files will be created in.  If the directory does not exist, Logback will give an
        error.  Logback will continue to log to the console, but there will be no log created.  Some developers will
        prefer this way so that they can see the files in the console, but don't have to worry about them on their local
        machines.

        To create a symbolic link (/opt/tomcat) to your actual tomcat installation:
        sudo ln -s ~/apache-tomcat-8.0.28 /opt/tomcat
    -->
    <property name="loggingDir" value="/opt/tomcat/logs"/>
    <property name="encoderPattern"
              value="%d{HH:mm:ss.SSS}|%-5level|${HOSTNAME}|${contextName}|%msg ||%class:%line %xException{full} %n"/>


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>${encoderPattern}</pattern>
        </encoder>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${loggingDir}/${contextName}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${contextName}.%d{yyyy-MM-dd}.log</fileNamePattern>

            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>${encoderPattern}</pattern>
        </encoder>
    </appender>

    <logger name="com.cdi" level="INFO"/>
    <!-- Show info on rest calls -->
    <logger name="org.springframework.web.client" level="DEBUG"/>
    <logger name="org.springframework.http.converter" level="DEBUG"/>

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

December 15th, 2015

Posted In: java ninja, Javaninja, logback

Leave a Reply

Your email address will not be published. Required fields are marked *

WP to LinkedIn Auto Publish Powered By : XYZScripts.com