hashicorp/go-hclog

GitHub: hashicorp/go-hclog

HashiCorp 出品的 Go 结构化日志库,提供键值对输出、多级别过滤、JSON 与人类可读双模式以及标准库 log 无缝桥接。

Stars: 348 | Forks: 48

# go-hclog [![Go 文档](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs] `go-hclog` 是一个 Go 包,提供简单的键/值日志记录 接口,适用于开发和生产环境。 与标准库 `log` 包不同,它提供日志级别,可根据 期望的输出量减少输出内容。 它通过 `hclog.Fmt()` 提供了 `Printf` 风格的值日志记录。 它提供了在开发中使用的人类可读输出模式,以及 在生产中使用的 JSON 输出模式。 ## 稳定性说明 该库已达到 1.0 稳定性。其 API 可以被认为是已经巩固的, 并承诺在未来的版本中保持不变。 ## 安装和文档 使用 `go get github.com/hashicorp/go-hclog` 安装。 完整文档可在以下地址获取: http://godoc.org/github.com/hashicorp/go-hclog ## 用法 ### 使用全局 logger ``` hclog.Default().Info("hello world") ``` ``` 2017-07-05T16:15:55.167-0700 [INFO ] hello world ``` (注意,为了简洁起见,在后续示例中移除了时间戳。) ### 创建新的 logger ``` appLogger := hclog.New(&hclog.LoggerOptions{ Name: "my-app", Level: hclog.LevelFromString("DEBUG"), }) ``` ### 输出包含 2 个键/值对的 Info 级别消息 ``` input := "5.5" _, err := strconv.ParseInt(input, 10, 32) if err != nil { appLogger.Info("Invalid input for ParseInt", "input", input, "error", err) } ``` ``` ... [INFO ] my-app: Invalid input for ParseInt: input=5.5 error="strconv.ParseInt: parsing "5.5": invalid syntax" ``` ### 为主要子系统创建新的 Logger ``` subsystemLogger := appLogger.Named("transport") subsystemLogger.Info("we are transporting something") ``` ``` ... [INFO ] my-app.transport: we are transporting something ``` 请注意,`subsystemLogger` 输出的日志包含 `my-app.transport`, 这反映了应用程序和子系统的名称。 ### 创建带有固定键/值对的新 Logger 使用 `With()` 将会在该 logger 输出的所有消息中包含特定的键值对。 ``` requestID := "5fb446b6-6eba-821d-df1b-cd7501b6a363" requestLogger := subsystemLogger.With("request", requestID) requestLogger.Info("we are transporting a request") ``` ``` ... [INFO ] my-app.transport: we are transporting a request: request=5fb446b6-6eba-821d-df1b-cd7501b6a363 ``` 这使得子 Logger 能够具有特定的上下文,而无需将其 传递给所有的调用方。 ### 使用 `hclog.Fmt()` ``` totalBandwidth := 200 appLogger.Info("total bandwidth exceeded", "bandwidth", hclog.Fmt("%d GB/s", totalBandwidth)) ``` ``` ... [INFO ] my-app: total bandwidth exceeded: bandwidth="200 GB/s" ``` ### 与使用标准库 logger 的代码配合使用 如果你想使用标准库的 `log.Logger` 接口,可以通过调用 `StandardLogger()` 方法来包装 `hclog.Logger`。这允许你使用 熟悉的 `Println()`、`Printf()` 等方法。例如: ``` stdLogger := appLogger.StandardLogger(&hclog.StandardLoggerOptions{ InferLevels: true, }) // Printf() is provided by stdlib log.Logger interface, not hclog.Logger stdLogger.Printf("[DEBUG] %+v", stdLogger) ``` ``` ... [DEBUG] my-app: &{mu:{state:0 sema:0} prefix: flag:0 out:0xc42000a0a0 buf:[]} ``` 或者,你可以配置系统范围的 logger: ``` // log the standard logger from 'import "log"' log.SetOutput(appLogger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true})) log.SetPrefix("") log.SetFlags(0) log.Printf("[DEBUG] %d", 42) ``` ``` ... [DEBUG] my-app: 42 ``` 请注意,如果 `appLogger` 初始化时使用了 `INFO` 日志级别,_并且_你 指定了 `InferLevels: true`,你在这里将看不到任何输出。你必须将 `appLogger` 更改为 `DEBUG` 才能看到输出。有关更多信息,请参阅文档。 如果日志行以时间戳开头,你可以使用 `InferLevelsWithTimestamp` 选项来尝试忽略它们。请注意, 要让 `InferLevelsWithTimestamp` 生效,必须将 `InferLevels` 设置为 `true`。
标签:EVTX分析, Go, Homebrew安装, Ruby工具, SOC Prime, 开发工具, 日志审计, 日志库, 组件库