You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Ralph Goers (JIRA)" <ji...@apache.org> on 2014/06/11 08:37:01 UTC
[jira] [Commented] (LOG4J2-632) Formatter logger mixes up arguments
[ https://issues.apache.org/jira/browse/LOG4J2-632?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14027472#comment-14027472 ]
Ralph Goers commented on LOG4J2-632:
------------------------------------
I'm a bit confused on how this works at all. The formatted logger uses String.format(). The javadoc for the format syntax -http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax - indicates that the argument indexes have to be followed by a $. which you have only done for the date in the second message.
I suspect what is happening is that in the first message %1 and %2 are taken to be widths and not argument indexes, so it automatically uses argument 1 and argument 2. In the second message the %1$ indicates the first argument should be used while %2s causes the first argument to be used since it is the first format specifier that does not have an argument index.
Please correct your format strings and the close this if that resolves the problem.
> Formatter logger mixes up arguments
> -----------------------------------
>
> Key: LOG4J2-632
> URL: https://issues.apache.org/jira/browse/LOG4J2-632
> Project: Log4j 2
> Issue Type: Bug
> Affects Versions: 2.0-rc1
> Reporter: Alexandre Gattiker
>
> {code}
> public class Example {
> private final static Logger FORMATTER_LOGGER = LogManager.getFormatterLogger(Example.class);
> public static void main(String[] args) {
> FORMATTER_LOGGER.log(ERROR, "It is %1s in %2s", new Date(), "Switzerland"); // works fine
> FORMATTER_LOGGER.log(ERROR, "It is %1$tF in %2s", new Date(), "Switzerland"); // ignores "Switzerland", uses %1 value instead
> }
> }
> {code}
> {noformat}
> 13:53:21.350 [main] ERROR com.example.Example - It is Tue May 06 13:53:21 CEST 2014 in Switzerland
> 13:53:21.355 [main] ERROR com.example.Example - It is 2014-05-06 in Tue May 06 13:53:21 CEST 2014
> {noformat}
--
This message was sent by Atlassian JIRA
(v6.2#6252)
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org