【问题标题】:Fluent Bit Filter to convert Unix Epoch timestamp to human readable time formatFluent Bit Filter 将 Unix Epoch 时间戳转换为人类可读的时间格式
【发布时间】:2020-12-12 17:15:42
【问题描述】:

我有一个 Java 应用程序,我在其中使用 Log4j2 在 JSONLayout 中打印日志,这里是日志格式的示例:

{
    "thread": "TopicStatusThreadPool-thread-1",
    "level": "INFO",
    "loggerName": "My Kafka Logger",
    "message": "Topic Status List is empty, returning from summarize method",
    "endOfBatch": false,
    "loggerFqcn": "org.apache.logging.slf4j.Log4jLogger",
    "instant": {
        "epochSecond": 1587980988,
        "nanoOfSecond": 214241000
    },
    "threadId": 37,
    "threadPriority": 5
  }

以这种格式打印的日志随后被 Fluent Bit 提取并推送到 Elasticsearch。我正在使用 Kibana 可视化日志,但是当我看到日志和时间为 epochSecondnonOfSecond 时,很难将其与实际应用程序日志关联起来,因为格式。

是否有任何 Fluent Bit Filter 可用于将这种时间格式修改为更易于阅读的格式。

目前我在 Fluent Bit 配置中使用 基本 JSON 解析器和 Kubernetes 过滤器 将 Kubernetes 信息添加到日志消息中。

更新:

我对 Log4j2 配置进行了更改,现在我得到了 timeMillis 字段,它在日志中以毫秒为单位打印时间。

{
    "thread": "TopicStatusThreadPool-thread-1",
    "level": "INFO",
    "loggerName": "My Kafka Logger",
    "message": "Topic Status List is empty, returning from summarize method",
    "endOfBatch": false,
    "loggerFqcn": "org.apache.logging.slf4j.Log4jLogger",
    "timeMillis": 1587980988213,
    "threadId": 37,
    "threadPriority": 5
  }

将它转换为人类可读格式的 lua 过滤器是什么。默认情况下,Fluent Bit 中的时间转换不支持以毫秒为单位的时间,它需要以秒为单位的时间。

我试过了:

[PARSER]
        Name   json
        Format json
        Time_Key timeMillis
        Time_Format %s
        Time_Keep On

但这并没有选择毫秒部分,以秒为单位处理时间。

【问题讨论】:

    标签: elasticsearch log4j2 fluent-bit efk


    【解决方案1】:

    你可以按如下方式使用 lua 过滤器:

    -- test.lua
    function append_converted_timestamp(tag, timestamp, record)
        new_record = record
        new_record["instant"]["recorded_time"] = os.date("%m/%d/%Y %I:%M:%S %p", record["instant"]["epochSecond"])
        return 2, timestamp, new_record
    end
    

    FluentBit 配置:

    [FILTER]
        Name    lua
        Match   *
        script  test.lua
        call    append_converted_timestamp
    

    它将在您的记录中附加一个带有人类可读日期的新字段 recorded_time

    更新

    timeMillis 字段的 Lua 函数可以这样实现:

    -- test.lua
    function append_converted_timestamp(tag, timestamp, record)
        new_record = record
        new_record["recorded_time"] = os.date("%m/%d/%Y %I:%M:%S %p", record["timeMillis"]/1000)
        return 2, timestamp, new_record
    end
    

    【讨论】:

    • 您好,感谢您的回答。您能否更新字段 timeMillis 的 lua 过滤器,我该如何处理?
    • 我猜你可能只是将 lua 函数中的 timeMillis 划分为 1000 以获得秒数。像这样的东西:new_record["recorded_time"] = os.date("%m/%d/%Y %I:%M:%S %p", record["timeMillis"]/1000)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2014-05-26
    • 2010-12-11
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多