Java 每次使用 log4j 运行一个日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19132433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One logfile per run with log4j
提问by lilalinux
How do you configure log4j.properties to have exactly one logfile per run of an app. I've read that you should use a timestamp in the filename but that will create many files per run as time goes by.
您如何将 log4j.properties 配置为每次运行应用程序时只有一个日志文件。我读过您应该在文件名中使用时间戳,但是随着时间的推移,每次运行都会创建许多文件。
I tried DailyRollingFileAppender and RollingFileAppender but can't find a way to configure exctly one log per run. The log should not be broken into multiple logs and it shouldn't be truncated and files of old runs should be preserved.
我尝试了 DailyRollingFileAppender 和 RollingFileAppender 但找不到一种方法来配置每次运行只配置一个日志。日志不应分成多个日志,不应截断,应保留旧运行的文件。
Each class has a static org.slf4j.Logger for it's own class name:
每个类都有一个静态的 org.slf4j.Logger 作为它自己的类名:
private static final Logger log = LoggerFactory.getLogger(Foo.class);
This is my current log4j.properties
这是我目前的 log4j.properties
log4j.rootLogger=error, RootAppender, RootErrorAppender
#log4j.logger.com.example=info, qspaBackendAppender, stderr
log4j.logger.com.example=info, qspaBackendAppender
log4j.additivity.com.example=true
#log4j.logger.com.example.util=trace, qspaBackendAppender, stderr
#log4j.additivity.com.example.util=true
log4j.appender.qspaBackendAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.qspaBackendAppender.file=logs/qspaBackend.log
log4j.appender.qspaBackendAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.qspaBackendAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target=System.err
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=%-5p %c{1}:%L - %m%n
log4j.appender.RootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootAppender.file=logs/root.log
log4j.appender.RootAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootErrorAppender.file=logs/rootError.log
log4j.appender.RootErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootErrorAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender.threshold=error
采纳答案by Udy
Combine the answer of Udo Klimaschewskiand the answer from this questionin order to get the desired result.
结合Udo Klimaschewski的答案和这个问题的答案,以获得所需的结果。
- add the property append = false
- add a current.time system property and use it in the file name
- 添加属性 append = false
- 添加 current.time 系统属性并在文件名中使用它
回答by Tobia Zambon
I had troubles retrieving which Udo Klimaschewski's answer Udy was referring so I put here my solution. log4j.properties:
我无法检索 Udo Klimaschewski 的回答 Udy 指的是哪个,所以我把我的解决方案放在这里。log4j.properties:
# Root logger option
log4j.rootLogger=INFO, fileout
# Direct log messages to file
log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.File=/logs/myapp_${current.date}.log
log4j.appender.fileout.ImmediateFlush=true
log4j.appender.fileout.Threshold=debug
log4j.appender.fileout.Append=false
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=%5p | %d | %m%n
Then put in the main class this block:
然后在主类中放入这个块:
public class Starter {
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhmmss");
System.setProperty("current.date", dateFormat.format(new Date()));
}
回答by Vadim Zin4uk
To start a new log file on app startup you may want to use OnStartupTriggeringPolicy
要在应用程序启动时启动新的日志文件,您可能需要使用 OnStartupTriggeringPolicy
Example of XML config:
XML 配置示例:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">C:/Logs/</Property>
</Properties>
<Appenders>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<RollingFile name="RollingFile" fileName="${log-path}/app.log"
filePattern="${log-path}/app-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{dd/MM/yyyy HH:mm:ss} [%-5p/%t]: %C{1}(%L): %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
<OnStartupTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
<appender-ref ref="console" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Works well for me
对我来说效果很好