Search code examples
javalogginglog4j2code-readability

Differentiate logging informations of multiple instances of the same class


I'm trying to log the methods call chain of some classes instances of a Java program using Log4j2. Each instance will behave differently based on the inputs they will recive (obviously).

The problem I'm facing is differentiating the logging messages of each instances for easly reconstructing the methods call chains I was talking about. In addition to logging the entering and the leaving of each method, which is the default logging schema I'm using, I've tried to:

  1. add method call parameters to the log: too few informations and not very readable;
  2. add more informations about the method behaviour: too verbose and not very readable;
  3. adding instance hash code to all the logging messages, which will become something like LOG.trace("{} - Leaving constructor(index:{})", System.identityHashCode(this), index);: good solution, but I've to add some boilerplate code to all the logging methods, which makes the code a little less readable;
  4. using per-instance loggers (so not the per-class/static one) and adding the instance hash code in the logger name (so the logger name will be Classname@hashcode): seems the best solution in clean code terms, but I didn't found any way for declaring logger settings (like logging threshold) for multiple loggere, i.e. for all the loggers which name starts with Classname.

Which one do you think will be the best solution, or do you have any other way to suggest?


Solution

  • For this requirement, you can easily use an nested thread context: Look at "Fish Tagging" in https://logging.apache.org/log4j/2.x/manual/thread-context.html .

    Excerpt:

    ThreadContext.push(UUID.randomUUID().toString()); // Add the fishtag;
    
    logger.debug("Message 1");
    .
    .
    .
    logger.debug("Message 2");
    .
    .
    ThreadContext.pop();