Log4j常用配置及使用
1 添加log4j依赖
maven的pom.xml文件中添加log4j依赖
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>provided</scope> </depenency>
2 使用代码配置并调用log
2.1 代码配置LogConfiguration.java
import java.util.Properties; import org.apache.log4j.PropertyConfigurator; public class LogConfiguration { public static void initLog(String logFilePath){ //声明日志文件存储路径以及文件名、格式 Properties prop = new Properties(); //配置日志输出的格式 prop.setProperty("log4j.rootLogger","info, toConsole, toFile"); prop.setProperty("log4j.appender.file.encoding","UTF-8" ); prop.setProperty("log4j.appender.toConsole","org.apache.log4j.ConsoleAppender"); prop.setProperty("log4j.appender.toConsole.Target","System.out"); prop.setProperty("log4j.appender.toConsole.layout","org.apache.log4j.PatternLayout "); prop.setProperty("log4j.appender.toConsole.layout.ConversionPattern","[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n"); prop.setProperty("log4j.appender.toFile.file", logFilePath);
//每天产生一个日志文件 prop.setProperty("log4j.appender.toFile", "org.apache.log4j.DailyRollingFileAppender"); //服务器启动日志是追加,false:服务器启动后会生成日志文件把老的覆盖掉 prop.setProperty("log4j.appender.toFile.Append","true"); prop.setProperty("log4j.appender.toFile.Threshold", "info"); prop.setProperty("log4j.appender.toFile.layout", "org.apache.log4j.PatternLayout"); prop.setProperty("log4j.appender.toFile.layout.ConversionPattern", "[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n");
//[2017-03-31 14:10:44] [ERROR] HttpResponseAnalyze:31 - Not equal //prop.setProperty("log4j.appender.toFile.layout.ConversionPattern", "[%d{yyyy-MM-dd HH:mm:ss}] [%p] %c{1}:%L - %m%n");
//设置每天生成一个文件名后添加的名称,备份名称:年月日.log
prop.setProperty("log4j.appender.toFile.DatePattern","'.'yyyy-MM-dd'.log'");
//使配置生效 PropertyConfigurator.configure(prop); } }
2.2 调用
import org.apache.log4j.Logger; public class TestLog { static Logger logger = Logger.getLogger(TestLog.class.getName()); public static void main(String[] args) { LogConfiguration.initLog("D:\\log\\result.log"); logger.info("log info"); logger.warn("log warn"); logger.error("log error"); try { Thread.sleep(2000); } catch(Exception ex) {} logger.info("log1 info"); logger.warn("log1 warn"); logger.error("log1 error"); } }
2.3 结果
输出到Ecliplse的Console界面和文件D:\log\result.log中,内容如下:
[2016-12-14 16:03:34] [INFO] log info [2016-12-14 16:03:34] [WARN] log warn [2016-12-14 16:03:34] [ERROR] log error [2016-12-14 16:03:36] [INFO] log1 info [2016-12-14 16:03:36] [WARN] log1 warn [2016-12-14 16:03:36] [ERROR] log1 error
另外,由于配置了每天生成一个备份,系统时间切一天后日后,再运行,原先的result.log会变成result.log.2016-12-13.log,新的日志会重新放到新建的日志文件result.log
3 使用Properties文件配置并调用log
3.1 文件配置log4j.properties
log4j.rootLogger=INFO, toConsole, toFile log4j.appender.file.encoding=UTF-8 log4j.appender.toConsole=org.apache.log4j.ConsoleAppender log4j.appender.toConsole.Target=System.out log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout log4j.appender.toConsole.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n log4j.appender.toFile.file=D:\\log\\result.log #Daily one log file log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.toFile.Append=true log4j.appender.toFile.Threshold=info log4j.appender.toFile.layout=org.apache.log4j.PatternLayout log4j.appender.toFile.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n log4j.appender.toFile.DatePattern='.'yyyy-MM-dd'.log'