Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate Slog into logx #327

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions logx/slogx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
### Quick Start

Prerequesites:
JiChenSSG marked this conversation as resolved.
Show resolved Hide resolved

go version required `1.21`

- Install `go-zero`:

```console
go get -u github.com/zeromicro/go-zero
```

Download the module:

```console
go get -u github.com/zeromicro/zero-contrib/logx/slogx
```

For example:

```go
package main

import (
"context"
"log/slog"
"os"
"testing"
"time"

"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/zero-contrib/logx/slogx"
)

func main() {
h := slog.NewJSONHandler(os.Stdout, nil)
writer := slogx.NewSlogWriter(h)
logx.SetWriter(writer)

logx.Infow("infow foo",
logx.Field("url", "http://localhost:8080/hello"),
logx.Field("attempt", 3),
logx.Field("backoff", time.Second),
)
logx.Errorw("errorw foo",
logx.Field("url", "http://localhost:8080/hello"),
logx.Field("attempt", 3),
logx.Field("backoff", time.Second),
)
logx.Sloww("sloww foo",
logx.Field("url", "http://localhost:8080/hello"),
logx.Field("attempt", 3),
logx.Field("backoff", time.Second),
)
logx.Error("error")
logx.Infov(map[string]interface{}{
"url": "localhost:8080/hello",
"attempt": 3,
"backoff": time.Second,
"value": "foo",
})
logx.WithDuration(1100*time.Microsecond).Infow("infow withduration",
logx.Field("url", "localhost:8080/hello"),
logx.Field("attempt", 3),
logx.Field("backoff", time.Second),
)
logx.WithContext(context.Background()).WithDuration(1100*time.Microsecond).Errorw(
"errorw withcontext withduration",
logx.Field("url", "localhost:8080/hello"),
logx.Field("attempt", 3),
logx.Field("backoff", time.Second),
)
logx.WithDuration(1100*time.Microsecond).WithContext(context.Background()).Errorw(
"errorw withduration withcontext",
logx.Field("url", "localhost:8080/hello"),
logx.Field("attempt", 3),
logx.Field("backoff", time.Second),
)
}
```
Loading