log4j-slf4j

rodertW發表於2018-12-30

#rootLogger引數分別為: 根Logger級別, 輸出器stdout, 輸出器log
log4j.rootLogger = info,stdout,log

# \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u53F0
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

 測試一下

public class Log4jTest {
	// public static Logger log = Logger.getLogger(Log4jTest.class);
	private static final Logger logger = LoggerFactory
			.getLogger(Log4jTest.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");

		logger.info("current time :{}", System.currentTimeMillis());
		logger.error("error");
		logger.info("這是我要輸出的日誌");
		System.out.println("列印結果");

		// 以下是log4j用法
		// log.debug("debug資訊");
		// log.info("info資訊");
		// log.warn("this is warm");
	}
}