支持日志框架:Java Util Logging, Log4J2 and Logback,默认是使用logback
配置方式:默认配置文件配置和引用外部配置文件配置
一、 默认配置文件配置(不建议使用:不够灵活,对log4j2等不够友好)
# 日志文件名,比如:roncoo.log,或者是 /var/log/roncoo.log
logging.file=roncoo.log
# 日志级别配置,比如: logging.level.org.springframework=DEBUG
logging.level.*=info
logging.level.org.springframework=DEBUG
二、 引用外部配置文件
1 logback配置方式:
spring boot默认会加载classpath:logback-spring.xml或者classpath:logback-spring.groovy
使用自定义配置文件,配置方式为:
logging.config=classpath:logback-roncoo.xml
注意:不要使用logback这个来命名,否则spring boot将不能完全实例化
2.1自定义配置 logback-roncoo.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
configuration
>
<
property
name
=
"PATTERN"
value
=
"%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n"
/>
<
property
name
=
"TEST_FILE_PATH"
value
=
"c:/opt/roncoo/logs"
/>
<
property
name
=
"PRO_FILE_PATH"
value
=
"/opt/roncoo/logs"
/>
<
springProfile
name
=
"dev"
>
<
appender
name
=
"CONSOLE"
class
=
"ch.qos.logback.core.ConsoleAppender"
>
<
encoder
>
<
pattern
>${PATTERN}
pattern
>
encoder
>
appender
>
<
logger
name
=
"com.roncoo.education"
level
=
"debug"
/>
<
root
level
=
"info"
>
<
appender-ref
ref
=
"CONSOLE"
/>
root
>
springProfile
>
<
springProfile
name
=
"test"
>
<
appender
name
=
"TEST-FILE"
class
=
"ch.qos.logback.core.rolling.RollingFileAppender"
>
<
file
>${TEST_FILE_PATH}
file
>
<
rollingPolicy
class
=
"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<
fileNamePattern
>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log
fileNamePattern
>
<
MaxHistory
>100
MaxHistory
>
rollingPolicy
>
<
layout
class
=
"ch.qos.logback.classic.PatternLayout"
>
<
pattern
>${PATTERN}
pattern
>
layout
>
appender
>
<
root
level
=
"info"
>
<
appender-ref
ref
=
"TEST-FILE"
/>
root
>
springProfile
>
<
springProfile
name
=
"prod"
>
<
appender
name
=
"PROD_FILE"
class
=
"ch.qos.logback.core.rolling.RollingFileAppender"
>
<
file
>${PRO_FILE_PATH}
file
>
<
rollingPolicy
class
=
"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<
fileNamePattern
>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log
fileNamePattern
>
<
MaxHistory
>100
MaxHistory
>
rollingPolicy
>
<
layout
class
=
"ch.qos.logback.classic.PatternLayout"
>
<
pattern
>${PATTERN}
pattern
>
layout
>
appender
>
<
root
level
=
"warn"
>
<
appender-ref
ref
=
"PROD_FILE"
/>
root
>
springProfile
>
configuration
>
|
2.2 log4j配置
2.2.1去除logback的依赖包,添加log4j2的依赖包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
dependency
>
<
groupId
>org.springframework.boot
groupId
>
<
artifactId
>spring-boot-starter-web
artifactId
>
<
exclusions
>
<
exclusion
>
<
groupId
>org.springframework.boot
groupId
>
<
artifactId
>spring-boot-starter-logging
artifactId
>
exclusion
>
exclusions
>
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot
groupId
>
<
artifactId
>spring-boot-starter-log4j2
artifactId
>
dependency
>
|
2.2.2 在classpath添加log4j2.xml或者log4j2-spring.xml(spring boot 默认加载),(可以通过文件名来区分不同环境logging.config=classpath:log4j2-dev.xml,logging.config=classpath:log4j2-test.xml,logging.config=classpath:log4j2-prod.xml)
2.3 自定义配置文件 log4j2-dev.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
configuration
>
<
properties
>
<
property
name
=
"PATTERN"
>%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n
property
>
properties
>
<
appenders
>
<
Console
name
=
"CONSOLE"
target
=
"system_out"
>
<
PatternLayout
pattern
=
"${PATTERN}"
/>
Console
>
appenders
>
<
loggers
>
<
logger
name
=
"com.roncoo.education"
level
=
"debug"
/>
<
root
level
=
"info"
>
<
appenderref
ref
=
"CONSOLE"
/>
root
>
loggers
>
configuration
>
|
三.比较
性能比较:Log4J2 和 Logback 都优于 log4j(不推荐使用)
配置方式:Logback最简洁,spring boot默认,推荐使用