JAKARTAPROJECT
JAKARTA TIPJSP TIPJSP Áú¹®&´äº¯DATABASE TIPJAVASCRIPT TIPWEBHACKING TIP±âŸ TIP
ÀÚÄ«¸£Å¸ ÇÁ·ÎÁ§Æ®
ÀÚÄ«¸£Å¸ ÇÁ·ÎÁ§Æ®
ÀÚÄ«¸£Å¸ ÇÁ·ÎÁ§Æ® ÆÁ °Ô½ÃÆÇ ÀÔ´Ï´Ù
À§ÇèÇÑ static Logger Çʵå...
¼­¿¬¾Æºü
À̹ÌÁö ½½¶óÀÌ´õ º¸±â

Logging/StaticLog

When Static References to Log objects can be used

There are two very common patterns used with logging:

public class Foo {
  private Log log = LogFactory.getLog(Foo.class);
  ....
}

and

public class Foo {
  private static Log log = LogFactory.getLog(Foo.class);
  ....
}

The use of the static qualifier can be beneficial in some circumstances. However in others it is a very bad idea indeed, and can have unexpected consequences. This page describes when each solution is appropriate.

The Static Problem

The technical result of using static is obvious: there is only one Log reference shared across all instances of the class. This is clearly memory efficient; only one reference(4 or 8 bytes) is needed no matter how many instances are created. It is also CPU-efficient; the lookup required to find the Log instance is only done once, when the class is first referenced.

When writing stand-alone application code the use of static is a good idea.

However when the java code concerned is a library that may be deployed within a container of some sort (such as a J2EE server) then problems can occur. It is common for containers to create a hierarchy of java ClassLoader objects such that each "application" deployed within the container has its own ClassLoader but where there is some shared classloader that is in the ancestry of all the deployed "applications". In this case, when a class holding a static reference to a Log instance is deployed at the "application" level (eg at the "webapp" level in a servlet or j2ee container) there is also no problem; if multiple applications deploy the class in question then they each have their own copy of the class and there are no interactions with other applications.

However consider the case when a class using "private static Log log = ..." is deployed via a ClassLoader that is in the ancestry of multiple supposedly independent "applications". In this case, the log member is initialised only once, because there is only one copy of the class. That initialisation (typically) occurs the first time any code tries to instantiate that class or call a static method on it. When initialisation of the class occurs, what should the log member be set to? The options are:

  • To reference an underlying Log object which is part of a hierarchy configured from information at the "container" level, ie not associated with any particular "application"

  • To reference an underlying Log object which is part of a hierarchy configured from information somehow related to the current application

  • To reference a "proxy" object which will determine what the "current application" is each time a log method is invoked, and delegate to the appropriate underlying Log object at that time.

The first option means that logging cannot be configured at a per-application level. Whatever logging configuration is set up will apply equally to every application within the container, and all the output from those applications will be mixed together. This is a major issue.

The second option means that the Log object will be configured according to the first application that called it. Other applications within the container will have their output sent to the destination configured for that first application. Obviously this is a major issue.

The third option allows per-application logging configuration, and correctly filters and outputs the required log messages. However the performance penalty paid is very large; this isn't an acceptable solution either.

Note that this discussion hasn't talked about "Thread Context ClassLoaders" or other technical points. The problem is not with the details but with the general concept: when the Log object is shared among multiple applications it isn't possible to have per-application configuration and reasonable performance.

The real root cause of this problem is that SHARED data (static members on a class) is being shared across supposedly independent "applications". If no classes are "shared" between applications then the problem does not exist. However it appears that (to my personal frustration) container vendors continue to encourage the use of shared classes, and application developers continue to use it.

The alternative solution is: avoid the use of "static" references to Log objects in any code that might be deployed into a shared classpath.

Does SLF4J have this problem?

Yes. SLF4J does suffer from exactly the issues listed above, and the recommendation is the same: avoid static references to log objects from code that might be deployed in a shared classpath.

There are a number of possible scenarios here. First however we need to define a term "TCCL-aware". A logging library is "TCCL-aware" when its initialisation code uses the Thread Context ClassLoader to attempt to locate its configuration files, rather than the ClassLoader of the classes in the logging library itself. As an example, in its standard form, log4j is not "TCCL-aware". However it can be made "TCCL-aware" via a "Contextual RepositorySelector" as described here:

Now onto some possible scenarios:

Suppose SLF4J is deployed at a shared level, that there is a class in the shared classpath with a static Logger reference, and that there is a class in the application classpath with a static logger. When the underlying logging library is not TCCL-aware then logging configuration is only read from the "shared" classpath, ie applies globally to all output from all applications deployed within the container. Output is correct, but there is no ability to enable debug logging for just one application, and all output from all applications is mixed together. When the underlying logging library is TCCL-aware, then there is per-application logging configuration. In addition, output from those classes deployed at the application level is correct. However the class deployed at the shared level will have its Log object initialised using the configuration for the very first application that calls that class; when other applications call that same class the logging output will go to the destination of the first application. Not good.

If SLF4J is deployed at both the shared and the application level, and parent-first classloading is selected for an application then behaviour is identical to the above. Even when child-first classloading is selected, if the underlying logging library is TCCL-aware then things also go poorly. However if child-first classloading is selected AND the underlying logging library is not TCCL-aware then the results are almost bearable: output from the shared class can only be configured globally, ie all output goes to the same destination regardless of which application made the call.

Unfortunately in most cases the authors of library code are not in the position of being able to specify to the library users where the code will be deployed, what classloading order will be selected, or what the behaviour of the underlying logging library should be. Therefore it is necessary to avoid the use of static references to SLF4J loggers for exactly the same reasons as commons-logging.

SLF4J does have less probability of encountering problems than commons-logging when used incorrectly, however. The reasons are:

  • Currently not a lot of logging libraries are "TCCL-aware", and

  • Very little library code of the type deployed into shared classpath locations currently uses SLF4J.

Commons-logging is itself TCCL-aware specifically to enable application-level configuration of logging libraries that are not TCCL-aware; effectively therefore every logging library qualifies as "TCCL-aware" when used with commons logging. Commons-logging is also very widely used by many libraries that are frequently deployed via shared classloaders.

Note that this is section is NOT intended as criticism of SLF4J. As described earlier, the problem is due to the fundamental conflict between isolating logging between applications while sharing a static Log reference, and therefore SLF4J faces the same constraints as commons-logging in these scenarios.

Does java.util.logging have this problem?

Yes. Code using the java.util.logging api can suffer from exactly the issues listed above, and the recommendation is the same: avoid static references to log objects from code that might be deployed in a shared classpath.

The java1.4 java.util.logging package is an API, allowing multiple implementations of that API. Java runtimes provide one very simple implementation of the API, but containers such as J2EE servers typically plug in an alternate more sophisticated implementation.

When using the default implementation, the "static" problem doesn't exist because the standard implementation is not container-aware. Of course this has the side-effect of making per-application configuration impossible for the reasons described earlier.

When a container provides an implementation that is container-aware (so that per-application log configuration is possible), then exactly the same situation occurs: when code in the shared classpath creates a Log object, it must be:

  • initialised with config NOT from any application, or

  • initialised with config from one of the available applications, or

  • a proxy that determines the appropriate config each time a method is called

As described above, the first gives no per-app config, the second misdirects logging to the wrong app when called from a different application, and the third is extremely inefficient.

So just as for commons-logging and SLF4J, it is necessary to avoid static Log objects when using java.util.logging unless you know that the underlying implementation that will be available at runtime is not container-aware.

Just to be completely clear: this only applies to code deployed via a classloader that is shared across multiple "independent" applications. None of this discussion is relevant to normal application code, or to library code that is never deployed into a shared classpath. These categories of code can safely use static references.

Alternatives to static loggers

In most cases, simply leaving out the "static" qualifier from the Log reference is the correct solution. Even when a Class is shared between supposedly-independent applications (because the .class file is deployed via a shared classloader), instances of that class are not shared. The TCCL active when the instance is created and its Log member initialised will therefore be the same TCCL active when any of its methods which generate log output are called. Note that in the case of commons-logging this does NOT increase the number of Log objects created; each instance will have a reference to the same Log object.

However the "lookup" of that Log object does need to be performed each time an instance is created; for objects which have short lifetimes this may be undesirable. In this situation, if the short-lived object has a reference to some longer-lived one then the longer-lived one can host the Log object, eg

  public ShortLiver {
    private Owner owner;
    ShortLiver(Owner owner) {
        this.owner = owner;
    }
    public void doSomething() {
      owner.getShortLiverLog().debug("doSomething called");
    }
  }

Alternatively, the Log object can be retrieved when necessary:

  public ShortLiver {
    public void doSomething() {
      Log log = LogFactory.getLog(ShortLiver.class);
      log.debug("doSomething called");
    }
  }

Note that this applies only to code that may be deployed in a shared ClassLoader. Normal application code need not be concerned with this issue at all.

What about static methods?

When Log objects are not static then logging from static methods presents a particular problem.

In general, calling LogFactory.getLog within the static method is the appropriate solution:

  public static void doStuff(...) {
    Log log = LogFactory.getLog("stuff");
    log.warn(...);
  }

If the static method has a reference to some object that it could retrieve a logger from it, eg

  public static void doStuff(AppContext context, ...) {
    context.getStuffLog().warn("oops");
  }

This doesn't seem widely applicable though. Fancier solutions like retrieving log objects from a collection in a thread-local variableare probably not worth trying; the LogFactory.getLog method isn't that slow.

Container-assisted logging

A few containers have tried to work around the problems listed above. In particular, JBoss provides a custom log4j "filter" that uses internal knowledge about the container to make configuration at the container level work in a somewhat acceptable manner. As described above, deploying commons-logging (or SLF4J) at the shared level, and deploying a non-TCCL-aware underlying library has the undesirable effect of imposing a single logging configuration on all contained applications. However the jboss custom filter then allows log messages flowing through that single logging instance to be separated out again into different destinations according to the originating application. This has the desired effect in the end, although it is somewhat inefficient; turning up logging to debug on one application actually turns up logging to debug on all applications, though output from applications other than the one of interest is then suppressed again before being output.

last edited 2006-03-08 01:44:06 by SimonKitching

 

http://wiki.apache.org/jakarta-commons/Logging/StaticLog

 

2007-02-14 19:46:22
168.154.45.***
0Á¡ (0¸í)
µ¡±Û 2°³ | ÅÂ±× 1°³ | °ü·Ã±Ûº¸±â
ű×ÀÔ·Â
½±Ç¥(,)±¸ºÐÀ¸·Î Çѹø¿¡ ¿©·¯ ű׸¦ ÀÔ·ÂÇÒ¼ö ÀÖ½À´Ï´Ù
ÈìÈì (1)
´ÞÆØÀÌ
(0) (0)
shared classLoader »ç¿ëÀ϶§ ¹®Á¦°¡ µÈ´Ù´Â°ÍÀÎÁö.. Çؼ®ÀÌ.. ¤Ñ¤Ñ;;
203.241.147.*** 2006-03-16 09:19:44
GoodBug
(0) (0)
Àúµµ ·Î±ëÀº Ç×»ó staticÀ¸·Î »ç¿ëÇϱä Çߴµ¥.. Çѹø °í½ÉÇØ ºÁ¾ß°Ú³×¿ä ÁÁÀºÀÚ·á °¨»çÇÕ´Ï´Ù
211.189.124.*** 2006-03-23 16:52:04
À̸§ ºñ¹Ð¹øÈ£
ÀÚÄ«¸£Å¸ ÇÁ·ÎÁ§Æ®
ÀÚÄ«¸£Å¸ ÇÁ·ÎÁ§Æ® ÆÁ °Ô½ÃÆÇ ÀÔ´Ï´Ù
! ¹øÈ£ Á¦¸ñ ±Û¾´ÀÌ ÀÏÀÚ Á¶È¸
Hierarchy of the Apache Software Foundation GoodBug 2005-10-14 10,814
Jakarta Project °­Á °Ô½ÃÆÇÀÔ´Ï´Ù 8 GoodBug 2005-04-03 11,701
44 Log4J log4j¿¡¼­ e.printStackTrace() ¸Þ½ÃÁö¸¦ log¿¡ ³²±â´Â ¹æ¹ý 1 kaiser 2008-10-22 17,613
43 DBUtils DBUtils¿¡¼­ Clob »ç¿ëÇϱâ 3 1 GoodBug 2007-08-28 10,633
42 Spring Spring ¼³Á¤ ÆÄÀÏ ·Îµù 1 GoodBug 2007-07-16 11,317
41 POI POIÀÇ HSLF¸¦ ÀÌ¿ëÇÏ¿© PowerPoint ¹®¼­¸¦ ÀоÀÚ 2 GoodBug 2007-05-28 14,910
40 POI POIÀÇ HWPF¸¦ ÀÌ¿ëÇÏ¿© MS WORD¹®¼­¸¦ ÀоÀÚ 2 GoodBug 2007-05-28 16,839
39 Validator Validator ¼Ó¼ºµé 1 GoodBug 2007-05-11 10,404
38 dd Commons-Fileupload 1.2 1 2 GoodBug 2007-04-23 15,345
37 Apache Apache2 + Tomcat5.5 + mod_jk 4 ¹ÙÀÌ·¯½ºô¸国 2007-01-29 11,066
36 DBUtils DBUtils¿¡¼­ number ŸÀÔÀÇ Ä÷³ÀÌ intÇüÀ¸·Î ¾È³Ñ¾î¿Ã¶§.. 3 1 GoodBug 2006-06-28 10,755
ÈìÈì À§ÇèÇÑ static Logger Çʵå... 2 1 ¼­¿¬¾Æºü 2006-03-16 10,113
34 Installing Tomcat with commons-daemon (jsvc) GoodBug 2006-01-08 9,067
33 commons Commons DbUtils ¸î°¡Áö ¿¹Á¦ 3 2 GoodBug 2005-11-17 15,221
32 commons Jakarta Commons Net ¿¡¼­ FTP »ç¿ë½Ã ¸ñ·ÏÀÌ ¾Èº¸ÀÏ °æ¿ì 2 GoodBug 2005-11-15 21,776
31 listFiles() ¿¡¼­ null À» ¹Ýȯ ÃßÀû.. ½Å¸¸µÎ 2008-11-11 11,866
30 commons ¸ñ·ÏÀÌ ¾Èº¸ÀÏ °æ¿ì ÇØ°á±â Iź 1 2 GoodBug 2005-12-23 15,932
29 POI POI·Î ¿¢¼¿ÆÄÀÏ ÀÐÀ»¶§, Invalid header signature ¿¡·¯ 1 GoodBug 2005-11-12 16,540
28 log4j log4j, JSP¿¡¼­ ¿øÇÏ´Â Appender °ñ¶ó¾²±â 1 GoodBug 2005-11-07 13,894
27 commons Commons-Email~ 7 2 GoodBug 2005-10-13 17,860
copyright 2005-2024 by Unicorn