32 lines
857 B
Markdown
32 lines
857 B
Markdown
# logx
|
||
|
||
`logx` 是 GoOmni 的业务层结构化日志入口。业务代码不需要依赖 Gin:
|
||
|
||
```go
|
||
import "git.yuntuo.hk/k2safe/go-omni/logx"
|
||
|
||
func (s *UserService) Create(ctx context.Context, input CreateInput) error {
|
||
logx.InfoContext(ctx, "user.create.start", "email", input.Email)
|
||
// ...
|
||
return nil
|
||
}
|
||
```
|
||
|
||
应用启动时可以替换默认输出:
|
||
|
||
```go
|
||
closeLog, err := logx.Setup(logx.Config{
|
||
Service: "billing-api",
|
||
Environment: "production",
|
||
Level: "info",
|
||
Format: "json",
|
||
File: "./var/log/billing-api.log",
|
||
})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer closeLog()
|
||
```
|
||
|
||
HTTP 请求的 `X-Request-ID` 会被放进标准 `context.Context`,之后的 `InfoContext`、`ErrorContext` 会自动带上 `requestId`。后续可以在不改变业务调用方式的前提下增加文件轮转、采样和 OpenTelemetry 导出。
|