Curt Arnold wrote:
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".
On my notebook, using a utility wrapper I made that gets classname,
methodname, and line number, the entire time was 0.06ms for a simple log
statement (loop of 1M for 60 seconds or 60 nanoseconds). Is that a lot
of overhead? Log4j does it in 0.3ms, but should be similar to mine once
1.3 comes out, as it too uses StackTraceElement.
FYI, my utility wrapper was simply getting stack trace information,
asking for a logger with the "classname.methodname", and prepending the
line number onto the message. The only problem with that is that more
memory is used for each and every "classname.methodname", which can be a
lot in a large project.
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:
Ahhh, yeah, what he said! :P
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.
Has anyone ever thought of making Logger or LogManager expose static
debug, warn, info, debug, and trace methods, which use the root logger?
That would certainly improve ease of use. And given the bench marks I
mentioned above with using StackTraceElement, I can't see using anything
other than the root logger. Can you? Am I missing something?
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]
--
Trenton D. Adams
Systems Analyst/Web Software Engineer
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!
__
This communication is intended for the use of the recipient to whom it
is addressed, and may contain confidential, personal, and or privileged
information. Please contact us immediately if you are not the intended
recipient of this communication, and do not copy, distribute, or take
action relying on it. Any communications received in error, or
subsequent reply, should be deleted or destroyed.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]