开发一个demo时想将日志输出到最终打包的jar所在目录,从网上学习实验整理之后的配置如下,
log4j.properties
log4j.rootLogger = INFO,console,logFile log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Threshold=INFO log4j.appender.console.ImmediateFlush=true log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m%n log4j.appender.logFile=org.apache.log4j.FileAppender log4j.appender.logFile.ImmediateFlush=true log4j.appender.logFile.Append=false log4j.appender.logFile.Encoding=UTF-8 log4j.appender.logFile.File=jarDemo.log log4j.appender.logFile.layout=org.apache.log4j.PatternLayout log4j.appender.logFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
Main.java
private static Logger logger = LoggerFactory.getLogger(Main.class); static { String path = new File("").getAbsolutePath(); FileAppender appender = (FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("logFile"); appender.setFile(path + File.separator + "jarDemo.log"); }
最终打包成jar执行时,将会在jar文件所在目录生产一个jarDemo.log的日志文件,如果时用idea运行,这个日志文件会在源码所在项目的根目录。
原文地址:https://www.cnblogs.com/01x2v3/p/9042431.html
时间: 2024-11-03 01:29:42