Skip to content

Commit

Permalink
use go 1.23
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm committed Aug 26, 2024
1 parent 38c5727 commit 480209f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ concurrency:
cancel-in-progress: true

env:
GO: "1.20"
GO: "1.23"

jobs:
CodeQL:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:
contents: write

env:
GO: "1.20"
GO: "1.23"

jobs:
release-cli:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
44 changes: 11 additions & 33 deletions log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
Expand All @@ -12,15 +13,15 @@ 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 {
return slog.Attr{}
}
return a
},
}.NewJSONHandler(os.Stdout)
})
}

// StructuredLogger is a simple, but powerful implementation of a custom structured
Expand All @@ -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)))

Expand All @@ -58,54 +61,29 @@ 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{Logger: slog.New(handler), ctx: ctx}

entry.Logger.LogAttrs(slog.LevelInfo, "request started")
entry.Logger.LogAttrs(ctx, slog.LevelInfo, "request started")

return &entry
}

type StructuredLoggerEntry struct {
Logger *slog.Logger
ctx context.Context
}

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)
}
}
}

0 comments on commit 480209f

Please sign in to comment.