You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Joel Griffith <jg...@nd.edu> on 2022/03/30 17:23:20 UTC

How to migrate parameterized messages

Hi,

I'm upgrading an application from Log4j v1 to v2.

The v1 code often uses the debug()/info()/etc. methods with the
`Logger.debug(Object message, Throwable throwable)` signature to throw
errors while logging.  In many cases, the `message` String is composed
using concatenation, as with

    logger.debug("Compiler Exception for " + datafilter, e);

where `datafilter` is a String and `e` is an Exception (`logger`, of
course, is a Logger).

I'm looking for the simplest drop-in replacement for such lines to migrate
them to Log4j v2 and its requirement for parameterized messages.  The Log4j
migration guide provides a drop-in replacement for the simplest case; i.e.

    logger.info("hi " + userName) -> logger.info("hi {}", userName)

but when I apply this naively to my case,

    /* Log4j v2 ? */
    logger.debug("Compiler Exception for " + datafilter, e) ->
logger.debug("Compiler Exception for {}", datafilter, e)

the method signature looks odd and I'm not sure it will work.  I can't test
it because the code is not functional because it's in the middle of Log4j
remediation.

My question is, is the above example labeled 'Log4j v2 ?' correct for
migrating from Log4j v1 to v2?  If not, what's the simplest drop-in
replacement for the original `debug()` method call that includes a
Throwable as an argument?

Thanks,
Joel

Re: How to migrate parameterized messages

Posted by Ralph Goers <ra...@dslextreme.com>.
If you are doing

   Logger logger = LogManager.getLogger(“edu.nd.MyClass”);
or
   Logger logger = LogManager.getLogger(edu.nd.MyClass.class);

then all you have to do is change the imports from org.apache.log4j to 
org.apache.logging.log4j. Everything else would be optional.

If you are obtaining the Logger from the Logger’s static method you 
would need to change that to one of the forms above.

Using parameterized messages is a good idea as it allows you to change

If (logger.isDebugEnabled()) {
  logger.debug(“Compiler Exception for “ + datafilter, e);
}

to 

logger.debug(“Compiler Exception for {}”, datafilter, e);

But doing this is optional and can be done over time to clean up the code.

Ralph

> On Mar 30, 2022, at 10:23 AM, Joel Griffith <jg...@nd.edu> wrote:
> 
> Hi,
> 
> I'm upgrading an application from Log4j v1 to v2.
> 
> The v1 code often uses the debug()/info()/etc. methods with the
> `Logger.debug(Object message, Throwable throwable)` signature to throw
> errors while logging.  In many cases, the `message` String is composed
> using concatenation, as with
> 
>    logger.debug("Compiler Exception for " + datafilter, e);
> 
> where `datafilter` is a String and `e` is an Exception (`logger`, of
> course, is a Logger).
> 
> I'm looking for the simplest drop-in replacement for such lines to migrate
> them to Log4j v2 and its requirement for parameterized messages.  The Log4j
> migration guide provides a drop-in replacement for the simplest case; i.e.
> 
>    logger.info("hi " + userName) -> logger.info("hi {}", userName)
> 
> but when I apply this naively to my case,
> 
>    /* Log4j v2 ? */
>    logger.debug("Compiler Exception for " + datafilter, e) ->
> logger.debug("Compiler Exception for {}", datafilter, e)
> 
> the method signature looks odd and I'm not sure it will work.  I can't test
> it because the code is not functional because it's in the middle of Log4j
> remediation.
> 
> My question is, is the above example labeled 'Log4j v2 ?' correct for
> migrating from Log4j v1 to v2?  If not, what's the simplest drop-in
> replacement for the original `debug()` method call that includes a
> Throwable as an argument?
> 
> Thanks,
> Joel


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org