java log4j,只想在控制台中显示调试(不是信息)并在应用程序启动时清除日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15780745/
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
log4j, want to show only debug(not info) in console and clear log files when app starts
提问by user1897151
I am new to this log4j and managed to setup on eclipse and get it running. I understand the chain of priority in the levels and right now this is my properties file config:
我是这个 log4j 的新手,并设法在 eclipse 上进行设置并使其运行。我了解级别中的优先级链,现在这是我的属性文件配置:
log4j.rootLogger = DEBUG, rollingFile, console
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.Threshold=INFO
log4j.appender.rollingFile.File=logs/logFile.log
log4j.appender.rollingFile.MaxFileSize=1MB
log4j.appender.rollingFile.MaxBackupIndex=5
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c: %m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
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,SSS} %-5p - %m%n
I have 2 questions for this log4j
1) Is it possible for the log4j to clear my log file each time I launch the application? I am not sure how to do this.
我对此 log4j 有 2 个问题
1) 每次启动应用程序时,log4j 是否可以清除我的日志文件?我不知道该怎么做。
2) Well from the config I setup my console to print debug but what i really wanted it to print is pure debug msg instead of INFO message as well. Is there anyway to control this? Like only print debug and errors if the threshold is set to debug?
2)从配置中我设置了我的控制台来打印调试,但我真正想要它打印的是纯调试消息而不是 INFO 消息。有没有办法控制这个?如果阈值设置为调试,则只打印调试和错误?
采纳答案by Anubhab
Add the below line in your log4j.properties
to make it fresh everytime app starts
在您的log4j.properties
应用程序中添加以下行以使其在每次应用程序启动时都是新鲜的
log4j.appender.rollingFile.Append=false
You can add logging level to your custom package also like this.
Suppose you have a package foo.bar.MyPack
.
You want to specify logging level for this package as info
then you have to add the below line in your log4j.properties
您也可以像这样将日志记录级别添加到您的自定义包中。
假设你有一个包foo.bar.MyPack
。
您想为此包指定日志记录级别,info
然后您必须在您的log4j.properties
log4j.logger.foo.bar.MyPack=info
In this way you can controll which package should be in info
or which should be in debug
etc.
通过这种方式,您可以控制应该在哪个包中info
或应该在哪个包中等debug
。
回答by Biswajit
set loglevel in your code. i.e.
在您的代码中设置日志级别。IE
private static org.apache.log4j.Logger log = Logger
.getLogger(LogClass.class);
log.setLevel(Level.Debug);
it will show only debug message
它只会显示调试消息
<param name="Append" value="false" />
If you set the append parameter to false, the base log file will be "started fresh" when the application restarts.
如果将 append 参数设置为 false,则当应用程序重新启动时,基本日志文件将“重新启动”。