log4j2如何禁用“date:”查找 - log4j抛出异常

时间:2014-11-24 11:07:17

标签: apache-camel log4j2

编辑目前似乎无法实现filed an issue


我在我的apache camel应用程序中使用log4j2。在camel文件名中可以​​这样配置"?fileName=${date:now:yyyyMMdd-HHmmss}ID.${id}.gz"

如果我将日志级别设置为调试,则camel尝试记录它正在执行的操作但是log4j似乎尝试使用" date:"来查找/解释该字符串。并引发异常:

2014-11-24 11:29:19,218 ERROR Invalid date format: "now:yyyyMMdd-HHmmss", using default java.lang.IllegalArgumentExcepti
on: Illegal pattern character 'n'
        at java.text.SimpleDateFormat.compile(Unknown Source)
        at java.text.SimpleDateFormat.initialize(Unknown Source)
        at java.text.SimpleDateFormat.<init>(Unknown Source)
        at java.text.SimpleDateFormat.<init>(Unknown Source)
        at org.apache.logging.log4j.core.lookup.DateLookup.formatDate(DateLookup.java:60)
        at org.apache.logging.log4j.core.lookup.DateLookup.lookup(DateLookup.java:53)
        at org.apache.logging.log4j.core.lookup.Interpolator.lookup(Interpolator.java:144)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.resolveVariable(StrSubstitutor.java:1008)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:926)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:816)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.replace(StrSubstitutor.java:385)
        at org.apache.logging.log4j.core.pattern.MessagePatternConverter.format(MessagePatternConverter.java:71)
        at org.apache.logging.log4j.core.pattern.PatternFormatter.format(PatternFormatter.java:36)
        at org.apache.logging.log4j.core.layout.PatternLayout.toSerializable(PatternLayout.java:189)
        at org.apache.logging.log4j.core.layout.PatternLayout.toSerializable(PatternLayout.java:53)
        at org.apache.logging.log4j.core.layout.AbstractStringLayout.toByteArray(AbstractStringLayout.java:52)
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:
104)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:97)
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:428)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:407)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:365)
        at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:112)
        at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1347)
        at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1312)
        at org.apache.logging.slf4j.Log4jLogger.debug(Log4jLogger.java:132)
        at org.apache.camel.util.IntrospectionSupport.setProperty(IntrospectionSupport.java:518)
        at org.apache.camel.util.IntrospectionSupport.setProperty(IntrospectionSupport.java:570)
        at org.apache.camel.util.IntrospectionSupport.setProperties(IntrospectionSupport.java:454)
        at org.apache.camel.util.EndpointHelper.setProperties(EndpointHelper.java:249)
        at org.apache.camel.impl.DefaultComponent.setProperties(DefaultComponent.java:272)
        at org.apache.camel.component.file.GenericFileComponent.createEndpoint(GenericFileComponent.java:67)
        at org.apache.camel.component.file.GenericFileComponent.createEndpoint(GenericFileComponent.java:37)
        at org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:123)
        at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:514)
        at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:547)

有没有办法关闭这个&#34;日期:&#34;抬头?为什么它试图解释来自日志的东西呢?我认为不应该以任何方式触及它?!

编辑,非常容易在测试中重现:

public class LogTest {

    private Logger log = LoggerFactory.getLogger(LogTest.class);

    @Test
    public void test() {
        log.info("${date:now:buhu}");

    }
}

对我们来说至关重要$ {date:} - 只有&#34;数据:现在&#34;工作中。 所以这个问题完全独立于camel ,但是camel使用$ {date:...}模式来处理好几件事。这是一个简单的路线,可以重现问题 - 将在骆驼设置阶段抛出异常 - 不需要测试代码 - 日志记录级别必须是&#34; debug&#34; !:

public class LogTest extends CamelTestSupport{

    private Logger log = LoggerFactory.getLogger(LogTest.class);

    @Test
    public void test() {
        //log.info("${date:now:yyyyMMdd-HHmmss}");

    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                from("direct:a").to("file:./?fileName=${date:now:yyyyMMdd-HHmmss}ID.${id}.gz");
            }
        };
    }

}

3 个答案:

答案 0 :(得分:4)

如果simple-Expression被写为$simple{..}而不是${..},则可以避免此问题。然后log4j2将不会使用他的日期查找。

因此,如果您改变路线:

from("direct:a").to("file:./?fileName=${date:now:yyyyMMdd-HHmmss}ID.${id}.gz"); 

为:

from("direct:a").to("file:./?fileName=$simple{date:now:yyyyMMdd-HHmmss}ID.${id}.gz");

它应该可以工作,即使你调试Camel。

答案 1 :(得分:2)

此问题已在2.7版本的Log4j2中修复。

解决方案是升级到该版本(或更高版本),并在模式属性中添加选项&#34; {nolookups}&#34;到%msg。

%msg{nolookups}

例如

<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{1} %L %M %t - %msg{nolookups}%n%xEx%n" />

答案 2 :(得分:1)

要在本地禁用日期查找,您可以添加&#34; $&#34;在表达式前面:

    log.info("$${date:now:buhu}");

这将打印${date:now:buhu},而不是抛出打印堆栈跟踪的异常。

至于如何使用Camel避免这种情况,我不确定。最干净的修复可能是log4j2更新,以禁用其DateLookup功能。临时修复是禁用org.apache.camel包中的DEBUG级别日志:

 <loggers>
     <logger name="org.apache.camel" level="INFO" />
     <root level="debug">
        <appender-ref ref="Console" />
     </root>
 </loggers>

它并不理想,但是如果我们需要调试Camel上下文创建,我们可以增加日志级别,因为日志语句不是一般日常开发所必需的。