gpt4 book ai didi

Java 记录器 - 标记的撇号问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:27 27 4
gpt4 key购买 nike

我对 java.util.logging 有一些无法解释的行为。让我们看一下这两个示例:

首先:

boolean var = false;
log.log( Level.WARNING, "Cant {0}", new Object[] { var } );

输出:

Cant false

第二个:

boolean var = false;
log.log( Level.WARNING, "Can't {0}", new Object[] { var } );

输出:

Can't {0}

为什么包含撇号 (') 会导致记录器不扩展 token ?

最佳答案

看来无论您使用何种日志记录机制,都在内部使用 MessageFormat 类。如果是这样,那么您需要转义撇号字符,因为它是一个单引号字符。单引号字符用于引用不需要解释的文本。

MessageFormat javadocs :

Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. A single quote itself must be represented by doubled single quotes '' throughout a String.

(强调我的)

尝试

log.log( Level.WARNING, "Can''t {0}", new Object[] { var } );

这段代码:

MessageFormat mf = new MessageFormat("Can''t {0}");
System.out.println(mf.format(new Object[] {false}));

产生这个输出:

Can't false

关于Java 记录器 - 标记的撇号问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22670627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com