You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by ha...@yahoo.com on 2003/12/23 18:34:45 UTC

centralized logging with application ids

I'm working on a distributed system consisting of multiple processes
per machine and multiple machines.  We're currently using the
SocketAppender and SimpleSocketServer to implement centralized logging.
 But I'd like for every log-server client to "automatically" put an
application ID to every log message to identify the
host/processid/servername from which each log message is coming.  What
is the best way to do this?

The most obvious way to me was to add this information to the MDC at
the very beginning of each main program.  This works fine for programs
in which we get to write the main program.  But this doesn't work so
well for programs in which we don't write the main program.  We can
create a "wrapper" main that sets the MDC and then calls the
third-party main method.  But it's still easy to mess up thread
inheritance.  Here's a simple example:

class ITL
{
  static InheritableThreadLocal itl = new InheritableThreadLocal();

  public static void main(String[] args) throws Exception
  {
    itl.set("inherited from main");

    new Bar();
    System.gc();
    System.runFinalization();
  }

  static // oops! static initializer runs before ITL was initialized
  {
    System.out.println("Foo thread local value = " + Foo.fooITLV);
  }
}

class Foo
{
  static Object fooITLV = ITL.itl.get();
}

class Bar
{
  public void finalize()
  {
    // oops! finalizer thread doesn't inherit from main thread
    System.out.println("Bar thread local value = " + ITL.itl.get());
  }
}

In addition to this simple example, threads attached via JNI and
windows signal handler threads won't inherit thread locals from the
main thread.  And if I can think of all these problems so quickly, I'm
sure there are other problems lurking out there.

So given these (admittedly somewhat obscure) problems, I thought of
another solution -- create an Appender that forces the MDC to contain
the host/processid/servername info, and make that Appender the first
one to get run in my log4j client-side config files.  But that just
feels hackish, since it presumes that the LoggingEvent hasn't had its
getMDCCopy() method called, and it forces me to make sure my log-server
clients' log4j config files always list this specialized Appender
before their SocketAppender.  This solution works better than the first
solution -- it catches the obscure cases.  But I personally I don't
like it.  It totally breaks abstraction -- relying on knowledge of
log4j internal MDC caching behavior on one hand and on the other hand
forcing the log-server client to hack their log4j config file.

Do you have any suggestions?

Thanks,

--Ian


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org