Search code examples
logback

logback how to store log file in folder having name as current date


following logback.xml will create a log file, but i want to create new folder every day which have same name as current date and store the new log file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <property name="DEV_HOME" value="/home/gaurav/flinklogs" />


   <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${DEV_HOME}/logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history capped at 3GB total size -->
      <maxHistory>30</maxHistory>
      <totalSizeCap>3GB</totalSizeCap>

    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="ERROR">
    <appender-ref ref="FILE" />
  </root>

</configuration>

I also tried following filenamepattern but its not working

<fileNamePattern>${DEV_HOME}/%d{yyyy/MM, aux}/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

it is creatin log file in home/gaurav/flinklogs/logFile.log


Solution

  • If both <file> and <fileNamePattern> are specified, the the current log file is located as specified in <file> and archive log files are located as specified in the <fileNamePattern> - see documentation.

    You need to remove <file>${DEV_HOME}/logFile.log</file> and then change <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern> to <fileNamePattern>${DEV_HOME}/%d{yyyy/MM, aux}/logFile.%d{yyyy-MM-dd}.log</fileNamePattern> and it should work the way you want it to.