properties配置
通常log42j的配置都是通过xml的语法形式,但也支持java配置文件properties的形式来配置
# Rolling file appender
log4j2.appender.rolling.type = RollingRandomAccessFile
log4j2.appender.rolling.name = RollingFile
log4j2.appender.rolling.fileName = ${karaf.data}/log/karaf.log
#log4j2.appender.rolling.filePattern = ${karaf.data}/log/karaf.log.%i
log4j2.appender.rolling.filePattern = ${karaf.data}/log/karaf_%d{yyyy-MM-dd-HH-mm}_%i.log
log4j2.appender.rolling.rollstrategy.type = DefaultRolloverStrategy
log4j2.appender.rolling.rollstrategy.max = 5
log4j2.appender.rolling.append = true
log4j2.appender.rolling.layout.type = PatternLayout
log4j2.appender.rolling.layout.pattern = ${log4j2.pattern}
log4j2.appender.rolling.policies.type = Policies
log4j2.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.size = 16MB
log4j2.appender.rolling.policies.timepolice.type = TimeBasedTriggeringPolicy
log4j2.appender.rolling.policies.timepolice.interval = 1
log4j2.appender.rolling.policies.timepolice.modulate = true
xxx.type代表的对应的一个节点的开始,节点类型通过type来设置对应
比如rolling.type=RollingRandomAccessFile 将rolling设置为RollingRandomAccessFile类型,之后就可以通过
- rolling.name
- rolling.fileName
来设置这个节点的属性,其中rolling名字为自定义的名称
对应的xml配置应该为
<RollingRandomAccessFile name = "RollingFile", fileName="${karaf.data}/log/karaf.log">
...
</RollingRandomAccessFile>
配置相关科参考官方文档
https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingRandomAccessFileAppender
https://logging.apache.org/log4j/2.x/manual/configuration.html
TimeBasedTriggeringPolicy
该属性必须和filePattern配合
如filePattern = ${karaf.data}/log/karaf_%d{yyyy-MM-dd-HH-mm}%i.log 代表按分钟划分文件
filePattern = ${karaf.data}/log/karaf%d{yyyy-MM-dd}_%i.log 代表按天划分文件
DefaultRolloverStrategy
<DefaultRolloverStrategy max=5>表示保存的文件个数,如果和TimeBasedTriggeringPolicy策略按时间段划分文件配合,则表示该时间段内能保存的最大文件个数
custom delete action
<DefaultRolloverStrategy>节点下的 custom delete action 用于自定义文件删除的策略,如设置多少时间后删除对应的文件
这个功能需要 log4j-2.5以后的版本,具体可参考文档
https://logging.apache.org/log4j/2.x/manual/appenders.html#CustomDeleteOnRollover
网友评论