-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
1 deletion.
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
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 | ||
``` |
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,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() | ||
} |
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,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 | ||
) |
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,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= |
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 |
---|---|---|
|
@@ -16,4 +16,5 @@ use ( | |
./examples/sources | ||
./examples/panic | ||
./examples/segfault | ||
./examples/zerolog | ||
) |