Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

java - logback create log files inside folder having name as current date

In my current project i want to create log files date wise i.e. log files should reside inside
folder having name as date. Also archiving should happen at that particular folder.

Current appender that i am using looks like this (it does archiving of log file based on 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As mentioned in the documentation for fileNamePattern, you can specify multiple %d tokens so as put the date in the folder name of the archive filename:

<fileNamePattern>${PROJECT_HOME}\projectname\audits\%d{yyyy-MM, aux}\myproject_%d{yyyy-MM-dd}.%i.zip</fileNamePattern>

Note that only one %d token can be primary, all other tokens must be marked as auxiliary by passing the 'aux' parameter.

But if you also want to put it in the file name of the non-archive filename, then you have two options:

  1. use a <timestamp /> element to set a variable which you use in the path. But this timestamp will only be set once at startup, so it's good for batch runs but not for services.

  2. Do like (1) above, but wrap the <appender/> and the <timestamp /> with a SiftingAppender, which will enable the timestamp to be re-evaluated, if using version of logback >=1.0.12. Not sure exactly how you'd want to configure the SiftingAppender. But hopefully that will put you on the right track.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...