java logback 在文件夹内创建日志文件,名称为当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16278720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:30:01  来源:igfitidea点击:

logback create log files inside folder having name as current date

javalogbackappender

提问by jai shukla

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 看起来像这样(它根据大小对日志文件进行归档)。

    <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>

回答by David Roussel

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文档中所述,您可以指定多个 %d 标记,以便将日期放在存档文件名的文件夹名称中:

<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.

请注意,只有一个 %d 标记可以是主要标记,所有其他标记必须通过传递 'aux' 参数标记为辅助标记。

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.

  1. 使用<timestamp />元素来设置您在路径中使用的变量。但是这个时间戳只会在启动时设置一次,所以它适用于批量运行但不适用于服务。

  2. 像上面的 (1) 一样,但是用 a包裹<appender/>和,如果使用版本的 logback >=1.0.12,这将允许重新评估时间戳。不确定您希望如何配置. 但希望这会让你走上正确的轨道。<timestamp />SiftingAppenderSiftingAppender