最近在学习JBoss下配置Log4j,JBoss的各个版本的目录、JBoss和Log4j集成的文件名每次都有变化,在JBoss5.x下配置了log4j.xml文件,但是配置没有生效。如果你在项目中没有指定使用的log4j的配置文件,最好使用JBoss自带的配置文件,文件名为jboss-log4j.xml。该文件在Jboss容器的目录为:${JBOSS_HOME}\server\default\conf下。下面就是基于log4j默认配置的一些基本修改和注释:
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
86
87
88
89
90
91
92
|
< span style = "font-size:12px;" ><? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> < log4j:configuration xmlns:log4j = "http://jakarta.apache.org/log4j/" debug = "false" > < appender name = "FILE" class = "org.jboss.logging.appender.DailyRollingFileAppender" > < errorHandler class = "org.jboss.logging.util.OnlyOnceErrorHandler" /> < param name = "File" value = "${jboss.server.log.dir}/server.log" /> < param name = "Append" value = "true" /> <!-- Rollover at midnight each day --> < param name = "DatePattern" value = "'.'yyyy-MM-dd" /> <!-- Rollover at the top of each hour <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/> --> < layout class = "org.apache.log4j.PatternLayout" > <!-- The default pattern: Date Priority [Category] Message\n --> < param name = "ConversionPattern" value = "%d %-5p [%c] %m%n" /> <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/> --> </ layout > </ appender > < appender name = "ErrorFile" class = "org.jboss.logging.appender.DailyRollingFileAppender" > < errorHandler class = "org.jboss.logging.util.OnlyOnceErrorHandler" /> < param name = "File" value = "${jboss.server.log.dir}/error.log" /> < param name = "Threshold" value = "ERROR" /> < param name = "Append" value = "true" /> <!-- Rollover at midnight each day --> < param name = "DatePattern" value = "'.'yyyy-MM-dd" /> < layout class = "org.apache.log4j.PatternLayout" > <!-- The default pattern: Date Priority [Category] Message\n --> < param name = "ConversionPattern" value = "%d %-5p [%c] %m%n" /> <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/> --> </ layout > </ appender > < appender name = "CONSOLE" class = "org.apache.log4j.ConsoleAppender" > < errorHandler class = "org.jboss.logging.util.OnlyOnceErrorHandler" /> < param name = "Target" value = "System.out" /> < param name = "Threshold" value = "INFO" /> < layout class = "org.apache.log4j.PatternLayout" > <!-- The default pattern: Date Priority [Category] Message\n --> < param name = "ConversionPattern" value = "%d{ABSOLUTE} %-5p [%c{1}] %m%n" /> </ layout > </ appender > <!-- ================ --> <!-- Limit categories --> <!-- ================ --> < category name = "org.hibernate" > < priority value = "WARN" /> </ category > < category name = "org.hibernate.engine.loading.LoadContexts" > < priority value = "ERROR" /> </ category > < category name = "org.jboss" > < priority value = "INFO" /> </ category > <!-- Limit the org.apache category to INFO as its DEBUG is verbose --> < category name = "org.apache" > < priority value = "WARN" /> </ category > <!-- Limit the jacorb category to WARN as its INFO is verbose --> < category name = "jacorb" > < priority value = "WARN" /> </ category > <!-- Limit the org.jgroups category to WARN as its INFO is verbose --> < category name = "org.jgroups" > < priority value = "WARN" /> </ category > <!-- Limit the org.quartz category to INFO as its DEBUG is verbose --> < category name = "org.quartz" > < priority value = "INFO" /> </ category > <!-- Limit the JSR77 categories --> < category name = "org.jboss.management" > < priority value = "INFO" /> </ category > <!-- Limit the org.jboss.serial (jboss-serialization) to INFO as its DEBUG is verbose --> < category name = "org.jboss.serial" > < priority value = "INFO" /> </ category > < category name = "com.liang" > < priority value = "DEBUG" /> </ category > <!-- ======================= --> <!-- Setup the Root category --> <!-- ======================= --> < root > < appender-ref ref = "CONSOLE" /> < appender-ref ref = "FILE" /> < appender-ref ref = "ErrorFile" /> </ root > </ log4j:configuration ></ span > |
1. Log4j的几种输出方式
org.apache.log4j.ConsoleAppender(控制台)
org.apache.log4j.FileAppender(文件)
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定位置)
2. 日志记录的优先级
优先级从高到低:OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL。
log4j建议使用FATAL、ERROR、WARN、INFO、DEBUG。
你可以在<param name="Threshold" value="ERROR" />来配置level,Threshold是一个全局的过滤器,低于其设置的level将不会显示出来。
3. 当重启Jboss服务之后是否保存之前的日志
<param name="Append" value="true" />,设置为true后,当Jboos重启后当前的log不会消失,即在原有日志上增加新日志。
4. 日志输出格式
%c 输出所属类的全名,可在修改为 %c{num} ,num表示输出的范围
如:"org.apache.elathen.ClassName",%c{2}将输出elathen.ClassName
%d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}
%l 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数
%n 换行符
%m 输出代码指定信息,如info("message"),输出message
%p 输出优先级,即 FATAL ,ERROR 等
%r 输出从启动到显示该log信息所耗费的毫秒数
%t 输出产生该日志事件的线程名
5. 关于category的配置
category指定了以什么开头的包下的类输出的日志的级别。例如:
1
2
3
4
|
< category name = "com.liang" > < priority value = "DEBUG" /> < appender-ref ref = 'FILE' /> </ category > |
从上面可以看出,项目中以com.liang包开头的所有类中,凡是DEBUG级别的日志可以输出到FILE中。
注意:定义了一个appender-ref=FILE的引用,但是你如果也在root下配置了appender-ref=FILE的引用,会导致输出到FILE的日志会重复。本人碰到过类似的问题。
总结
以上就是本文关于JBoss5.x下配置Log4j方法介绍的全部内容,希望对大家有所帮助。
原文链接:http://blog.csdn.net/jiuqiyuliang/article/details/42134199