Skip to content

Commit

Permalink
Add zerolog example (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-u410 authored Dec 23, 2023
1 parent 8204d74 commit 59557b4
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,11 @@ Some loggers may need a custom formatter to extract attributes from `oops.OopsEr
Available loggers:
- log: [playground](https://go.dev/play/p/uNx3CcT-X40) - [example](https://github.com/samber/oops/tree/master/examples/log)
- slog: [playground](https://go.dev/play/p/-X2ZnqjyDLu) - [example](https://github.com/samber/oops/examples/slog)
- zerolog: [playground](https://go.dev/play/p/DaHzR4Zc-jj) - [example](https://github.com/samber/oops/examples/zerolog)
- logrus: [formatter](https://github.com/samber/oops/tree/master/loggers/logrus) - [playground](https://go.dev/play/p/-_7EBnceJ_A) - [example](https://github.com/samber/oops/tree/master/examples/logrus)

We are looking for contributions and examples for:
- zap
- zerolog
- go-sentry
- other?

Expand Down
9 changes: 9 additions & 0 deletions examples/zerolog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# Example of Zerolog logger

Playground: https://go.dev/play/p/DaHzR4Zc-jj

```sh
go run examples/zerolog/example.go 2>&1 | jq
go run examples/zerolog/example.go 2>&1 | jq .stacktrace -r
```
64 changes: 64 additions & 0 deletions examples/zerolog/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"os"
"time"

"github.com/rs/zerolog"
"github.com/samber/oops"
)

func d() error {
return oops.
Code("iam_authz_missing_permission").
In("authz").
Time(time.Now()).
With("user_id", 1234).
With("permission", "post.create").
Hint("Runbook: https://doc.acme.org/doc/abcd.md").
User("user-123", "firstname", "john", "lastname", "doe").
Errorf("permission denied")
}

func c() error {
return d()
}

func b() error {
return oops.
In("iam").
Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9").
With("hello", "world").
Wrapf(c(), "something failed")
}

func a() error {
return b()
}

func WithOops(l zerolog.Logger, err error) *zerolog.Logger {
if oopsErr, ok := oops.AsOops(err); ok {
ctx := l.With()
for k, v := range oopsErr.ToMap() {
ctx = ctx.Interface(k, v)
}

logger := ctx.Err(oopsErr).Logger()
return &logger
}

// Recursively call into ourself so we can at least get a stack trace for any error
return WithOops(l, oops.Wrap(err))
}

func main() {
logger := zerolog.
New(os.Stderr).
With().
Timestamp().
Logger()

err := a()

WithOops(logger, err).Error().Send()
}
17 changes: 17 additions & 0 deletions examples/zerolog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/samber/oops/examples/zerolog

go 1.21

require (
github.com/rs/zerolog v1.31.0
github.com/samber/oops v1.9.2
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/samber/lo v1.38.1 // indirect
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 // indirect
golang.org/x/sys v0.12.0 // indirect
)
24 changes: 24 additions & 0 deletions examples/zerolog/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/oops v1.9.2 h1:ph8WaOk5pEMetbtp+UTcdrdaggk8E8gKhpunShAKyIM=
github.com/samber/oops v1.9.2/go.mod h1:xEXk4BLyqajkvCxzpxBnqfzHzRLFk3+g4E+tOJtOPZY=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ use (
./examples/sources
./examples/panic
./examples/segfault
./examples/zerolog
)

0 comments on commit 59557b4

Please sign in to comment.