Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized logging in slf4j - how does it compare to scala's by-name parameters?

Here are two statements that seem to be generally accepted, but that I can't really get over:

1) Scala's by-name params gracefully replace the ever-so-annoying log4j usage pattern:

if (l.isDebugEnabled() ) { 
      logger.debug("expensive string representation, eg: godObject.toString()") 
   }

because the by-name-parameter (a Scala-specific language feature) doesn't get evaluated before the method invocation.

2) However, this problem is solved by parametrized logging in slf4f:

logger.debug("expensive string representation, eg {}:", godObject[.toString()]);

So, how does this work? Is there some low-level magic involved in the slf4j library that prevents the evaluation of the parameter before the "debug" method execution? (is that even possible? Can a library impact such a fundamental aspect of the language?)

Or is it just the simple fact that an object is passed to the method - rather than a String? (and maybe the toString() of that object is invoked in the debug( ) method itself, if applicable).

But then, isn't that true for log4j as well? (it does have methods with Object params). And wouldn't this mean that if you pass a string - as in the code above - it would behave identically to log4j?

I'd really love to have some light shed on this matter.

Thanks!

like image 867
teo Avatar asked Nov 02 '12 16:11

teo


People also ask

What is the default logging level in SLF4J?

If no logging dependency is provided on the classpath, it will default to a no-operation implementation and nothing gets logged.

Does SLF4J use Commons logging?

Unlike Commons logging, SLF4J doesn't need a properties file or system property to find the underlying implementation. Instead, SLF4J depends on static binding to bind with the underlying logging framework for which we need to provide a SLF4J binding jar.

Does SLF4J depend on log4j?

So essentially, SLF4j does not replace LOG4j; they work together hand in hand. It removes the dependency on LOG4j from your application and make it easy to replace it in future with more capable library without any kind of source code changes.

How does SLF4J binding work?

Bindings are basically implementations of a particular SLF4J class meant to be extended to plug in a specific logging framework. By design, SLF4J will only bind with one logging framework at a time. Consequently, if more than one binding is present on the classpath, it will emit a warning.


1 Answers

There is no magic in slf4j. The problem with logging used to be that if you wanted to log let's say

logger.debug("expensive string representation: " + godObject)

then no matter if the debug level was enabled in the logger or not, you always evaluated godObject.toString()which can be an expensive operation, and then also string concatenation. This comes simply from the fact that in Java (and most languages) arguments are evaluated before they're passed to a function.

That's why slf4j introduced logger.debug(String msg, Object arg) (and other variants for more arguments). The whole idea is that you pass cheap arguments to the debug function and it calls toString on them and combines them into a message only if the debug level is on.

Note that by calling

logger.debug("expensive string representation, eg: {}", godObject.toString());

you drastically reduce this advantage, as this way you convert godObject all the time, before you pass it to debug, no matter what debug level is on. You should use only

logger.debug("expensive string representation, eg: {}", godObject);

However, this still isn't ideal. It only spares calling toString and string concatenation. But if your logging message requires some other expensive processing to create the message, it won't help. Like if you need to call some expensiveMethod to create the message:

logger.debug("expensive method, eg: {}",
    godObject.expensiveMethod());

then expensiveMethod is always evaluated before being passed to logger. To make this work efficiently with slf4j, you still have to resort back to

if (logger.isDebugEnabled())
    logger.debug("expensive method, eg: {}",
        godObject.expensiveMethod());

Scala's call-by-name helps a lot in this matter, because it allows you to wrap arbitrary piece of code into a function object and evaluate that code only when needed. This is exactly what we need. Let's have a look at slf4s, for example. This library exposes methods like

def debug(msg: => String) { ... }

Why no arguments like in slf4j's Logger? Because we don't need them any more. We can write just

logger.debug("expensive representation, eg: " +
    godObject.expensiveMethod())

We don't pass a message and its arguments, we pass directly a piece of code that is evaluated to the message. But only if the logger decides to do so. If the debug level isn't on, nothing that's within logger.debug(...) is ever evaluated, the whole thing is just skipped. Neither expensiveMethod is called nor any toString calls or string concatenation happen. So this approach is most general and most flexible. You can pass any expression that evaluates to a String to debug, no matter how complex it is.

like image 168
Petr Avatar answered Sep 28 '22 04:09

Petr