1
我試圖傳遞一個messageMap變量來記錄,但是當我這樣做時,我無法通過%k {key}語法訪問它們。然而,地圖正在通過,但作爲一個簡單的消息對象。如果我將%msg打印到控制檯中,則會打印所有變量及其值。Log4j2.xml配置文件不解析mapMessage屬性
在控制檯消息中,我希望看到最後傳遞的%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>
你正在使用哪個'log4j2'版本?你可以發佈你的'log4j2'依賴 –