Markus Jevring
Markus Jevring

Reputation: 832

Logback sometimes does not write to the log file, and sometimes does not roll the log file

Sometimes when I launch my java application, logback refuses to write anything to my logfile. Sometimes it also refuses to roll the logfile at midnight (or at the first logging event after midnight), which results in logging events being lost to the void. When i look at my main log file when logbacks has failed to roll the log, it will have a time like 23:59, with yesterday's date, and any and all logging statements after that time will be irretrievably lost. I have a fairly simple configuration file, and it looks correct. It certainly should be correct, as it works most of the time.

Here's my configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <!--and http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy-->
    <!--for further documentation-->
    <append>true</append>
    <File>aggregator.log</File>
    <encoder>
        <!-- was: %d{yyyy-MM-dd HH:mm:ss}%5p [%t] (%F:%L) - %msg%n -->
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- By setting the name to .gz here, we get free compression. -->
      <fileNamePattern>aggregator.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
    </rollingPolicy>
  </appender>
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="file"/>
    <appender-ref ref="console"/>
  </root>
</configuration>

Unfortunately, I cannot reliably reproduce this error, so debugging it is a bit difficult. Could someone tell me either what I'm doing wrong, or what else might be the problem? If it's of any help, I redirect STDOUT and STDERR to /dev/null (I run on linux, btw).

Upvotes: 3

Views: 11203

Answers (2)

Markus Jevring
Markus Jevring

Reputation: 832

It turns out that this had very little to do with logback at all. The problem was that I had a .policy file that didn't specify the proper permissions for the applications. The times when I thought I managed to rotate files was times when I had moved or deleted the previous ones by hand. I solved this by making sure that logback had sufficient permissions to rotate its own logs.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328594

To debug the issue, use <configuration debug="true"> and don't redirect stdout. Logback will print messages there as it parses the config and when something goes wrong.

Upvotes: 9

Related Questions