原文链接:https://blog.csdn.net/zheng0518/article/details/69558893
最近由于项目的需要,我们把log4j 1.x的版本全部迁移成log4j 2.x 的版本,那随之而来的slf4j整合log4j的配置,以及log4j2配置文件的详解。本文就专门来讲解下log4j2.xml配置文件的各项标签的意义。
log4j 2.x版本不再支持像1.x中的.properties后缀的文件配置方式,2.x版本配置文件后缀名只能为".xml",".json"或者".jsn".
系统选择配置文件的优先级(从先到后)如下:
我们一般默认使用log4j2.xml进行命名。如果本地要测试,可以把log4j2-test.xml放到classpath,而正式环境使用log4j2.xml,则在打包部署的时候不要打包log4j2-test.xml即可。
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
Configuration
status
=
"WARN"
>
<
Appenders
>
<
Console
name
=
"Console"
target
=
"SYSTEM_OUT"
>
<
PatternLayout
pattern
=
"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
/>
Console
>
Appenders
>
<
Loggers
>
<
Root
level
=
"error"
>
<
AppenderRef
ref
=
"Console"
/>
Root
>
Loggers
>
Configuration
>
|
共有8个级别,按照从低到高为:All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
程序会打印高于或等于所设置级别的日志,设置的日志等级越高,打印出来的日志就越少。
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
configuration
status
=
"WARN"
monitorInterval
=
"30"
>
<
appenders
>
<
console
name
=
"Console"
target
=
"SYSTEM_OUT"
>
<
PatternLayout
pattern
=
"[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"
/>
console
>
<
File
name
=
"log"
fileName
=
"log/test.log"
append
=
"false"
>
<
PatternLayout
pattern
=
"%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"
/>
File
>
<
RollingFile
name
=
"RollingFileInfo"
fileName
=
"${sys:user.home}/logs/info.log"
filePattern
=
"${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"
>
<
ThresholdFilter
level
=
"info"
onMatch
=
"ACCEPT"
onMismatch
=
"DENY"
/>
<
PatternLayout
pattern
=
"[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"
/>
<
Policies
>
<
TimeBasedTriggeringPolicy
/>
<
SizeBasedTriggeringPolicy
size
=
"100 MB"
/>
Policies
>
RollingFile
>
<
RollingFile
name
=
"RollingFileWarn"
fileName
=
"${sys:user.home}/logs/warn.log"
filePattern
=
"${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log"
>
<
ThresholdFilter
level
=
"warn"
onMatch
=
"ACCEPT"
onMismatch
=
"DENY"
/>
<
PatternLayout
pattern
=
"[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"
/>
<
Policies
>
<
TimeBasedTriggeringPolicy
/>
<
SizeBasedTriggeringPolicy
size
=
"100 MB"
/>
Policies
>
<
DefaultRolloverStrategy
max
=
"20"
/>
RollingFile
>
<
RollingFile
name
=
"RollingFileError"
fileName
=
"${sys:user.home}/logs/error.log"
filePattern
=
"${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log"
>
<
ThresholdFilter
level
=
"error"
onMatch
=
"ACCEPT"
onMismatch
=
"DENY"
/>
<
PatternLayout
pattern
=
"[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"
/>
<
Policies
>
<
TimeBasedTriggeringPolicy
/>
<
SizeBasedTriggeringPolicy
size
=
"100 MB"
/>
Policies
>
RollingFile
>
appenders
>
<
loggers
>
<
logger
name
=
"org.springframework"
level
=
"INFO"
>
logger
>
<
logger
name
=
"org.mybatis"
level
=
"INFO"
>
logger
>
<
root
level
=
"all"
>
<
appender-ref
ref
=
"Console"
/>
<
appender-ref
ref
=
"RollingFileInfo"
/>
<
appender-ref
ref
=
"RollingFileWarn"
/>
<
appender-ref
ref
=
"RollingFileError"
/>
root
>
loggers
>
configuration
>
|