2

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

Gaurav
  • 173
  • 1
  • 13
  • 1
    Possible duplicate of [logback create log files inside folder having name as current date](https://stackoverflow.com/questions/16278720/logback-create-log-files-inside-folder-having-name-as-current-date) – Igand Jun 25 '18 at 12:43
  • no thats not solving my problem – Gaurav Jun 25 '18 at 12:48

2 Answers2

3

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.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • thanks it worked, but this first create year folder then under it month folder i want it as a single folder having name as current date – Gaurav Jun 26 '18 at 05:13
  • Because there is a slash in pattern `%d{yyyy/MM, aux}`. Change it to `%d{yyyy-MM, aux}` and you will have a single folder per month, eg. `2018-06` or to `%d{yyyy-MM-dd, aux}` to have a single folder per day, eg. `2018-06-26`. – Konrad Botor Jun 26 '18 at 06:04
0

I made a sample code using the answer of "Konrad Botor". Refer to the below code.

<appender name="dailyAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${payloadLoggingFilePath}-%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%m%n</pattern>
        </encoder>
장재훈
  • 621
  • 1
  • 6
  • 9