views:

40

answers:

2

Hi,

We are looking to use SLF4J, but one thing we found was that you can't specify the level as an argument, i.e

Logger.log(Level.INFO, "messsage");

You have to do this

logger.info("message");

this prevents being able to pass everything through a method, so you can tack other properties to all log messages in a class.

public class Test
{
    public Test(SomeObj obj)
    {
       log(Level.INFO, "message");
    }

    public void anotherMethod()
    {
       log(Level.DEBUG, "another message");
    }
    private void log(Level level, String message)
    {
        logger.log(level, message + obj.someString());
    }
}

Is there a way to achieve this using SLF4j ?

+1  A: 

The answer is No. Refer to this discussion.

Raghuram
+2  A: 

Write a wrapper around the slf4j call and create your own enum for the six log levels. Then in your wrapper, use a switch to call the correct slf4j call.

void myLog(Level level, String message)
{
  switch (level)
  {
  case FATAL:
    log.fatal(message);
    break;
  case ERROR:
    log.error(message);
    break;
  ....
  }
}
Starkey