user1897151
user1897151

Reputation: 503

log4j, want to show only debug(not info) in console and clear log files when app starts

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.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.

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?

Upvotes: 4

Views: 14131

Answers (2)

Anubhab
Anubhab

Reputation: 1786

Add the below line in your log4j.properties to make it fresh everytime app starts

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

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.

Upvotes: 3

Biswajit
Biswajit

Reputation: 2506

set loglevel in your code. i.e.

  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.

Upvotes: 1

Related Questions