Spring Boot + Logback日志输出文件配置

一.配置模板下载

如果你使用“Starters”,默认使用Logback 来记录日志。如果想要使用Logging, Log4J, or SLF4J ,也可以兼容使用。点击:Spring Boot LogBack 默认配置,选择对应的文件拷贝即可,如图1-1所示:

图1-1

file-appender.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
File appender logback configuration provided for import, equivalent to the programmatic
initialization performed by Boot
-->

<included>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
            <charset>${FILE_LOG_CHARSET}</charset>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
            <cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
            <maxFileSize>${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}</maxFileSize>
            <totalSizeCap>${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}</totalSizeCap>
            <maxHistory>${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-7}</maxHistory>
        </rollingPolicy>
    </appender>
</included>

Spring Boot官方推荐使用logback-spring.xml的文件命名方式,而不是logback.xml.理由是使用logback-spring.xml可以让Spring完全控制日志的初始化。

二.配置文件重要标签说明

1.<fileNamePattern/> 是强制属性,定义了归档日志的名称。Logback官网对于这个标签的描述很多,可以自行查看。特别重要的一点是,归档日志文件的滚转周期由标签值推断出来。

Logback官网还给出了一些<fileNamePattern/>标签值,对应的滚转周期,以及影响解释。

如表1-1所示:

fileNamePattern

Rollover schedule

Example

/wombat/foo.%d

Daily rollover (at midnight). Due to the omission of the optional time and date pattern for the %d token specifier, the default pattern of yyyy-MM-dd is assumed, which corresponds to daily rollover.

file property not set: During November 23rd, 2006, logging output will go to the file /wombat/foo.2006-11-23. At midnight and for the rest of the 24th, logging output will be directed to /wombat/foo.2006-11-24.

file property set to /wombat/foo.txt: During November 23rd, 2006, logging output will go to the file /wombat/foo.txt. At midnight, foo.txt will be renamed as /wombat/foo.2006-11-23. A new /wombat/foo.txt file will be created and for the rest of November 24th logging output will be directed to foo.txt.

/wombat/%d{yyyy/MM}/foo.txt

Rollover at the beginning of each month.

file property not set: During the month of October 2006, logging output will go to /wombat/2006/10/foo.txt. After midnight of October 31st and for the rest of November, logging output will be directed to /wombat/2006/11/foo.txt.

file property set to /wombat/foo.txt: The active log file will always be /wombat/foo.txt. During the month of October 2006, logging output will go to /wombat/foo.txt. At midnight of October 31st, /wombat/foo.txt will be renamed as /wombat/2006/10/foo.txt. A new /wombat/foo.txt file will be created where logging output will go for the rest of November. At midnight of November 30th, /wombat/foo.txt will be renamed as /wombat/2006/11/foo.txt and so on.

/wombat/foo.%d{yyyy-ww}.log

Rollover at the first day of each week. Note that the first day of the week depends on the locale.

Similar to previous cases, except that rollover will occur at the beginning of every new week.

/wombat/foo%d{yyyy-MM-dd_HH}.log

Rollover at the top of each hour.

Similar to previous cases, except that rollover will occur at the top of every hour.

/wombat/foo%d{yyyy-MM-dd_HH-mm}.log

Rollover at the beginning of every minute.

Similar to previous cases, except that rollover will occur at the beginning of every minute.

/wombat/foo%d{yyyy-MM-dd_HH-mm, UTC}.log

Rollover at the beginning of every minute.

Similar to previous cases, except that file names will be expressed in UTC.

/foo/%d{yyyy-MM,aux}/%d.log

Rollover daily. Archives located under a folder containing year and month.

In this example, the first %d token is marked as auxiliary. The second %d token, with time and date pattern omitted, is then assumed to be primary. Thus, rollover will occur daily (default for %d) and the folder name will depend on the year and month. For example, during the month of November 2006, archived files will all placed under the /foo/2006-11/ folder, e.g /foo/2006-11/2006-11-14.log.

表1-1

2.<maxHistory/>是可选属性,控制着归档文件的最大保留数量,默认值是0,不会删除归档文件。在查官网前,个人搜索了其他文章,对于这个标签值的单位网上文章多掐头去尾的认为是天或者月。其实根据官网的说法,标签值只是一个数量,具体单位参考表1-1的Rollover schedule描述。例如每月滚转一次,那么该标签值的单位就是月。

3.<maxFileSize/>是可选属性,控制着每个归档文件的大小,默认值是10MB。单位可以是字节,KB,MB和MB。5000000, 5000KB, 5MB是等价的。

4.<totalSizeCap/>是可选属性,控制着所有归档文件的大小,默认值0,不限制总归档文件的大小。<maxHistory/>标签值最先应用,然后<totalSizeCap/>才应用。

5.<cleanHistoryOnStart/>默认属性值为false。当设置为true时,appender启动会移除历史归档文件。

三.结语

写下本文主要是在解决日志归档输出不正确,用于记录相关文档。同时吐槽一下Logback官网主页,这入口找半天,也太限流了吧。Logback主页如图3-1所示:

图3-1

引用文档:

Logback主页
Spring Boot官方文档logging部分
Spring Boot Logback默认配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值