Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zzzzer91/zlog
https://github.com/zzzzer91/zlog
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/zzzzer91/zlog
- Owner: zzzzer91
- Created: 2022-09-28T07:59:24.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-03T03:01:01.000Z (about 1 year ago)
- Last Synced: 2024-04-17T05:53:29.747Z (7 months ago)
- Language: Go
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# zlog
## 功能
基于 logrus 的日志组件封装,支持打印错误栈、输出到文件和链路追踪。主要用于请求链路中的日志打印,基本的日志打印可以使用 [logx](https://github.com/zzzzer91/gopkg/tree/main/logx)。
## Usage
执行:
```go
package mainimport (
"context"
"os""github.com/pkg/errors"
"github.com/zzzzer91/zlog/trace"
"github.com/zzzzer91/zlog"
)func main() {
// 可选,加入链路追踪,然后可以在 jager 中看到日志
// zlog.SetLogger(zlog.NewLogger(conf.App.Log, trace.NewTraceHook()))
ctx := context.WithValue(context.Background(), zlog.EntityFieldNameRequestID, "abcdefghijk")
ctx = context.WithValue(ctx, zlog.EntityFieldNameTraceID, "123456789")
err := f(ctx)
if err != nil {
zlog.Ctx(ctx).WithError(err).Error("failed to execute f()")
}
}func f(ctx context.Context) error {
return errors.New("new error")
}
```输出:
```
{
"time": "2023-03-11T11:22:26.801+08",
"level": "error",
"caller": "main.main:17",
"msg": "failed to execute f()",
"error": "new error",
"extraFields": {
"errorStack": ""main.f:22\nmain.main:15\nruntime.main:250"",
"requestId": "abcdefghijk",
"traceId": "123456789"
}
}
```