繁体   English   中英

Log4j2 JSON布局:在UTC中添加自定义日期字段

[英]Log4j2 JSON Layout: add custom date field in UTC

Log4j2支持JSON布局 ,我在log4j2.xml中添加了一个额外的自定义字段:

<JsonLayout compact="true" eventEol="true" stacktraceAsString="true">
    <KeyValuePair key="@timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}"/>
</JsonLayout>

通常,一切正常,但是此日志由Filebeats处理,并且假定该日期以UTC表示。

所有日志条目的日期值都在本地时区。

是否可以通过某种方式在UTC中输出日期?

您可以创建自己的查找 ,因为您已经知道, KeyValuePair您共享手册页的 value属性中支持查找。

出于示例目的,我创建了一个使用System.currentTimeMillis()的简单查找。

这是示例代码:

首先,查找类:

package utcTime;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;

@Plugin(name = "UtcMillis", category = "Lookup")
public class UtcMillisLookup implements StrLookup{
    /**
     * Lookup the value for the key.
     * @param key  the key to be looked up, may be null
     * @return The value for the key.
     */
    public String lookup(String key) {
        return String.valueOf(getUTCMillis());
    }

    /**
     * @return current UTC time in milliseconds
     */
    private long getUTCMillis(){
        return System.currentTimeMillis();
    }

    /**
     * Lookup the value for the key using the data in the LogEvent.
     * @param event The current LogEvent.
     * @param key  the key to be looked up, may be null
     * @return The value associated with the key.
     */
    public String lookup(LogEvent event, String key) {
        return String.valueOf(getUTCMillis());
    }
}

接下来,log4j2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <JsonLayout compact="true" eventEol="true" stacktraceAsString="true">
                <KeyValuePair key="@timestamp" value="$${UtcMillis:}"/>
            </JsonLayout>
        </Console>

    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

需要注意的是,查找没有参数/键,以便你可以离开的那部分空白/空,但你仍然必须使用冒号( : ),这就是为什么你看到$${UtcMillis:}在上面的配置。

最后,一个简单的类生成一个日志事件:

package utcTime;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MainUtcLookup {
    private static final Logger log = LogManager.getLogger();
    public static void main(String[] args){
        log.info("Here's some info!");
    }
}

这是示例输出:

{  
   "thread":"main",
   "level":"INFO",
   "loggerName":"utcTime.MainUtcLookup",
   "message":"Here's some info!",
   "endOfBatch":false,
   "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
   "instant":{  
      "epochSecond":1534642997,
      "nanoOfSecond":556000000
   },
   "threadId":1,
   "threadPriority":5,
   "@timestamp":"1534642997558"
}

我不会深入研究以UTC毫秒为单位的当前时间的所有不同方式的细节,因为我敢肯定您可以自己研究细节。 我只是想提供一个示例,说明如何实现将毫秒级时间戳添加到log4j2 JSONLayout主要目标。

希望这可以帮助!

暂无
暂无

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

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