Log4j提供的layout有以下几种
org.apache.log4j.HTMLLayout(以HTML表格形式布局),
org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
HTMLLayout实例,我们把前面的实例的配置文件改下
1
2
3
|
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.HTMLLayout
|
我们运行 看下输出信息:
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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
title
>Log4J Log Messages</
title
>
<
style
type
=
"text/css"
>
<!--
body, table {font-family: arial,sans-serif; font-size: x-small;}
th {background: #336699; color: #FFFFFF; text-align: left;}
-->
</
style
>
</
head
>
<
body
bgcolor
=
"#FFFFFF"
topmargin
=
"6"
leftmargin
=
"6"
>
<
hr
size
=
"1"
noshade>
Log session start time Wed Mar 29 10:17:41 CST 2017<
br
>
<
br
>
<
table
cellspacing
=
"0"
cellpadding
=
"4"
border
=
"1"
bordercolor
=
"#224466"
width
=
"100%"
>
<
tr
>
<
th
>Time</
th
>
<
th
>Thread</
th
>
<
th
>Level</
th
>
<
th
>Category</
th
>
<
th
>Message</
th
>
</
tr
>
<
tr
>
<
td
>0</
td
>
<
td
title
=
"main thread"
>main</
td
>
<
td
title
=
"Level"
>INFO</
td
>
<
td
title
=
"com.open1111.Test category"
>com.open1111.Test</
td
>
<
td
title
=
"Message"
>普通Info信息</
td
>
</
tr
>
<
tr
>
<
td
>16</
td
>
<
td
title
=
"main thread"
>main</
td
>
<
td
title
=
"Level"
><
font
color
=
"#339933"
>DEBUG</
font
></
td
>
<
td
title
=
"com.open1111.Test category"
>com.open1111.Test</
td
>
<
td
title
=
"Message"
>调试debug信息</
td
>
</
tr
>
<
tr
>
<
td
>16</
td
>
<
td
title
=
"main thread"
>main</
td
>
<
td
title
=
"Level"
><
font
color
=
"#993300"
><
strong
>ERROR</
strong
></
font
></
td
>
<
td
title
=
"com.open1111.Test category"
>com.open1111.Test</
td
>
<
td
title
=
"Message"
>报错error信息</
td
>
</
tr
>
<
tr
>
<
td
>16</
td
>
<
td
title
=
"main thread"
>main</
td
>
<
td
title
=
"Level"
><
font
color
=
"#993300"
><
strong
>WARN</
strong
></
font
></
td
>
<
td
title
=
"com.open1111.Test category"
>com.open1111.Test</
td
>
<
td
title
=
"Message"
>警告warn信息</
td
>
</
tr
>
<
tr
>
<
td
>16</
td
>
<
td
title
=
"main thread"
>main</
td
>
<
td
title
=
"Level"
><
font
color
=
"#993300"
><
strong
>FATAL</
strong
></
font
></
td
>
<
td
title
=
"com.open1111.Test category"
>com.open1111.Test</
td
>
<
td
title
=
"Message"
>严重错误fatal信息</
td
>
</
tr
>
<
tr
>
<
td
>16</
td
>
<
td
title
=
"main thread"
>main</
td
>
<
td
title
=
"Level"
><
font
color
=
"#993300"
><
strong
>ERROR</
strong
></
font
></
td
>
<
td
title
=
"com.open1111.Test category"
>com.open1111.Test</
td
>
<
td
title
=
"Message"
>报错信息</
td
>
</
tr
>
<
tr
><
td
bgcolor
=
"#993300"
style
=
"color:White; font-size : xx-small;"
colspan
=
"6"
>java.lang.IllegalArgumentException: 非法参数
<
br
> at com.open1111.Test.main(Test.java:16)
</
td
></
tr
>
|
标准的html table表格格式,显示信息包括 线程 等级 类 报错信息;
PatternLayout是我们以后推荐使用的,很灵活;
有个ConversionPattern属性,灵活配置输出属性:
%m 输出代码中指定的消息;
%M 输出打印该条日志的方法名;
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL;
%r 输出自应用启动到输出该log信息耗费的毫秒数;
%c 输出所属的类目,通常就是所在类的全名;
%t 输出产生该日志事件的线程名;
%n 输出一个回车换行符,Windows平台为"rn”,Unix平台为"n”;
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss,SSS},输出类似:2002-10-18 22:10:28,921;
%l 输出日志事件的发生位置,及在代码中的行数;
SimpleLayout简单布局:
1
2
3
|
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.SimpleLayout
|
运行输出:
INFO - 普通Info信息
DEBUG - 调试debug信息
ERROR - 报错error信息
WARN - 警告warn信息
FATAL - 严重错误fatal信息
ERROR - 报错信息
java.lang.IllegalArgumentException: 非法参数
at com.open1111.Test.main(Test.java:16)
只有级别和信息,确实简单
TTCCLayout我们测试下:
1
2
3
|
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.TTCCLayout
|
运行输出:
[main] INFO com.open1111.Test - 普通Info信息
[main] DEBUG com.open1111.Test - 调试debug信息
[main] ERROR com.open1111.Test - 报错error信息
[main] WARN com.open1111.Test - 警告warn信息
[main] FATAL com.open1111.Test - 严重错误fatal信息
[main] ERROR com.open1111.Test - 报错信息
java.lang.IllegalArgumentException: 非法参数
at com.open1111.Test.main(Test.java:16)
我们实际开发使用的是灵活的PatternLayout 其他的了解即可;