slf4j+log4j與slf4j+logback的簡單測試示例
阿新 • • 發佈:2019-02-14
首先是整個目錄結構:
1.slf4j+log4j
需求:讓com.zero.log4j.log1包下的類的日誌輸出到log1.log中,也可以指定某些日誌輸出到指定的日誌檔案log3.log,其它未指明的預設輸出到log2.log2。
log4j.xml配置日誌:
1: <?xml version="1.0" encoding="UTF-8"?>
2: <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3: <log4j:configuration debug="true">
4:
5: <appender name="log1" class="org.apache.log4j.DailyRollingFileAppender">
6: <param name="File" value="logs/log1.log" />
7: <param name="Append" value="true" />
8: <!-- 如果是 true,日誌被追加到檔案結尾,如果是 false,清空現存檔案,預設是true。 -->
9: <layout class ="org.apache.log4j.PatternLayout">
10: <param name="ConversionPattern" value="%-5p %d{MM/dd HH:mm} %c %m%n" />
11: </layout>
12: </appender>
13:
14: <appender name="log2" class="org.apache.log4j.DailyRollingFileAppender">
15: <param name="File" value="logs/log2.log" />
16: <param name="Append" value="true" />
17: <!-- 如果是 true,日誌被追加到檔案結尾,如果是 false,清空現存檔案,預設是true。 -->
18: <layout class="org.apache.log4j.PatternLayout">
19: <param name="ConversionPattern" value="%-5p %d{MM/dd HH:mm} %C %m%n" />
20: </layout>
21: </appender>
22:
23: <appender name="log3" class="org.apache.log4j.DailyRollingFileAppender">
24: <param name="File" value="logs/log3.log" />
25: <param name="Append" value="true" />
26: <param name="encoding" value="UTF-8" />
27: <layout class="org.apache.log4j.PatternLayout">
28: <param name="ConversionPattern" value="%d{YYYY-MM-dd HH:mm:ss} %-2p [%-5C] %m%n" />
29: </layout>
30: </appender>
31:
32: <!--可以將com.zero.log4j.log1包中的類日誌輸出指定到到log1.log日誌檔案中 -->
33: <category name="com.zero.log4j.log1" additivity="false">
34: <priority value="debug" />
35: <appender-ref ref="log1" />
36: </category>
37:
38: <!--zero的日誌記錄到log3日誌檔案中 , Logger.getLogger("zero"); -->
39: <category name="zero" additivity="false">
40: <priority value="debug" />
41: <appender-ref ref="log3" />
42: </category>
43:
44: <!--預設輸出到的log2日誌檔案 -->
45: <root>
46: <priority value="debug" />
47: <appender-ref ref="log2" />
48: </root>
49:
50: </log4j:configuration>
java程式碼:
1: package com.zero.log4j.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: myLogger.warn(logStr);
12: logger.info("LogUtils, 我想記錄在log3.log");
13: }
14: }
1: package com.zero.log4j.log1;
2:
3: import org.apache.log4j.Logger;
4:
5: public class Log1 {
6: private Logger logger = Logger.getLogger(Log1.class);
7:
8: public Log1() {
9: logger.debug("Log1,我想記錄在log1.log");
10: logger.info("Log1,我想記錄在log1.log");
11: logger.warn("Log1,我想記錄在log1.log");
12: logger.error("Log1,我想記錄在log1.log");
13: }
14: }
1: package com.zero.log4j.log2;
2:
3: import org.apache.log4j.Logger;
4:
5: public class Log2 {
6: private Logger logger = Logger.getLogger(Log2.class);
7: public Log2(){
8: logger.debug("Log2, 我預設記錄到log2.log");
9: logger.info("Log2, 我預設記錄到log2.log");
10: logger.warn("Log2, 我預設記錄到log2.log");
11: logger.error("Log2, 我預設記錄到log2.log");
12: }
13: }
1: package com.zero.log4j.log3;
2:
3: import org.apache.log4j.Logger;
4:
5: import com.zero.log4j.log.LogUtils;
6:
7: public class Log3 {
8: private Logger logger = Logger.getLogger(Log3.class);
9: public Log3(){
10: logger.debug("Log3, 我預設記錄到log2.log");
11: logger.info("Log3, 我預設記錄到log2.log");
12: logger.warn("Log3, 我預設記錄到log2.log");
13: logger.error("Log3, 我預設記錄到log2.log");
14: LogUtils.log("Log3,"+" 我要記錄到log3.log", "zero");;
15: }
16: }
1: package com.zero.test;
2:
3: import java.io.File;
4:
5: import org.apache.log4j.xml.DOMConfigurator;
6: import org.slf4j.Logger;
7: import org.slf4j.LoggerFactory;
8:
9: import com.zero.log4j.log1.Log1;
10: import com.zero.log4j.log2.Log2;
11: import com.zero.log4j.log3.Log3;
12: