Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Log4j2:在StringBuilderPerformAttable上调用了toString()而不是formatTo()_Java_Logging_Log4j_Log4j2_Stringbuilder - Fatal编程技术网

Java Log4j2:在StringBuilderPerformAttable上调用了toString()而不是formatTo()

Java Log4j2:在StringBuilderPerformAttable上调用了toString()而不是formatTo(),java,logging,log4j,log4j2,stringbuilder,Java,Logging,Log4j,Log4j2,Stringbuilder,我使用的是log4j2版本2.10.0 我已经实现了StringBuilderFormattable接口来实现无gc日志记录 实施示例: public static class Formattable implements StringBuilderFormattable { @Override public void formatTo(StringBuilder buffer) { buffer.append("FORMAT_TO_CALLED");

我使用的是log4j2版本2.10.0

我已经实现了
StringBuilderFormattable
接口来实现无gc日志记录

实施示例:

    public static class Formattable implements StringBuilderFormattable {


    @Override
    public void formatTo(StringBuilder buffer) {
        buffer.append("FORMAT_TO_CALLED");
    }

    @Override
    public String toString() {
        return "TO_STRING_CALLED";
    }
}
我正在调用
LOGGER.info(newformattable())
并将调用的
格式设置为字符串,而不是预期的
格式设置为调用的字符串

我目前正在使用以下属性运行它:

-Dlog4j2.enable.threadlocals=true
-Dlog4j2.enable.direct.encoders=true
-Dlog4j2.asyncLoggerWaitStrategy=Yield
经过一番挖掘,我发现log4j find
javax.servlet.servlet
类并设置了
常量。WEB\u APP
属性设置为
true
,所以我的
-Dlog4j2.enable.threadlocals=true
实际上设置为false,由于它使用了
ParameterizedMessageFactory
而不是
ReusableMessageFactory
,这导致调用
toString
,所以我尝试在简单的项目中使用相同的配置运行它,但没有
javax.servlet.servlet
,它工作得很好


所以问题是为什么它有这样的行为,它调用
toString()
,而不是
formato()
?为什么它依赖于web应用程序?我理解webapp threadlocals可能导致的问题,但我不理解为什么
ParameterizedMessageFactory
也不使用此
格式。另外,如果我仅从核心线程使用此
StringBuilderFormattable
进行日志记录,那么将
log4j2.is.webapp
设置为
false
是否安全?

我也面临类似的问题。正在调用“toString”而不是“formatTo”。您是否找到了解决此问题的方法?