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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:35:20+00:00 2026-05-20T09:35:20+00:00

With sl4fj if I want to construct a string message there is a nice

  • 0

With sl4fj if I want to construct a string message there is a nice approach which makes use of substitutions. For instance, it might be something like:

logger.info("Action {} occured on object {}.", objectA.getAction(), objectB);

If there are more than a few substitutions required then it is something like:

logger.info("Action {} occured on object {} with outcome {}.", 
    new Object[]{objectA.getAction(), objectB, outcome});

My question is: Is there a generic way for me to create a string (and not just a slf4j log message)? Something like:

String str = someMethod("Action {} occured on object {}.", objectA.getAction(), objectB);

or

String str = someMethod("Action {} occured on object {} with outcome {}.", 
    new Object[]{objectA.getAction(), objectB, outcome});

If it is in the standard Java library, what would that “someMethod” be?

  • 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-20T09:35:20+00:00Added an answer on May 20, 2026 at 9:35 am

    String.format

    String str = String.format("Action %s occured on object %s.",
       objectA.getAction(), objectB);
    

    Or

    String str = String.format("Action %s occured on object %s with outcome %s.",
       new Object[]{objectA.getAction(), objectB, outcome});
    

    You can also use numeric positions, for example to switch the parameters around:

    String str = String.format("Action %2$s occured on object %1$s.",
       objectA.getAction(), objectB);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a Custom logger which logs all log entries to a
What's the correct approach to log both an error message and an exception using
When Use with slf4j, String test = blahblahblah; logger.info({},test); Trace as below java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
I'm using Apache Commons Logging and SLF4J with log4j, but I also want to
I want to log only the first few lines of Exceptions in my program.
I've heard that using StringBuilder is faster than using string concatenation, but I'm tired
i want to create a very simple JMS standalone client to a JMS Topic
I have a case where I want a dependency on the runtime classpath but
I've found a useful article that explains how to make Jersey to use SLF4J
It seems that log4j has some class loading issues (among others) and it seems

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.