4

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?

user1897151
  • 493
  • 2
  • 9
  • 28

2 Answers2

3

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.

Anubhab
  • 1,736
  • 2
  • 18
  • 28
  • awesome, thanks this works well..now i guess for the debugging part is not working from what Biswajit says – user1897151 Apr 03 '13 at 07:23
  • @user1897151 See the log levels are cascading kind of thing. If you print `debug` statements then you cannot stop it from printing `info`/`error` statements..but wwhat you can do is you can specify logging level for each class in your `log4j.properties` file itself.. – Anubhab Apr 03 '13 at 07:26
  • @user1897151 i can add that to my answer – Anubhab Apr 03 '13 at 07:30
  • thanks again, i will try it out..but 1 question about my getLogger() method. should i pass in getLogger("info"); ? – user1897151 Apr 03 '13 at 07:37
  • You should always pass the `.class` of the Java Class to the `getLogger()` where you want to log. – Anubhab Apr 03 '13 at 07:40
1

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.

Biswajit
  • 2,434
  • 2
  • 28
  • 35
  • erm for that param name..i dont have xml file setup since i am using properties...can guide me where to get the xml file? or do i require it as i thought for standalone application a properties file should be sufficient – user1897151 Apr 03 '13 at 07:19
  • check the link http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/ – Biswajit Apr 03 '13 at 07:22