Skip to content

Migrate logging to slog #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19.1
go-version: 1.24.3

- name: Get coverage tool
run: |
Expand Down
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM golang:1.18.2-alpine3.16 as builder
FROM golang:1.24-alpine3.21 AS builder

WORKDIR /code/

ADD src/ .

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

FROM alpine:3.16
FROM alpine:3.21

WORKDIR /code/

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

CMD "/bin/go-http-server"
CMD ["/bin/go-http-server"]
6 changes: 1 addition & 5 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
module go-http-server

go 1.18
go 1.24.3

require (
bou.ke/monkey v1.0.2
github.com/andybalholm/brotli v1.0.4
github.com/felixge/httpsnoop v1.0.3
github.com/hashicorp/golang-lru v0.5.4
github.com/rs/zerolog v1.29.1
github.com/urfave/cli/v2 v2.16.3
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.10.0 // indirect
)
19 changes: 0 additions & 19 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
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.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
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/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk=
Expand All @@ -29,9 +16,3 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34 changes: 17 additions & 17 deletions src/util/log.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package util

import (
"log/slog"
"net"
"net/http"
"os"
"time"

"github.com/felixge/httpsnoop"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

type LogRequestHandlerOptions struct {
Expand All @@ -35,31 +34,32 @@ type HTTPReqInfo struct {
referer string
}

func logHTTPReqInfo(ri *HTTPReqInfo) {
log.Info().
Str("method", ri.method).
Str("path", ri.path).
Int("code", ri.code).
Int64("size", ri.size).
Dur("duration", ri.duration).
IPAddr("ipAddress", ri.ipAddress).
Str("userAgent", ri.userAgent).
Str("referer", ri.referer).
Send()
func logHTTPReqInfo(l *slog.Logger, ri *HTTPReqInfo) {
l.Info("HTTP Request",
"method", ri.method,
"path", ri.path,
slog.Int("code", ri.code),
slog.Int64("size", ri.size),
slog.Int64("duration", ri.duration.Milliseconds()), // in milliseconds
"ipAddress", ri.ipAddress,
"userAgent", ri.userAgent,
"referer", ri.referer,
)
}

func LogRequestHandler(h http.Handler, opt *LogRequestHandlerOptions) http.Handler {
var logger *slog.Logger
if opt.Pretty {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
} else {
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
}

zerolog.DurationFieldUnit = time.Millisecond

fn := func(w http.ResponseWriter, r *http.Request) {
// runs handler h and captures information about HTTP request
mtr := httpsnoop.CaptureMetrics(h, w, r)

logHTTPReqInfo(&HTTPReqInfo{
logHTTPReqInfo(logger, &HTTPReqInfo{
method: r.Method,
path: r.URL.String(),
code: mtr.Code,
Expand Down
Loading