On Nov 28, 2005, at 5:04 PM, Trenton D. Adams wrote:
Hi guys,
Here's a scenario. I have a web application running under tomcat5,
with multiple servlets, and various other classes. I want them all
to log to the root logger. Then I plan on using "%-35C{2}",
"%-15M" and, "%-5L" inside my log4j.properties, to print out the
classname, method name, and line number respectively. How should
one go about doing this?
Determining the class name and method name from the stack information
is very expensive and occurs for each log request. It is usually a
lot cheaper to get a logger once for each class and name it after
the class. If you really need to have the method available too, I'd
extend it to have a static logger for each method and name them like
"org.example.someapp.MyClass:setFooFactor".
I was thinking I could create a static variable in each class that
logs, with a variable of type Logger. But what happens on
serialization/deserialization? Am I going to have problems with that?
Loggers are not serializable and static members don't get serialized
with instance variables. So doing something like:
private static final Logger logger = Logger.getLogger
("org.example.someapp.MyClass");
or
private static final Logger setFooLogger = Logger.getLogger
("org.example.someapp.MyClass:setFoo");
private static final Logger getFooLogger = Logger.getLogger
("org.example.someapp.MyClass:getFoo");
should work well with serialized classes.
Should I create a utility class that exposes a public variable of
type Logger?
I wouldn't.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]