Curt Arnold wrote:

On Nov 28, 2005, at 5:18 PM, Trenton D. Adams wrote:

Trenton D. Adams wrote:

Huh, serialization on a static variable? To my knowledge, that doesn't happen anyhow. LOL Ok, forget that one. What about on an instance variable?


Logger is not serializable (and intentionally not) so you could mark it as transient and recreate it in the appropriate constructor. However, having a logger as an instance variable is rarely desirable.


On Nov 28, 2005, at 5:28 PM, Trenton D. Adams wrote:

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.


You should benchmark the case when logging is disabled. In that case, the static variable approach will have one logger request per class or method and trivial overhead per log request to check that logging is disabled. Your approach may be retrieving a StackTraceElement to get a logger name even though logging on that class or method may be disabled. Your constant overhead may be less than retrieving loggers at class load time, but your cost per log request would have to be higher, particularly if logging is disabled.

Retrieving class and method names were wildly expensive pre JDK 1.3. They are less so with modern JVM's, but they still aren't free and repeated calls to Logger.getLogger() aren't free either. log4j will be optimized for calls to Logger.getLogger() to be much less frequent that calls to Logger.debug() and the like.


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.


Logger.getRootLogger.debug("foo") wouldn't be all that bad and logging everything to root isn't a common practice. You are certainly free to create your own helper class along the lines of:

package com.example.myapp;

public class RootLogger {
private static final Logger logger = org.apache.log4j.Logger.getRootLogger();
   private RootLogger() {
   }

   public static void debug(final String msg) {
      logger.debug(msg);
   }
   ...
}

Yes, I can do that, but the printing of method name and line number does not work then, because all of the calls originate from RootLogger.



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?


Organizing log requests by program structure (class and method names) is definitely the most common categorizing log requests to allow the user to disable or route messages. However, it is not the only one. A user may want to organize log requests by function (security, data, financial) or by some other aspect that doesn't map to class or method names and may not be extractable from the stack trace.

Yes, I wasn't thinking of other people's uses, just mine. Meaning if all I care about is class name, method name, and line number, then logging to the root logger should be ok, given the 0.06ms log time.




---------------------------------------------------------------------
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]

Reply via email to