Because it's so onerous to pass a reference to the logger down through parameters lists, I thought I might try using Java's thread-local store. I haven't been using it for anything else either, but I thought I'd start. For now, the logger is the only thing that tempts me. In past lives as a web-application writer, I used it quite a bit.

My question is, "Does this offend anyone who cares to give an opinion?" If there's a reason not to do this, I'll go to the effort (and muddy my parameter lists). Otherwise, I'll give it a try.


public class CustomProcessor extends AbstractProcessor
{
  private static ThreadLocal< ComponentLog > tls = new ThreadLocal<>();

  public static ThreadLocal< ComponentLog > getTls() { return tls; }

  @Override
  public void onTrigger( final ProcessContext context, final ProcessSession session ) throws ProcessException
  {
    // grab the logger and store it on the thread...
    tls.set( getLogger() );

    ...

    // we've finished using it--dump the logger...
    tls.remove();
  }
}

public class Foo
{
  public void bar()
  {
    ComponentLog logger = CustomProcessor.getTls().get();
    logger.warn( "This is a warning!" );
  }
}

Thanks for any opinions, statements of best practice, cat calls, sneers, etc.

;-)

Russ

Reply via email to