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 8182485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:52:44+00:00 2026-06-07T00:52:44+00:00

When you do something like LOG.debug(Exported {}., product) in slf4j it will eventually call

  • 0

When you do something like LOG.debug("Exported {}.", product) in slf4j it will eventually call toString() on the arguments, e.g. product.

For certain reasons, I can’t override toString() on all the classes I want to use as arguments. Some classes come from third party jars, others will have their toString() called in other contexts, too, where the information I want to print in my log statement isn’t available.

However, I have a class for debugging purposes which has a method DebugFormatter.format(Object) that has a long cascade of instanceofs that selects the routine to find some useful debugging information about that object.

My question is: Is it possible to configure slf4j so that it calls such a static method instead of toString()?

Of course, I could call my format method on the object before passing it as a parameter to Logger.debug() but then it would be executed even when the respective logger is not enabled. So I had to surround it with if (LOG.isDebugEnabled()) which means that the whole point of having arguments in debug() was missed.

  • 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-07T00:52:46+00:00Added an answer on June 7, 2026 at 12:52 am

    You could create a shim, taking your object and calling DebugFormatter.format() from its toString() function. Something like this:

    class DebugFormatObject {
      private final Object o;
    
      public static DebugFormatObject forDebug(Object o) {
        return new DebugFormatObject(o);
      }
    
      private DebugFormatObject(Object o) {
        this.o = o;
      }
    
      @Override
      public String toString() {
        return DebugFormatter.format(o);
      }
    }
    

    With the appropriate static import, your logging statement becomes this:

    LOG.debug("Exported {}.", forDebug(product));
    

    This does have slightly more overhead than passing the object in straight, but it’s a small, constant overhead — and the object created will be very short-lived.

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

Sidebar

Related Questions

My app is something like a log book. I keep an evidence of events
My user activity log looks something like this: Id, Username, Date I need to
I am considering using something like StackFrame stackFrame = new StackFrame(1) to log the
Something like this: if self.__class__ == User: logging.debug(%s non_pks were found % (str(len(non_pks))) )
For awhile I've used something like the following to do my debugging. #ifdef DEBUG
I have a log4j properties something like below. Everything that is logged in TextProcessor.log
how do i debug something like this? i need the debugger to stop and
I am able to log things with things like logger.debug This is a logged
I'm doing something like zgrep somepattern access_log.X.gz But I find that a lot of
Something like this: _declspec(align(16)) float dens[4]; //Here the code comes. F32vec4 S_START, Pos, _Vector

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.