-
using Log4j (4 messages)
- Posted by: Katie Huber
- Posted on: June 10 2004 16:29 EDT
Hi, I'm new to log4j. I've been asked to go through an project already underway (100+ java files) and clean up the code and change System.out.println to logging statements using log4j. I've read about log4j, and can implement it on an individual class basis (e.g. create a static logger for each class, set level for each class), but I was wondering if there is a way to set the level for all loggers in a package, or the whole project? After reading about logger inheritence and configuration files, etc., it seems like it should be, but I'm not sure how to implement it. I've examined several examples, but the examples I found were for individual classes, not packages. I'm just learning log4j so any help, suggestions, or references would be very much appreciated. Thank you.Threaded Messages (4)
- using Log4j by Paul Strack on June 10 2004 20:06 EDT
- static vs non-static loggers by Katie Huber on June 11 2004 13:42 EDT
-
static vs non-static loggers by Paul Strack on June 11 2004 03:15 EDT
- RE: by Katie Huber on June 11 2004 03:55 EDT
-
static vs non-static loggers by Paul Strack on June 11 2004 03:15 EDT
- static vs non-static loggers by Katie Huber on June 11 2004 13:42 EDT
-
using Log4j[ Go to top ]
- Posted by: Paul Strack
- Posted on: June 10 2004 20:06 EDT
- in response to Katie Huber
A common pattern for Log4J is to organize your logging categories by Java class. In fact, the Logger has a factory method supporting this:
// In the MyClass class:
Logger logger = Logger.getLogger(MyClass.class);
// For non-static loggers:
Logger logger = Logger.getLogger(this.getClass());
If you consistently follow this approach, it is pretty easy to organize your logging around classes and the packages that contain them:
# Logging settings for com.acme package:
log4j.logger.com.acme=ERROR
# Logging settings for com.acme.util package:
log4j.logger.com.acme.util=WARN
# Logging settings for com.acme.util.MyClass class:
log4j.logger.com.acme.util.MyClass=DEBUG
The most specific configuration takes precedence. Given the example above, all the classes in the com.acme.io package would use the settings for the com.acme package, and the com.acme.util.NonConfiguredClass would use the settings for the com.acme.util package. You can tweak your settings up and down, depending on which classes and packages you are currently debugging. -
static vs non-static loggers[ Go to top ]
- Posted by: Katie Huber
- Posted on: June 11 2004 13:42 EDT
- in response to Paul Strack
A common pattern for Log4J is to organize your logging categories by Java class. In fact, the Logger has a factory method supporting this:// In the MyClass class:Logger logger = Logger.getLogger(MyClass.class);// For non-static loggers:Logger logger = Logger.getLogger(this.getClass());
I've read most people advice the use of a static logger for each class. Does it make much of a difference if loggers are static or not? -
static vs non-static loggers[ Go to top ]
- Posted by: Paul Strack
- Posted on: June 11 2004 15:15 EDT
- in response to Katie Huber
I've read most people advice the use of a static logger for each class. Does it make much of a difference if loggers are static or not?
I agree with that advice. A static logger will use less resources, and therefore you will get better performance. The following syntax is generally preferable:
Logger logger = Logger.getLogger(MyClass.class);
There are some circumstances where you can do clever things with instance loggers (such as polymorphic logging), but as a rule, this is more for advanced users. -
RE:[ Go to top ]
- Posted by: Katie Huber
- Posted on: June 11 2004 15:55 EDT
- in response to Paul Strack
Thank you for your help! I appreciate it.
Katie