文章目录 [hide]
背景
因为log4j2的占位符的强大便利, 以及log4j2的异步使得日志性能大提升几十倍, 所以要换!
正文
1 检查环境
因为apache官网已经说明, 2和1是水火不容的, 所以项目里面不能有log4j1
因为使用的是maven, 项目中又使用了jxl, 但是jxl又在编译中自动加载了log4j1, 所以刚开始一直以为用的是2, 结果加了{}测试log.info(“{}”, “test”)都打不出来, 才知道不对
2 加入log4j2
这个网上本来很多, 然而这里多来一笔也没什么不妥当
首先maven配置[全文github]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<dependency>
<groupId>
org
.
apache
.
logging
.
log4j
<
/
groupId
>
<artifactId>
log4j
-
api
<
/
artifactId
>
<version>
2.1
<
/
version
>
<
/
dependency
>
<dependency>
<groupId>
org
.
apache
.
logging
.
log4j
<
/
groupId
>
<artifactId>
log4j
-
core
<
/
artifactId
>
<version>
2.1
<
/
version
>
<
/
dependency
>
|
内容:新建文件log4j2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<configuration
debug
=
"off"
monitorInterval
=
"1800"
>
<Properties>
<Property
name
=
"log-path"
>
logs
</Property>
</Properties>
<Appenders>
<Console
name
=
"Console"
target
=
"SYSTEM_OUT"
>
<PatternLayout
pattern
=
"%d{HH:mm:ss.SSS} %-5level %class{36}.%M()/%L - %msg%xEx%n %replace{%logger }{\.}{/}"
/>
</Console>
<File
name
=
"app_debug"
fileName
=
"${log-path}/app/debug.log"
append
=
"false"
>
<Filters>
<ThresholdFilter
level
=
"info"
onMatch
=
"DENY"
onMismatch
=
"NEUTRAL"
/>
<ThresholdFilter
level
=
"debug"
onMatch
=
"ACCEPT"
onMismatch
=
"NEUTRAL"
/>
</Filters>
<PatternLayout
pattern
=
"%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"
/>
</File>
<File
name
=
"app_info"
fileName
=
"${log-path}/app/info.log"
append
=
"false"
>
<Filters>
<ThresholdFilter
level
=
"warn"
onMatch
=
"DENY"
onMismatch
=
"NEUTRAL"
/>
<ThresholdFilter
level
=
"info"
onMatch
=
"ACCEPT"
onMismatch
=
"DENY"
/>
</Filters>
<PatternLayout
pattern
=
"%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"
/>
</File>
<File
name
=
"app_error"
fileName
=
"${log-path}/app/error.log"
append
=
"false"
>
<Filters>
<ThresholdFilter
level
=
"warn"
onMatch
=
"ACCEPT"
onMismatch
=
"DENY"
/>
</Filters>
<PatternLayout
pattern
=
"%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"
/>
</File>
</Appenders>
<Loggers>
<Logger
name
=
"com.test.app"
level
=
"trace"
additivity
=
"false"
>
<appender-ref
ref
=
"Console"
/>
<appender-ref
ref
=
"app_debug"
/>
<appender-ref
ref
=
"app_info"
/>
<appender-ref
ref
=
"app_error"
/>
</Logger>
<root
level
=
"Info"
>
<appender-ref
ref
=
"Console"
/>
</root>
</Loggers>
</configuration>
|
3 写测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com
.
paymoon
.
demo
.
log
;
import
org
.
apache
.
logging
.
log4j
.
LogManager
;
import
org
.
apache
.
logging
.
log4j
.
Logger
;
public
class
logtest
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
logtest
.
class
)
;
public
static
void
main
(
String
[
]
args
)
{
logger
.
entry
(
)
;
logger
.
info
(
"Hello, World! {}"
,
"hello"
)
;
logger
.
exit
(
)
;
}
}
|
运行结果
20:57:03.488 INFO com.paymoon.demo.log.logtest.main()/12 – Hello, World! hello |
就对了.
要注意的点
1 log4j2的环境配置
eclipse中pom如果不看dependency hierarchy的话, 光看jar, 很容易出冲突问题的, 这个以前在csdn博客里面写过, 不过当时用的是myeclipse, 有graf图可以看, 但是在eclipse里面, 只有dependency hierarchy可以看, 但是还是很方便的. 具体点这里
|
2 代码中引用的包
测试类中
private static Logger logger = LogManager.getLogger(logtest.class);
的引用包, 分别是以下两个, 如果有错, 则说明写的不对
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
3 关于log4j2.xml
文件中的配置, 有一些信息一定要有, 否则无法显示日志,
我在添加好这个文件后, 就少加了一句话:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<Loggers>
<Logger
name
=
"com.test.app"
level
=
"trace"
additivity
=
"false"
>
<appender-ref
ref
=
"Console"
/>
<appender-ref
ref
=
"app_debug"
/>
<appender-ref
ref
=
"app_info"
/>
<appender-ref
ref
=
"app_error"
/>
</Logger>
<root
level
=
"Info"
>
<appender-ref
ref
=
"Console"
/>
</root>
</Loggers>
|
结果就一直以为哪里不对
4 多说一句.
log4j2除了xml支持, 还支持json[不过我猜没什么人在用吧?]
新加了一种支持YAML, 不过目前看来并没有什么卵用.
超越XML和JSON:YAML,Java开发人员的新选择 – 51CTO.COM
http://developer.51cto.com/art/200808/85469_all.htm
对了, 不支持prop文件.