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

  • SEARCH
  • Home
  • 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 7036385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:25:04+00:00 2026-05-28T01:25:04+00:00

We are using SLF4J Based on a recent discussion in the team if(LOG.isDebugEnabled()){ LOG.debug(hello

  • 0

We are using SLF4J

Based on a recent discussion in the team

if(LOG.isDebugEnabled()){
  LOG.debug("hello " + a + " world" + b);
}

is better than

LOG.debug("hello {} world {}", a, b);

because in the latter case, the String hello {} world {} is created even if “debug” is not enabled. In other words, we are always creating Strings even when it is not required.

I like the latter version as it improves readability significantly.

Can anybody please provide inputs?

Regards,

Shardul.

EDIT

Let me put it differently.

Which is a better approach OR Which is the most widely accepted approach?

Shardul.

  • 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-05-28T01:25:05+00:00Added an answer on May 28, 2026 at 1:25 am

    No, the shorter version does not create the “hello {} world {}” string. The string is already created and placed in the constant pool during compilation and class loading. You are always referencing the same instance from the constant pool, it is as cheap as referencing constant int.

    But the string is created in the first form since you are using string concatenation.

    The only extra overhead is calling LOG.debug with three arguments which does nothing as it calls isDebugEnabled() internally. Also chances are it will be inlined.

    That being said I would go for shorter form in 99% of the cases. The only situation where I would explicitly call isDebugEnabled is when computing the message to be logged has a significant cost:

    if(LOG.isDebugEnabled()){
      LOG.debug("hello {} world {}", cheap, veryExpensive());
    }
    

    If veryExpensive() is, ekhem, very expensive, then it is smart to avoid calling it if it will be discarded. BTW veryExpensive() should not have any side effects and it is hard to imagine long-running, side-effect free method…

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

Sidebar

Related Questions

By default slf4j, when using with jdk ( slf4j-jdk14-1.6.1.jar ), does not log debug
I want to log pretty printed XML using Logback/SLF4J. Right now, what I get
We're using slf4j in a java webapp, and i'd like to be able to
I've heard that using StringBuilder is faster than using string concatenation, but I'm tired
I'm using JUnit4 and Hibernate3 in my project. Hibernate depends on Slf4j and thus
We are running OSGI bundles with pax-runner. We are logging using logback over slf4j.
I'm using SLF4J for the first time in my Maven project, but I can't
We are using SLF4J/Logback combination to perform our logging. One of the requirement we
What's the correct approach to log both an error message and an exception using
I'm using SLF4J with Log4J underneath. What access levels should I be setting my

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.