LOG4J: I'm new to LOG4J.. need some answers..

I'm new to log4j .. so I have few questions which would be nice to clear out before I start using it. (I will split the points between these 3 questions)

1. I would like to log everything (DEBUG,INFO,WARN,ERROR,FATAL) to the console, but send only WARN and higher (with different data like code line numer.. etc) to a file. How can I do that without creating two Logger objects in the code and having two log lines for each error in the code? probably through inheriting somehow?

2. I see I can use configureAndWatch(String configFilename, long delay_in_milliseconds) to fire up a thread that checks for changes in the configuration file. What happens if I would call the function more than one time. Would many watch threads be spawned or is the logger smart enough to only change the delay_in_milliseconds of the current watch thread?

3. I see in some doc's "WARNING: Using %d{dd MMM yyyy HH:mm:ss,SSS} "is slow" and I should use %d{ISO8601} instead. But how slow actually is it (using custom date format which uses SimpleDateFormat) are we talking about nano, micro, milliseconds..?
nediam1234Asked:
Who is Participating?
 
girionisConnect With a Mentor Commented:
1. Try to use

log4j.appender.consoleApp.Threshold=DEBUG
log4j.appender.fileApp.Threshold=WARN

2. You would need to know which thread is spawned and then stop it from running. But why you would want to call this method more than one time?

3. They say its significantly better than this pattern %d{dd MMM yyyy HH:mm:ss,SSS} but not sure how the "significantly better" is measured.
0
 
objectsConnect With a Mentor Commented:
1. u define two appenders
2. it would create a thread for each call
0
 
nediam1234Author Commented:
thanks for your answers...  little clarification ..

>1. u define two appenders
ok, If I have two appenders, how can I make each of them log for different level?

lets say I have the config file like this:

log4j.rootLogger=DEBUG, consoleApp, fileApp

log4j.appender.consoleApp=org.apache.log4j.ConsoleAppender
log4j.appender.consoleApp.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleApp.layout.ConversionPattern=%r [%p] %m%n

log4j.appender.fileApp=org.apache.log4j.FileAppender
log4j.appender.fileApp.File=log4j_test.log
log4j.appender.fileApp.layout=org.apache.log4j.PatternLayout
log4j.appender.fileApp.layout.ConversionPattern=%r %d{dd.mm.yyyy HH:mm:ss,SSS} [%p] %l: %m%n

And I would like the level to be DEBUG for the appender consoleApp and WARN level for the fileApp ? I don't find the config for that.

>2. it would create a thread for each call
ok.. is there then a way to remove a watch thread?  
0
 
nediam1234Author Commented:
Girionis,
1. Thank you.. it was the Threshold I was missing.. works correctly now :)

2. You would need to know which thread is spawned and then stop it from running. But why you would want to call this method more than one time?
ok.. so just kill -9 :)  .. well..  I was just thinking about if I would change the refresh time.... if I wanted it to refresh often doing "tests".
well.. maybe there is a function to force log4j to read the config file again? That would be enough.

3. They say its significantly better than this pattern %d{dd MMM yyyy HH:mm:ss,SSS} but not sure how the "significantly better" is measured.
yes, that's why I'm afraid to use it ... but maybe "significantly better" is very little... :)   This part I should just do test with my self.. so I just cancel this part of my question...

I got the answers I asked for... so I will close the question in about an hour if I have not got reply to the "extra" question. "is there a function to force log4j to read the config file again?"

thanks :)
0
 
girionisCommented:
2. Not sure if kill -9 is the same, I think the "kill" command kills the entire application running and not just a single thread. So if you would do a kill to a java application it would kill *all* the threads spawned. I guess that configureAndWatch stays there and watches for all changes during the object's lifetime. The best thing you could do is to make sure that the class with the configureAndWatch is only loaded once, eg at server's start up or something similar. You probably want to put it in a static initilization block like

static
    {
        DOMConfigurator.configureAndWatch(<path to proerty file>);
    }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.