ECS 记录器是你最喜欢的日志库的格式化程序/编码器插件。它们可让你轻松地将日志格式化为与 ECS 兼容的 JSON。
编码器以 JSON 格式记录,内部依赖于默认的 logrus.JSONFormatter。它还处理 ECS 错误格式的错误字段记录。
默认情况下,会添加以下字段:
1. {
2. "log.level": "info",
3. "@timestamp": "2020-09-13T10:48:03.000Z",
4. "message":" some logging info",
5. "ecs.version": "1.6.0"
6. }
安装
将包添加到你的 go.mod 文件中:
1. module zerolog-logging
2. require go.elastic.co/ecslogrus master
我们使用如下的命令来下载包:
配置
设置默认记录器。例如:
1. log := logrus.New()
2. log.SetFormatter(&ecslogrus.Formatter{})
示例
使用结构化日志记录
logrus_structrued.go
1. package main
3. import (
4. "errors"
6. "github.com/sirupsen/logrus"
7. "go.elastic.co/ecslogrus"
8. )
10. func main() {
11. log := logrus.New()
12. log.SetFormatter(&ecslogrus.Formatter{})
14. // Add custom fields.
15. log.WithError(errors.New("boom!")).WithField("custom", "foo").Info("hello")
16. }
我们使用如下的命令来运行应用:
go run logrus_structrued.go
1. $ go run logrus_structrued.go
2. {"@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
1. package main
3. import (
4. "errors"
6. "github.com/sirupsen/logrus"
7. "go.elastic.co/ecslogrus"
8. )
10. func main() {
11. log := logrus.New()
12. log.SetFormatter(&ecslogrus.Formatter{
13. DataKey: "labels",
14. })
15. log.WithError(errors.New("boom!")).WithField("custom", "foo").Info("hello")
16. }
我们使用如下的命令来运行应用:
go run logrus_custom.go
1. $ go run logrus_custom.go
2. {"@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
1. package main
3. import (
4. "errors"
6. "github.com/sirupsen/logrus"
7. "go.elastic.co/ecslogrus"
8. )
10. func main() {
11. log := logrus.New()
12. log.SetFormatter(&ecslogrus.Formatter{})
13. log.ReportCaller = true
14. log.WithError(errors.New("boom!")).WithField("custom", "foo").Info("hello")
15. }
我们使用如下的命令来运行应用:
go run logrus_caller.go
1. $ go run logrus_caller.go
2. {"@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
- 按照 Filebeat 快速入门
- 将以下配置添加到你的 filebeat.yaml 文件中。
Filebeat 7.16+
filebeat.yaml
1. filebeat.inputs:
2. - type: filestream # 1
3. paths: /path/to/logs.json
4. parsers:
5. - ndjson:
6. overwrite_keys: true # 2
7. add_error_key: true # 3
8. expand_keys: true # 4
10. processors: # 5
11. - add_host_metadata: ~
12. - add_cloud_metadata: ~
13. - add_docker_metadata: ~
14. - add_kubernetes_metadata: ~
- 使用 filestream 输入从活动日志文件中读取行。
- 如果发生冲突,解码的 JSON 对象的值将覆盖 Filebeat 通常添加的字段(type、source、offset 等)。
- 如果发生 JSON 解组错误,Filebeat 将添加 “error.message” 和 “error.type: json” 键。
- Filebeat 将递归地从解码的 JSON 中去掉点键,并将其扩展为分层对象结构。
- Processors 可增强你的数据。请参阅 processors 以了解更多信息。
Filebeat < 7.16
filebeat.yaml
1. filebeat.inputs:
2. - type: log
3. paths: /path/to/logs.json
4. json.keys_under_root: true
5. json.overwrite_keys: true
6. json.add_error_key: true
7. json.expand_keys: true
9. processors:
10. - add_host_metadata: ~
11. - add_cloud_metadata: ~
12. - add_docker_metadata: ~
13. - add_kubernetes_metadata: ~