小東昇職記
阿新 • • 發佈:2018-12-16
1.首先在POM.XML下引入slf4j-log4j依賴包
2.配置log4j-properties檔案
# rootLogger引數分別為:根Logger級別,輸出器stdout,輸出器log log4j.rootLogger = info,stdout,log # 輸出資訊到控制檯 log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d [%-5p] %l %rms: %m%n # 輸出DEBUG級別以上的日誌到D://logs/debug.log log4j.appender.log = org.apache.log4j.DailyRollingFileAppender log4j.appender.log.DatePattern = '.'yyyy-MM-dd log4j.appender.log.File = D://debug.log log4j.appender.log.Encoding = UTF-8 #log4j.appender.log.Threshold = INFO log4j.appender.log.layout = org.apache.log4j.PatternLayout log4j.appender.log.layout.ConversionPattern = %d [%-5p] (%c.%t): %m%n
3.編寫一個測試
package com.zhiyou100.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 通常輸出日誌開銷非常大,從上述結果可見, * SLF4J通過{}作為佔位符的方式輸出字串, * 相比字串拼接的方式,效率有顯著的提升。 * @author Administrator * */ public class LoggerTest { private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class); public static void main(String[] args) { logger.info("Current Time: {}", System.currentTimeMillis()); logger.info("Current Time: " + System.currentTimeMillis()); logger.info("Current Time: {}", System.currentTimeMillis()); logger.trace("trace log"); logger.warn("warn log"); logger.debug("debug log"); logger.info("info log"); logger.error("error log"); } }