【问题标题】:Log4j2 JSON Layout: add custom date field in UTCLog4j2 JSON 布局:在 UTC 中添加自定义日期字段
【发布时间】:2018-08-17 14:14:11
【问题描述】:

Log4j2 支持JSON Layout,我在 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 格式输出日期?

【问题讨论】:

    标签: java log4j2


    【解决方案1】:

    您可以create your own lookup,因为我相信您已经知道,KeyValuePair 支持根据manual page you shared 在其值属性中查找。

    例如,我创建了一个使用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 的主要目标。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-15
      • 2022-11-25
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      相关资源
      最近更新 更多