Log4j2.xml配置文件无法解析mapMessage属性

时间:2017-10-18 19:32:23

标签: java log4j2

我试图将变量的messageMap传递给日志,但是当我这样做时,我无法通过%k {key}语法访问它们。然而,地图正在通过,但作为一个简单的消息对象。如果我将%msg打印到控制台中,则会打印所有变量及其值。

在控制台消息中,我希望看到最后传递的%K {ip}值,但似乎没有获得任何值,但ip字符串是%msg值。

如果我错过了什么,请查看我的代码并告诉我。 配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
        <Configuration status="debug">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n  %K{argsString} %K{ip} %n%ex{full}%n" />
        </Console>
        <JDBC name="databaseAppender" tableName="durations">
            <ConnectionFactory class="com.lg.log.ConnectionFactory" method="getDatabaseConnection" />
            <Column name="level" pattern="%level" isUnicode="false" />
            <Column name="method" pattern="%K{method}" isUnicode="false" />
            <Column name="arguments" pattern="%K{argsString}" isUnicode="false" />
            <Column name="timestamp" isEventTimestamp="true" />
            <Column name="duration" pattern="%K{duration}" isUnicode="false" />
            <Column name="exception" pattern="%ex{full}" isUnicode="false" />
            <Column name="ip" pattern="%K{ip}" isUnicode="false" />
            <Column name="threadIdentifier" pattern="%K{threadID}" isUnicode="false" />
        </JDBC>
        <JDBC name="databaseExceptionsAppender" tableName="exceptions">
            <ConnectionFactory class="com.lg.log.ConnectionFactory" method="getDatabaseConnection" />
            <Column name="level" pattern="%level" isUnicode="false" />
            <Column name="method" pattern="%K{method}" isUnicode="false" />
            <Column name="arguments" pattern="%K{argsString}" isUnicode="false" />
            <Column name="timestamp" isEventTimestamp="true" />
            <Column name="exception" pattern="%K{exception}" isUnicode="false" />
            <Column name="ip" pattern="%K{ip}" isUnicode="false" />
            <Column name="threadIdentifier" pattern="%K{threadID}" isUnicode="false" />
        </JDBC>
    </Appenders>
    <Loggers>
        <Logger name="com.lg.log.ExceptionLoggerAspect" level="error">
            <AppenderRef ref="databaseExceptionsAppender" />
            <AppenderRef ref="Console" />
        </Logger>
        <Logger name="com.lg.log.DurationLoggingAspect" level="info">
            <AppenderRef ref="databaseAppender"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="ExceptionLoggerAspect"/>
        </Root>
    </Loggers>
</Configuration>

Java代码:

private void logException(JoinPoint joinPoint, Throwable e)  {
    Map<String, String> myMessageMap = new HashMap<>();
    try {
        myMessageMap.put("ip", InetAddress.getLocalHost().getHostAddress().toString());
    } catch (UnknownHostException e1) {
        myMessageMap.put("ip", "UNKNOWN");
    }
    if (joinPoint.getTarget() != null) {
        myMessageMap.put("method", joinPoint.getTarget().toString() + '.' + joinPoint.getSignature().getName());
    } else {
        myMessageMap.put("method", joinPoint.getSignature().getName());
    }
    myMessageMap.put("threadID", String.valueOf(Thread.currentThread().getId()));
    myMessageMap.put("exception", ExceptionUtils.getStackTrace(e));
    myMessageMap.put("argsString", getArgumentsAsText(joinPoint));
    logger.error(new MapMessage(myMessageMap));
}

控制台输出:

15:23:44.705 [main] ERROR com.lg.log.ExceptionLoggerAspect - argsString="" exception="java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
" ip="192.168.0.85" method="com.lg.Test" threadID="1"

LOG4J2依赖关系:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.9.1</version>
        </dependency>

1 个答案:

答案 0 :(得分:2)

log4j2版本2.9 ,您必须使用org.apache.logging.log4j.message.StringMapMessage代替org.apache.logging.log4j.message.MapMessage才能获得字符串值。

根据{{​​3}},

StringMapMessage与版本2.9之前的MapMessage相同。

此外,MapMessage现在属于通用类型,您使用它的方式应该会收到一些警告。