简体   繁体   English

Log4j2 - 如何将消息和日志级别添加到 keyValuePair 值

[英]Log4j2 - How to add message and log level to keyValuePair value

Desired Outcome: I would like my logs to store the below data in the keyValuePair: "@timestamp" : "05-11-2020 17:24:29.895", "@Message" : "Log Message", "@LogLevel" : "INFO"预期结果:我希望我的日志将以下数据存储在 keyValuePair 中: "@timestamp" : "05-11-2020 17:24:29.895", "@Message" : "Log Message", "@LogLevel" : “信息”

Issue i'm having is in the KeyValuePair section i am unable to get the event lookup to pull through the message field of the log and the level of the log.我遇到的问题是在 KeyValuePair 部分,我无法通过事件查找来拉取日志的消息字段和日志级别。

Below is my config file currently:以下是我目前的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j2.test" status="info">
    <Appenders>
        <Console name="ConsoleRuleLogger" target="SYSTEM_OUT">
            <JsonLayout complete="false" compact="false" properties="true" objectMessageAsJsonObject="true">
                <KeyValuePair key="@timestamp" value="${date:dd-MM-yyyy HH:mm:ss.SSS}" />
                <KeyValuePair key="@Message" value="${message}" />
                <KeyValuePair key="@LogLevel" value="${level}" />
            </JsonLayout>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.reflections" level="off" additivity="false">
            <AppenderRef ref="ConsoleRuleLogger" />
        </Logger>
        <Root level="info">
            <AppenderRef ref="ConsoleRuleLogger"/>
        </Root>
    </Loggers>
</Configuration>

I have also tried the following changes to KeyValuePair:我还尝试对 KeyValuePair 进行以下更改:

 <KeyValuePair key="@Message" value="$${message}" />
 <KeyValuePair key="@LogLevel" value="$${level}" />

and

 <KeyValuePair key="@Message" value="$${event:message}" />
 <KeyValuePair key="@LogLevel" value="$${event:level}" />

Any help would be appriciated i know i can just create my own map and pass it into the message with these fields but that isn't the desired outcome.任何帮助都会得到帮助,我知道我可以创建自己的地图并将其传递到带有这些字段的消息中,但这不是预期的结果。

In log4j-core-2.11.2 version you may need to this :在 log4j-core-2.11.2 版本中,您可能需要这样做:

put attribute ip into MDC:将属性ip放入 MDC:

MDC.put("ip","127.0.0.1");
<JsonLayout compact="true" eventEol="true">
    <KeyValuePair key="ip" value="$${ctx:ip}"/>
</JsonLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM