Log4j, you want to show only debugging (not information) in the console and clear log files when the application starts

I am new to this log4j and managed to install on eclipse and run it. I understand the priority chain at the levels, and now this is my configuration of the properties file:

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 log4j to clear my log file every time the application starts? I am not sure how to do this.

2) Well, from the configuration, I configure my console to print debugging, but what I really wanted it to print was a pure debug msg, not an INFO message. Is there any way to control this? As soon as debugging and printing errors, if the threshold is set for debugging?

+4
source share
2 answers

Add the line below to log4j.properties so that it starts every time the application starts

 log4j.appender.rollingFile.Append=false 

You can also add a logging level to your own package. Suppose you have the package foo.bar.MyPack .
You want to specify the logging level for this package as info , then you should add the line below in log4j.properties

 log4j.logger.foo.bar.MyPack=info 

This way you can control which package should be in info or which should be in debug , etc.

+3
source

set the loglevel level in your code. i.e.

  private static org.apache.log4j.Logger log = Logger .getLogger(LogClass.class); log.setLevel(Level.Debug); 

only a debug message will be displayed

 <param name="Append" value="false" /> 

If you set append to false, the base log file will be “started fresh” when the application restarts.

+1
source

All Articles