From e10578a2289d47d9ae721759d98230a1bc7c1bb4 Mon Sep 17 00:00:00 2001 From: Peter Van Bouwel Date: Fri, 31 May 2024 14:56:25 +0200 Subject: [PATCH] working: traefik v2.10.1 + zap v1.24.0 --- README.md | 3 ++- src/yaegi_zap_issue_demo/go.mod | 6 +++++ src/yaegi_zap_issue_demo/go.sum | 15 +++++++++++ src/yaegi_zap_issue_demo/issue.go | 45 ++++++++++++++++++++++++++++--- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/yaegi_zap_issue_demo/go.sum diff --git a/README.md b/README.md index cb3b545..98960c4 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,5 @@ Just a demo to illustrate issues experienced with zap when using inside a traffi ## Repro 1. Start whoami container which serves as backend service `podman run --name whoami -d --rm -p 8080:8888 -p 8081:80 traefik/whoami` -2. Start traefik container with plugin: `podman run -it --rm -v $PWD/traefik.yaml:/etc/traefik.yaml -v $PWD/src:/plugins-local/src --network container:whoami docker.io/traefik:v2.10.1 --configfile=/etc/traefik.yaml` \ No newline at end of file +2. Start traefik container with plugin: `podman run -it --rm -v $PWD/traefik.yaml:/etc/traefik.yaml -v $PWD/src:/plugins-local/src --network container:whoami docker.io/traefik:v2.10.1 --configfile=/etc/traefik.yaml` +3. In another terminal window `curl http://localhost:8080/demo` \ No newline at end of file diff --git a/src/yaegi_zap_issue_demo/go.mod b/src/yaegi_zap_issue_demo/go.mod index 7492a28..93961d0 100644 --- a/src/yaegi_zap_issue_demo/go.mod +++ b/src/yaegi_zap_issue_demo/go.mod @@ -1,3 +1,9 @@ module github.com/pvbouwel/yaegi_zap_issue_demo go 1.22.2 + +require ( + go.uber.org/atomic v1.7.0 // indirect + go.uber.org/multierr v1.10.0 // indirect + go.uber.org/zap v1.24.0 // indirect +) diff --git a/src/yaegi_zap_issue_demo/go.sum b/src/yaegi_zap_issue_demo/go.sum new file mode 100644 index 0000000..5883175 --- /dev/null +++ b/src/yaegi_zap_issue_demo/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= +go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= +go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= +go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= diff --git a/src/yaegi_zap_issue_demo/issue.go b/src/yaegi_zap_issue_demo/issue.go index 4ecec61..fc8fd0b 100644 --- a/src/yaegi_zap_issue_demo/issue.go +++ b/src/yaegi_zap_issue_demo/issue.go @@ -3,6 +3,10 @@ package yaegi_zap_issue_demo import ( "context" "net/http" + "time" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) type Config struct { @@ -14,17 +18,50 @@ func CreateConfig() *Config { } type issuePlugin struct { - name string - next http.Handler + name string + next http.Handler + logger *zap.Logger } func (p *issuePlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + p.logger.Info("test") p.next.ServeHTTP(rw, req) } func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) { + logger, _ := NewLogger("INFO") return &issuePlugin{ - name: name, - next: next, + name: name, + next: next, + logger: logger, }, nil } + +func NewLogger(logLevel string) (*zap.Logger, error) { + config := zap.Config{ + Encoding: "json", + OutputPaths: []string{"stdout"}, + ErrorOutputPaths: []string{"stdout"}, + EncoderConfig: zapcore.EncoderConfig{ + TimeKey: "time", + EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.UTC().Format("2006-01-02T15:04:05Z")) + }, + LevelKey: "level", + NameKey: "logger", + MessageKey: "message", + StacktraceKey: "stacktrace", + LineEnding: zapcore.DefaultLineEnding, + EncodeLevel: zapcore.LowercaseLevelEncoder, + EncodeCaller: zapcore.ShortCallerEncoder, + }, + } + + var level zapcore.Level + err := level.UnmarshalText([]byte(logLevel)) + if err != nil { + panic(err) + } + config.Level = zap.NewAtomicLevelAt(level) + return config.Build() +} \ No newline at end of file