From 71e45cd6166a0df35c132a9c04ff4b786f0376aa Mon Sep 17 00:00:00 2001 From: Sergey Vilgelm <523825+SVilgelm@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:48:40 +0200 Subject: [PATCH] use go 1.23 (#41) --- .github/workflows/checks.yaml | 2 +- .github/workflows/release.yaml | 2 +- go.mod | 4 +-- go.sum | 4 +-- log.go | 47 ++++++++++------------------------ 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index f44be96..be3b735 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -15,7 +15,7 @@ concurrency: cancel-in-progress: true env: - GO: "1.20" + GO: "1.23" jobs: CodeQL: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 3bac731..3523155 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -9,7 +9,7 @@ permissions: contents: write env: - GO: "1.20" + GO: "1.23" jobs: release-cli: diff --git a/go.mod b/go.mod index d6693d9..729895c 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/sv-tools/mock-http-server -go 1.20 +go 1.23.0 require ( github.com/go-chi/chi/v5 v5.1.0 github.com/spf13/pflag v1.0.5 - golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 + golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index c739e5e..51f6889 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6ZucTZatCCEm9D47sZ2zo= -golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA= +golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/log.go b/log.go index 8729f6b..c27936a 100644 --- a/log.go +++ b/log.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "net/http" "os" @@ -12,7 +13,7 @@ import ( func LogHandler() *slog.JSONHandler { // Setup a JSON handler for the new log/slog library - return slog.HandlerOptions{ + return slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ // Remove default time slog.Attr, we create our own later ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.TimeKey { @@ -20,7 +21,7 @@ func LogHandler() *slog.JSONHandler { } return a }, - }.NewJSONHandler(os.Stdout) + }) } // StructuredLogger is a simple, but powerful implementation of a custom structured @@ -37,6 +38,8 @@ type StructuredLogger struct { } func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry { + ctx := r.Context() + var logFields []slog.Attr logFields = append(logFields, slog.String("ts", time.Now().UTC().Format(time.RFC1123))) @@ -58,54 +61,32 @@ func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry { slog.String("uri", fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)), )) - entry := StructuredLoggerEntry{Logger: slog.New(handler)} + entry := StructuredLoggerEntry{ + ctx: ctx, + Logger: slog.New(handler), + } - entry.Logger.LogAttrs(slog.LevelInfo, "request started") + entry.Logger.LogAttrs(ctx, slog.LevelInfo, "request started") return &entry } type StructuredLoggerEntry struct { + ctx context.Context Logger *slog.Logger } func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) { - l.Logger.LogAttrs(slog.LevelInfo, "request complete", + l.Logger.LogAttrs(l.ctx, slog.LevelInfo, "request complete", slog.Int("resp_status", status), slog.Int("resp_byte_length", bytes), slog.Float64("resp_elapsed_ms", float64(elapsed.Nanoseconds())/1000000.0), ) } -func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) { - l.Logger.LogAttrs(slog.LevelInfo, "", +func (l *StructuredLoggerEntry) Panic(v any, stack []byte) { + l.Logger.LogAttrs(l.ctx, slog.LevelInfo, "", slog.String("stack", string(stack)), slog.String("panic", fmt.Sprintf("%+v", v)), ) } - -// Helper methods used by the application to get the request-scoped -// logger entry and set additional fields between handlers. -// -// This is a useful pattern to use to set state on the entry as it -// passes through the handler chain, which at any point can be logged -// with a call to .Print(), .Info(), etc. - -func GetLogEntry(r *http.Request) *slog.Logger { - entry := middleware.GetLogEntry(r).(*StructuredLoggerEntry) - return entry.Logger -} - -func LogEntrySetField(r *http.Request, key string, value interface{}) { - if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok { - entry.Logger = entry.Logger.With(key, value) - } -} - -func LogEntrySetFields(r *http.Request, fields map[string]interface{}) { - if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok { - for k, v := range fields { - entry.Logger = entry.Logger.With(k, v) - } - } -}