Solved

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

Posted on 2006-11-06
5
367 Views
Last Modified: 2011-09-20
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..?
0
Comment
Question by:nediam1234
  • 2
  • 2
5 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 17880050
1. u define two appenders
2. it would create a thread for each call
0
 

Author Comment

by:nediam1234
ID: 17880125
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
 
LVL 35

Accepted Solution

by:
girionis earned 210 total points
ID: 17880785
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
 

Author Comment

by:nediam1234
ID: 17881106
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
 
LVL 35

Expert Comment

by:girionis
ID: 17881193
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now