创建项目:
使用的是log4j-1.2.17.jar;
log4j.properties:
log4j.rootLogger=DEBUG, Console ,File
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#File
log4j.appender.File = org.apache.log4j.FileAppender
log4j.appender.File.File = C://log2.log
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
测试代码:
Test.java:
package com.cy; import org.apache.log4j.Logger; public class Test { private static Logger logger = Logger.getLogger(Test.class); public static void main(String[] args) { logger.info("普通Info信息"); logger.debug("调试debug信息"); logger.error("报错error信息"); logger.warn("警告warn信息"); logger.fatal("严重错误fatal信息"); logger.error("报错信息", new IllegalArgumentException("非法参数")); } }
console打印:
c:/log2.log也记录了信息: