Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:29:51+00:00 2026-06-14T08:29:51+00:00

Here are two statements that seem to be generally accepted, but that I can’t

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:29:52+00:00Added an answer on June 14, 2026 at 8:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are two queries that return the same resultset, but which is the optimal
Can someone explain why I get different results from these two statements? I thought
This should be easy, but I can´t seem to get it to work. I
While trying to execute the following lines only the last two statements are displayed(Here
I have two tables: entitytype and project . Here are the create table statements:
Here's two screen shots, showing the effect with a small viewport that has to
Can anyone enlighten me as to the differences between the two statements below. [self
Posts: I can't seem to get my auth controller working. I'm not sure what
Here are two statements from my Mathematica program. Print[ExpandAll[Chop[Integrate[Resi*Cos[7*1*\[Theta]]*Sin[(1*Pi*x)/L], {\[Theta], 0,2*Pi},{x,0, L}], 10^-6]]]; This
I've read through scores of questions on here that seem at first to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.