logback在名称为当前日期的文件夹中创建日志文件 [英] logback create log files inside folder having name as current date

查看:2392
本文介绍了logback在名称为当前日期的文件夹中创建日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我希望日期创建日志文件,即日志文件应位于名称为日期的

文件夹中。
此外,归档应该发生在该特定文件夹中。

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.

我使用的当前appender看起来像这样(它根据大小存档日志文件)。 / p>

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>


推荐答案

fileNamePattern ,您可以指定多个%d标记,以便将日期放在存档文件名的文件夹名称中:

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>

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

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. 使用< timestamp /> 元素设置您在路径中使用的变量。但是这个时间戳只会在启动时设置一次,所以它对批量运行有好处,但不适用于服务。

  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.

如上所述(1),但包装< appender /> < timestamp /> 带有 SiftingAppender ,如果使用logback> = 1.0.12的版本,将启用时间戳重新评估。不确定您要如何配置 SiftingAppender 。但希望这会让你走上正轨。

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.

这篇关于logback在名称为当前日期的文件夹中创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆