Java – logback creates a log file in a folder named the current date
In my current project, I want the date to create a log file, that is, the log file should reside in it
The current appender I'm using looks like this (it archives log files according to size)
<appender name="AUDITFILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${PROJECT_HOME}\\projectname\\audits\\myproject.log</file> <append>true</append> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${PROJECT_HOME}\\projectname\\audits\\myproject_%d{yyyy-MM-dd}.%i.zip </fileNamePattern> <maxHistory>10</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10KB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%date %msg%n </pattern> </encoder> </appender>
Solution
As described in the documentation for filenamepattern, you can specify multiple% d tags to place the date in the folder name of the archive file name:
<fileNamePattern>${PROJECT_HOME}\\projectname\\audits\\%d{yyyy-MM,aux}\\myproject_%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
Note that only one% d tag can be the primary tag, and all other tags must be marked as secondary tags by passing the 'aux' parameter
However, if you also want to put it in a file name other than the archive file name, you have two options:
>Use the < timestamp / > element to set the variables used in the path However, this timestamp is only set once at startup, so it is good for batch operation, but not for services. > As described above (1), but the packaging < appender / > and < timestamp / > use the siftingappender, if logback > = 1.0 In version 12, the timestamp will be re evaluated Not sure how you want to configure siftingappender But I hope it will get you on track