[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-11-06
5
Medium Priority
?
432 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 600 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 840 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

829 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