-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Support zerolog * feat: Update README for zerolog usage
- Loading branch information
Showing
9 changed files
with
990 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Kitex zerolog (This is a community driven project) | ||
|
||
## Introduction | ||
|
||
This is a logger library that uses [zerolog](https://github.com/rs/zerolog) to implement the [Kitex logger interface](https://www.cloudwego.io/docs/kitex/tutorials/basic-feature/logging/), work together with kitex [obs-opentelemetry](https://github.com/kitex-contrib/obs-opentelemetry) | ||
|
||
## Usage | ||
|
||
Download and install it: | ||
|
||
```go | ||
go get github.com/kitex-contrib/obs-opentelemetry/logging/zerolog | ||
``` | ||
|
||
Import it in your code: | ||
|
||
```go | ||
import kitexzerolog github.com/kitex-contrib/obs-opentelemetry/logging/zerolog | ||
``` | ||
|
||
### Set logger impl | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/rs/zerolog/log" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
kitexzerolog "github.com/kitex-contrib/obs-opentelemetry/logging/zerolog" | ||
) | ||
|
||
func main() { | ||
logger := kitexzerolog.NewLogger() | ||
klog.SetLogger(logger) | ||
klog.SetLevel(klog.LevelDebug) | ||
|
||
// OR / AND using global logger | ||
log.Logger = *logger.Logger() | ||
} | ||
``` | ||
|
||
> We provide some methods to help you customize logger | ||
| Configuration | Description | | ||
| -------------------------- | ----------------------------------------------------------------------------- | | ||
| WithLogger | [Logger](https://pkg.go.dev/github.com/rs/zerolog#Logger) | | ||
| WithTraceErrorSpanLevel | trace error span level option | | ||
| WithRecordStackTraceInSpan | record stack track option | | ||
|
||
### Log with context | ||
|
||
```go | ||
// Echo implements the Echo interface. | ||
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) { | ||
klog.CtxDebugf(ctx, "echo called: %s", req.GetMessage()) | ||
return &api.Response{Message: req.Message}, nil | ||
} | ||
``` | ||
|
||
### Log with global logger | ||
|
||
In case your code base has already used global logger from zerolog | ||
|
||
```go | ||
import ( | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Echo implements the Echo interface. | ||
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) { | ||
log.Debug().Ctx(ctx).Msgf("echo called: %s", req.GetMessage()) | ||
return &api.Response{Message: req.Message}, nil | ||
} | ||
``` | ||
|
||
### View log | ||
|
||
```json | ||
{ | ||
"level": "info", | ||
"ts": 1667619647.1459548, | ||
"msg": "hello world", | ||
"trace_id": "c77e46c0fb590ee80b6d78ed6682768e", | ||
"span_id": "b42c96c6dd01ceaf", | ||
"trace_flags": "01" | ||
} | ||
``` | ||
|
||
> For some reason, zerolog will not log extra context info. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module github.com/kitex-contrib/obs-opentelemetry/logging/zerolog | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/cloudwego/kitex v0.7.3 | ||
github.com/rs/zerolog v1.31.0 | ||
go.opentelemetry.io/otel v1.19.0 | ||
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 | ||
go.opentelemetry.io/otel/sdk v1.19.0 | ||
go.opentelemetry.io/otel/trace v1.19.0 | ||
) | ||
|
||
require ( | ||
github.com/go-logr/logr v1.3.0 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
go.opentelemetry.io/otel/metric v1.19.0 // indirect | ||
golang.org/x/sys v0.13.0 // indirect | ||
) |
Oops, something went wrong.