Logback file属性 与 fileNamePattern属性的关系

https://juejin.cn/post/6844903859865780231

官方原文

Note that the file property in RollingFileAppender (the parent of TimeBasedRollingPolicy) can be either set or omitted. By setting the file property of the containing FileAppender, you can decouple the location of the active log file and the location of the archived log files. The current logs will be always targeted at the file specified by the file property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit the file property, then the active file will be computed anew for each period based on the value of fileNamePattern.


翻译:

  1. RollingFileAppender的file属性可以设置,也可以忽略。
  2. 如果设置了FileAppender包含的属性file,系统实时生成的日志和根据日期生成的日志可以存储在不同的目录文件下。在这种情况下,系统实时生成的日志内容都会记录在file属性指定的文件中。因此,该日志文件名称不会随着时间的移动而更改。
  3. 如果忽略了FileAppender包含的属性file,活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次,即每隔一段时间会生成一个日志文件。

举例说明

<appender name="emergencyLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- 写入日志内容的文件名称(目录) -->
    <File>log/check.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
        <fileNamePattern>log/check.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- 每产生一个日志文件,该日志文件的保存期限为30天 -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <!-- pattern节点,用来设置日志的输入格式 -->
        <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger [%msg]%n</pattern>
        <!-- 记录日志的编码:此处设置字符集 - -->
        <charset>UTF-8</charset>
    </encoder>
</appender>
复制代码

以2019-06-04输出的日志为例,来解释下设置、忽略File属性的不同。

设置File属性

  1. 系统会将日志内容全部写入log/check.log中。
  2. 在2019-06-05凌晨,check.log会被重命名为log/check.2019-06-04.log
  3. 然后再生成新的check.log文件,按照上面的步骤生成log/check.2019-06-05.loglog/check.2019-06-06.log等日志。

忽略File属性

  1. 系统会将日志内容直接写入log/check.2019-06-04.log中。
  2. 在2019-06-05凌晨,系统会将日志内容直接写入log/check.2019-06-04.log

总结

仍以2019-06-04为例,如果你设置了File属性,当天你只能看到check.log日志文件,2019-06-05才会看到check.201-06-04.log文件。但是如果你忽略了,你当天就能看到check.2019-06-04.log文件,但你始终看不到check.log文件。


作者:风吟空城
链接:https://juejin.cn/post/6844903859865780231
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @   _海阔天空  阅读(1207)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示