0

I recently started using Log4j. I came across a better logging technique of parameterized logging.

I referred the below link.

https://logging.apache.org/log4j/2.x/performance.html

The above link is for log4j2. But I was wondering if the same is applicable for 1.2.9

When I try to write a logger statement with parameters, I am getting below error.

The method info(Object, Throwable) in the type Category is not applicable for the arguments (String, String)

I checked the source code of

I am using log4j 1.2.9

The java code is as below.

import org.apache.log4j.Logger;

import com.test.validators.CreateValidator;

public class CreateLogic {

    Logger admin = Logger.getLogger("admin");

public void createLogic(){
    admin.info("This is Create Logic");
    CreateValidator CreateValidator = new CreateValidator();
    CreateValidator.validateCreate();
    int i = 5;
    admin.info("the name is {}",i);

    }

}

log4j properties are as below.

log4j.logger.admin=INFO, admin
log4j.additivity.admin = false

log4j.appender.admin=org.apache.log4j.RollingFileAppender
log4j.appender.admin.File=C:\\Create.log
log4j.appender.admin.MaxFileSize=5MB
log4j.appender.admin.MaxBackupIndex=1
log4j.appender.admin.layout=org.apache.log4j.PatternLayout
log4j.appender.admin.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
5
  • You're right. .info(Object message, Throwable t) is offered in both 1.x and 2.x APIs. You're issue seems to be that you're passing in i as a Throwable, but it's not. Commented May 2, 2017 at 16:33
  • how do I write parameterized loggers in that case? Commented May 3, 2017 at 10:16
  • I see the issue Commented May 3, 2017 at 13:18
  • Hey, just curious on how you resolved the issue, if you did. Commented Jun 1, 2017 at 16:41
  • 2
    I upgraded to log4j 2. Use it along with slf4j. Commented Jun 1, 2017 at 17:10

1 Answer 1

2

The method that you reference from the 2.x API isn't parameterized logging, or at least in the way you want. The method that you actually want is void info(String message, Object... params) . And this method is a part of the log4j 2.x API. It looks like log4j 1.x doesn't offer it on its own. However, if you aren't bound to log4j, you could take a look at the sl4j library, which essentially hides the log4j implementation and offers that parameterized logging functionality.

If you are bound to log4j, then I think one solution would be to format the message beforehand with the parameters you would have passed in the log message, like so:

 int i = 5;
 String msg = String.format("the name is %d", i);
 admin.info(msg);

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.