Skip to content

Commit 267e81b

Browse files
Merge pull request #18 from panz3r/migrate-logger
Migrate logging to `slog`
2 parents c50df8b + d623916 commit 267e81b

6 files changed

Lines changed: 415 additions & 45 deletions

File tree

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v3
1818
with:
19-
go-version: 1.19.1
19+
go-version: 1.24.3
2020

2121
- name: Get coverage tool
2222
run: |

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
FROM golang:1.18.2-alpine3.16 as builder
1+
FROM golang:1.24-alpine3.21 AS builder
22

33
WORKDIR /code/
44

55
ADD src/ .
66

77
RUN go build -o dist/ -ldflags "-s -w"
88

9-
FROM alpine:3.16
9+
FROM alpine:3.21
10+
1011
WORKDIR /code/
1112

1213
COPY docker-entrypoint.sh /bin/
@@ -15,4 +16,4 @@ ENTRYPOINT ["/bin/docker-entrypoint.sh"]
1516
COPY --from=builder /code/dist/go-http-server /bin/
1617
RUN chmod +x /bin/go-http-server
1718

18-
CMD "/bin/go-http-server"
19+
CMD ["/bin/go-http-server"]

src/go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
module go-http-server
22

3-
go 1.18
3+
go 1.24.3
44

55
require (
66
bou.ke/monkey v1.0.2
77
github.com/andybalholm/brotli v1.0.4
88
github.com/felixge/httpsnoop v1.0.3
99
github.com/hashicorp/golang-lru v0.5.4
10-
github.com/rs/zerolog v1.29.1
1110
github.com/urfave/cli/v2 v2.16.3
1211
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
1312
)
1413

1514
require (
1615
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
17-
github.com/mattn/go-colorable v0.1.13 // indirect
18-
github.com/mattn/go-isatty v0.0.19 // indirect
1916
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2017
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
21-
golang.org/x/sys v0.10.0 // indirect
2218
)

src/go.sum

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,12 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
22
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
33
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
44
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
5-
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
65
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
76
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
87
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
98
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
10-
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
119
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
1210
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
13-
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
14-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
15-
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
16-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
17-
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
18-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
19-
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
20-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
21-
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
22-
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
23-
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
2411
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
2512
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
2613
github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk=
@@ -29,9 +16,3 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
2916
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
3017
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
3118
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
32-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33-
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35-
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
36-
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
37-
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

src/util/log.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package util
22

33
import (
4+
"log/slog"
45
"net"
56
"net/http"
67
"os"
78
"time"
89

910
"github.com/felixge/httpsnoop"
10-
"github.com/rs/zerolog"
11-
"github.com/rs/zerolog/log"
1211
)
1312

1413
type LogRequestHandlerOptions struct {
@@ -35,31 +34,32 @@ type HTTPReqInfo struct {
3534
referer string
3635
}
3736

38-
func logHTTPReqInfo(ri *HTTPReqInfo) {
39-
log.Info().
40-
Str("method", ri.method).
41-
Str("path", ri.path).
42-
Int("code", ri.code).
43-
Int64("size", ri.size).
44-
Dur("duration", ri.duration).
45-
IPAddr("ipAddress", ri.ipAddress).
46-
Str("userAgent", ri.userAgent).
47-
Str("referer", ri.referer).
48-
Send()
37+
func logHTTPReqInfo(l *slog.Logger, ri *HTTPReqInfo) {
38+
l.Info("HTTP Request",
39+
"method", ri.method,
40+
"path", ri.path,
41+
slog.Int("code", ri.code),
42+
slog.Int64("size", ri.size),
43+
slog.Int64("duration", ri.duration.Milliseconds()), // in milliseconds
44+
"ipAddress", ri.ipAddress,
45+
"userAgent", ri.userAgent,
46+
"referer", ri.referer,
47+
)
4948
}
5049

5150
func LogRequestHandler(h http.Handler, opt *LogRequestHandlerOptions) http.Handler {
51+
var logger *slog.Logger
5252
if opt.Pretty {
53-
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
53+
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
54+
} else {
55+
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
5456
}
5557

56-
zerolog.DurationFieldUnit = time.Millisecond
57-
5858
fn := func(w http.ResponseWriter, r *http.Request) {
5959
// runs handler h and captures information about HTTP request
6060
mtr := httpsnoop.CaptureMetrics(h, w, r)
6161

62-
logHTTPReqInfo(&HTTPReqInfo{
62+
logHTTPReqInfo(logger, &HTTPReqInfo{
6363
method: r.Method,
6464
path: r.URL.String(),
6565
code: mtr.Code,

0 commit comments

Comments
 (0)