Elasticsearch:Golang ECS 日志记录 - Logrus

ECS 记录器是你最喜欢的日志库的格式化程序/编码器插件。它们可让你轻松地将日志格式化为与 ECS 兼容的 JSON。

编码器以 JSON 格式记录,内部依赖于默认的 logrus.JSONFormatter。它还处理 ECS 错误格式的错误字段记录。

默认情况下,会添加以下字段:

复制代码
{
  "log.level": "info",
  "@timestamp": "2020-09-13T10:48:03.000Z",
  "message":" some logging info",
  "ecs.version": "1.6.0"
}

安装

将包添加到你的 go.mod 文件中:

复制代码
module zerolog-logging
require go.elastic.co/ecslogrus master

我们使用如下的命令来下载包:

配置

设置默认记录器。例如:

复制代码
log := logrus.New()
log.SetFormatter(&ecslogrus.Formatter{})

示例

使用结构化日志记录

logrus_structrued.go

复制代码
package main

import (
	"errors"

	"github.com/sirupsen/logrus"
	"go.elastic.co/ecslogrus"
)

func main() {
	log := logrus.New()
	log.SetFormatter(&ecslogrus.Formatter{})

	// Add custom fields.
	log.WithError(errors.New("boom!")).WithField("custom", "foo").Info("hello")
}

我们使用如下的命令来运行应用:

复制代码
go run logrus_structrued.go 

$ go run logrus_structrued.go 
{"@timestamp":"2024-07-23T15:50:15.202+0800","custom":"foo","ecs.version":"1.6.0","error":{"message":"boom!"},"log.level":"info","message":"hello"}

将自定义字段嵌套在 "label" 下

为了完全符合 ECS 要求,自定义字段应嵌套在 "label" 对象中。

logrus_custom.go

复制代码
package main

import (
	"errors"

	"github.com/sirupsen/logrus"
	"go.elastic.co/ecslogrus"
)

func main() {
	log := logrus.New()
	log.SetFormatter(&ecslogrus.Formatter{
		DataKey: "labels",
	})
	log.WithError(errors.New("boom!")).WithField("custom", "foo").Info("hello")
}

我们使用如下的命令来运行应用:

复制代码
go run logrus_custom.go

$ go run logrus_custom.go
{"@timestamp":"2024-07-23T15:53:35.311+0800","ecs.version":"1.6.0","error":{"message":"boom!"},"labels":{"custom":"foo"},"log.level":"info","message":"hello"}

报告调用者信息

logrus_caller.go

复制代码
package main

import (
	"errors"

	"github.com/sirupsen/logrus"
	"go.elastic.co/ecslogrus"
)

func main() {
	log := logrus.New()
	log.SetFormatter(&ecslogrus.Formatter{})
	log.ReportCaller = true
	log.WithError(errors.New("boom!")).WithField("custom", "foo").Info("hello")
}

我们使用如下的命令来运行应用:

复制代码
go run logrus_caller.go 

$ go run logrus_caller.go 
{"@timestamp":"2024-07-23T15:56:47.832+0800","custom":"foo","ecs.version":"1.6.0","error":{"message":"boom!"},"log.level":"info","log.origin.file.line":14,"log.origin.file.name":"/Users/liuxg/go/go-logging-logrus/logrus_caller.go","log.origin.function":"main.main","message":"hello"}

把日志写入到 Elasticsearch

  1. 按照 Filebeat 快速入门
  2. 将以下配置添加到你的 filebeat.yaml 文件中。

Filebeat 7.16+

filebeat.yaml

复制代码
filebeat.inputs:
- type: filestream  # 1
  paths: /path/to/logs.json
  parsers:
    - ndjson:
      overwrite_keys: true # 2
      add_error_key: true  # 3
      expand_keys: true    # 4

processors: # 5
  - add_host_metadata: ~
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~
  1. 使用 filestream 输入从活动日志文件中读取行。
  2. 如果发生冲突,解码的 JSON 对象的值将覆盖 Filebeat 通常添加的字段(type、source、offset 等)。
  3. 如果发生 JSON 解组错误,Filebeat 将添加 "error.message" 和 "error.type: json" 键。
  4. Filebeat 将递归地从解码的 JSON 中去掉点键,并将其扩展为分层对象结构。
  5. Processors 可增强你的数据。请参阅 processors 以了解更多信息。

Filebeat < 7.16

filebeat.yaml

复制代码
filebeat.inputs:
- type: log
  paths: /path/to/logs.json
  json.keys_under_root: true
  json.overwrite_keys: true
  json.add_error_key: true
  json.expand_keys: true

processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
相关推荐
洛森唛16 小时前
Elasticsearch DSL 查询语法大全:从入门到精通
后端·elasticsearch
字节跳动数据平台20 小时前
代码量减少 70%、GPU 利用率达 95%:火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
得物技术1 天前
深入剖析Spark UI界面:参数与界面详解|得物技术
大数据·后端·spark
武子康1 天前
大数据-238 离线数仓 - 广告业务 Hive分析实战:ADS 点击率、购买率与 Top100 排名避坑
大数据·后端·apache hive
武子康2 天前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天2 天前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
Elasticsearch3 天前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
Elasticsearch4 天前
通用表达式语言 ( CEL ): CEL 输入如何改进 Elastic Agent 集成中的数据收集
elasticsearch
武子康4 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康5 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive