Solved

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

Posted on 2006-11-06
5
382 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 91
how to add new optional parameter to JSP 1 41
How to convert from xls to xlsx using java 7 39
String array comparison 4 33
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

770 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