Keith Enlow
Keith Enlow

Reputation: 914

Logback XML Timestamp Printing Literal Expression String

I'm trying to consolidate all my log files for my application into a more structured and organized fashion. I'm using logback and in short I'm trying to get my log files to roll per day - per month to a file directory of pattern logs/MM-yyyy/application.MM-dd-yyyy.log.

Below you can see my current logback.xml file with the configuration:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_FILE" value="logs/%d{MM-yyyy}/application.%d{MM-dd-yyyy}.log"/>
    <property name="LOG_FILE_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${LOG_FILE_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}</fileNamePattern>
        </rollingPolicy>
    </appender>

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

The problem is that for some reason the logs are actually going to a literal file structure of logs/%d{MM-yyyy}/application.%d{MM-dd-yyyy}.log where the expressions that are supposed to print dates instead are actually just printing the expression as a string.

Can anyone see a problem in what I'm doing or give a better solution for what I'm doing? All help appreciated. Cheers!

Upvotes: 0

Views: 1207

Answers (1)

sheltem
sheltem

Reputation: 3825

You can define multiple %d{} in the fileNamePattern, but you have to mark any additional ones as auxiliary. As per the examples in the logback manual (http://logback.qos.ch/manual/appenders.html#tbrpFileNamePattern scroll down to the examples), changing your LOG_FILE-property to the following should roll over every day, sorting each log file into folders:

<property name="LOG_FILE" value="logs/%d{MM-yyyy,aux}/application.%d{MM-dd-yyyy}.log"/>

Since the <file>-tag has no pattern replacement, you will have to give a regular path to where the currently active log file should reside:

<file>logs/application.log</file>

While I can not currently test and verify it where I am, the end result should be a structure like this:

$APP_ROOT/logs/application.log (Currently active log file)
$APP_ROOT/logs/12-2014/*.log (Daily rolled over logs for December 2014)
$APP_ROOT/logs/11-2014/*.log (Daily rolled over logs for November 2014)
...

Upvotes: 4

Related Questions