3
votes

With growing projects we made new agreements in terms of logging different scenarios within these projects, because we usually tend to forget to delete tons of disrubting logger calls which where used for hunting bugs in production code (we even created a coffee bug, where everyone has to pay 20c for each undesired logger without success :/). So what I would love to have is a special log level for Log4j which we can use for debug purposes only.

Currently there are these loggers:

DEBUG
INFO
WARN
ERROR
FATAL

warn, error and fatal are speaking for themselves but I would love to add another category somewhere before DEBUG, because INFO we want to use for general logging of application in production mode and DEBUG gives detail Information about project states, which aren`t necessarly of use in production mode.

Next to DEBUG I would appreciate having a special logger, like for example USER (or simular), for temporary included loggers, which where used for example for bug hunting, or whatever, which can be when found in code be deleted at every time, without hesitation.

I stumbled over this useful article to create an own custom level, but I hate, that I have to add the log level at every log I want to add for debug purposes:

logger.log(MyLevel.DISASTER, "i am a disaster");

So I wondered if there is any way to simply this to gain a new method like

logger.disaster( "i am a disaster" );

for example with writing a fassade for Logger.class. Unfortunatly I am not sure, about logger Initialization and - Creation for delegation to the logger, so I wondered, if anyone might have any idea to simply this.I know with coding templates I can generate the level easily, but it just doesnt read well.

2
a decorator/wrapper for the logger may be which internally will call add the label? The logger initialization takes the same steps as until now. All you do is add a class YourLogger that will call the logger - Eugene

2 Answers

1
votes

May be it's just me but wouldn't :

 class DisasterLogger{
      private static final Log logger = Logger.getLogger(DisasterLogger.class); //or any other way you provide the logger
      public void log(String message){
           logger.log(MyLevel.DISASTER, message);
      }
 }

simply work?

0
votes

Simple Implementation after very helpful opinions to this topic from Eugene and Fildor:

public class IspLogger
{
  private final Logger logger;

  private IspLogger( String clazzName )
  {
    logger = Logger.getLogger( clazzName );
  }

  public static IspLogger getLogger( Class<?> clazz )
  {
    return new IspLogger( clazz.getName() );
  }

  public void info( Object message )
  {
    logger.log( IspLogger.class.getCanonicalName(), Level.INFO, message, null );
  }

  public void info( Object message, Throwable t )
  {
    logger.log( IspLogger.class.getCanonicalName(), Level.INFO, message, t);
  }

  public void dev( Object message )
  {
    logger.log( IspLogger.class.getCanonicalName(), IspDebugLevel.ISP_DEBUG, message, null );
  }

  public void dev( Object message, Throwable t )
  {
    logger.log( IspLogger.class.getCanonicalName(), IspDebugLevel.ISP_DEBUG, message, t);
  }

  public void debug( Object message )
  {
    logger.log( IspLogger.class.getCanonicalName(), Level.DEBUG, message, null );
  }

  ...
}


class IspDebugLevel extends Level
{
  public static final IspDebugLevel ISP_DEBUG = new IspDebugLevel( 20000, "DEV", 0 );/20000 is equal to INFO.

  public IspDebugLevel( int level, String levelStr, int syslogEquivalent )
  {
    super( level, levelStr, syslogEquivalent );
  }

  public static IspDebugLevel toLevel( int val, Level defaultLevel )
  {
    return ISP_DEBUG;
  }

  public static IspDebugLevel toLevel( String sArg, Level defaultLevel )
  {
    return ISP_DEBUG;
  }
}


public class LoggerTest
{
  private static final IspLogger logger = IspLogger.getLogger( LoggerTest.class );

  public static void main( String[] args )
  {
    logger.dev( "Hello World" );
  }
}