1. 自动加载,在项目中把log4j.xml放在src目录下,编译后自动加载
2. 主动加载 DOMConfigurator.configure(path + "log4j.xml");
3. web.xml中配置
<context-parm>
<…….
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener></listener-class>
<listener>
1: <?xml version="1.0" encoding="UTF-8"?>
2: <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3: <log4j:configuration debug="true">
4:
5: <!-- Log4j提供的appender有以下几种: -->
6: <!-- org.apache.log4j.ConsoleAppender(控制台), -->
7: <!-- org.apache.log4j.FileAppender(文件), -->
8: <!-- org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件), -->
9: <!-- org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件), -->
10: <!-- org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方) -->
11:
12: <appender name="log1" class="org.apache.log4j.DailyRollingFileAppender">
13: <param name="File" value="logs/log1.log" />
14: <param name="Append" value="true" />
15: <!-- 如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。 -->
16: <layout class="org.apache.log4j.PatternLayout">
17: <param name="ConversionPattern" value="%-5p %d{MM/dd HH:mm} %c %m%n" />
18: </layout>
19: </appender>
20:
21:
22: <appender name="log2" class="org.apache.log4j.DailyRollingFileAppender">
23: <param name="File" value="logs/log2.log" />
24: <param name="Append" value="true" />
25: <!-- 如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。 -->
26: <layout class="org.apache.log4j.PatternLayout">
27: <param name="ConversionPattern" value="%-5p %d{MM/dd HH:mm} %C %m%n" />
28: </layout>
29: </appender>
30:
31: <appender name="log3" class="org.apache.log4j.DailyRollingFileAppender">
32: <param name="File" value="logs/log3.log" />
33: <param name="Append" value="true" />
34: <param name="encoding" value="UTF-8" />
35: <layout class="org.apache.log4j.PatternLayout">
36: <param name="ConversionPattern" value="%d{YYYY-MM-dd HH:mm:ss} %-2p [%-5C] %m%n" />
37: </layout>
38: </appender>
39:
40: <!--可以将各个包中的类日志输出指定到到不同的日志文件中 -->
41: <category name="com.zero.log1" additivity="false">
42: <priority value="debug" />
43: <appender-ref ref="log1" />
44: </category>
45:
46: <!--zero的日志记录到log3日志文件中 , Logger.getLogger("zero"); -->
47: <category name="zero" additivity="false">
48: <priority value="debug" />
49: <appender-ref ref="log3" />
50: </category>
51:
52: <!--默认输出到的日志文件 -->
53: <root>
54: <priority value="debug" />
55: <appender-ref ref="log2" />
56: </root>
57:
58: </log4j:configuration>
1: package com.zero.log;
2:
3: import org.apache.log4j.Logger;
4:
5: public class LogUtils {
6: private static final Logger logger = Logger.getLogger("zero");
7:
8: public static void log(String logStr, String logType) {
9: Logger myLogger = Logger.getLogger(logType);
10: myLogger.info(logStr);
11: logger.info("*LogUtils*");
12: }
13: }
1: package com.zero.log1;
2:
3: import org.apache.log4j.Logger;
4:
5: public class Log1 {
6: private Logger logger = Logger.getLogger(Log1.class);
7: public Log1(){
8: logger.info("Log1");
9: }
10: }
1: package com.zero.log2;
2:
3: import org.apache.log4j.Logger;
4:
5:
6: public class Log2 {
7: private Logger logger = Logger.getLogger(Log2.class);
8: public Log2(){
9: logger.debug("Log3");
10: }
11: }
1: package com.zero.log3;
2:
3: import org.apache.log4j.Logger;
4:
5: import com.zero.log.LogUtils;
6:
7:
8: public class Log3 {
9: private Logger logger = Logger.getLogger(Log3.class);
10: public Log3(){
11: LogUtils.log("Log3", "zero");;
12: }
13: }
1: package com.zero.test;
2:
3: import java.io.File;
4:
5: import org.apache.log4j.Logger;
6: import org.apache.log4j.PropertyConfigurator;
7: import org.apache.log4j.xml.DOMConfigurator;
8: import org.w3c.dom.DOMConfiguration;
9:
10: import com.zero.log1.Log1;
11: import com.zero.log2.Log2;
12: import com.zero.log3.Log3;
13:
14: public class Test {
15: static Logger logger = Logger.getLogger(Test.class);
16: public static void main(String[] args) {
17: String path = new File("").getAbsolutePath() + File.separator
18: + "config" + File.separator;
19: DOMConfigurator.configure(path + "log4j.xml");
20: logger.info("Test");
21: new Log1();
22: new Log3();
23: new Log2();
24: }
25:
26: }