From 80776a4032e2c6157ccff2364b4b5b6efc59fc7b Mon Sep 17 00:00:00 2001 From: Luke Roy Date: Tue, 22 Jul 2025 07:56:04 +0200 Subject: [PATCH] add logging example go with standard slog --- logging/go-zap/.ceignore | 1 + logging/go-zap/.dockerignore | 4 ++++ logging/go-zap/Dockerfile | 10 +++++++++ logging/go-zap/build | 15 +++++++++++++ logging/go-zap/go.mod | 10 +++++++++ logging/go-zap/go.sum | 14 ++++++++++++ logging/go-zap/main.go | 33 ++++++++++++++++++++++++++++ logging/go/go.mod | 7 ------ logging/go/go.sum | 14 ------------ logging/go/main.go | 42 +++++++++++++++++++++++------------- 10 files changed, 114 insertions(+), 36 deletions(-) create mode 100644 logging/go-zap/.ceignore create mode 100644 logging/go-zap/.dockerignore create mode 100644 logging/go-zap/Dockerfile create mode 100755 logging/go-zap/build create mode 100644 logging/go-zap/go.mod create mode 100644 logging/go-zap/go.sum create mode 100644 logging/go-zap/main.go diff --git a/logging/go-zap/.ceignore b/logging/go-zap/.ceignore new file mode 100644 index 00000000..5657f6ea --- /dev/null +++ b/logging/go-zap/.ceignore @@ -0,0 +1 @@ +vendor \ No newline at end of file diff --git a/logging/go-zap/.dockerignore b/logging/go-zap/.dockerignore new file mode 100644 index 00000000..210eadbc --- /dev/null +++ b/logging/go-zap/.dockerignore @@ -0,0 +1,4 @@ +.dockerignore +build +Dockerfile +vendor \ No newline at end of file diff --git a/logging/go-zap/Dockerfile b/logging/go-zap/Dockerfile new file mode 100644 index 00000000..3602d953 --- /dev/null +++ b/logging/go-zap/Dockerfile @@ -0,0 +1,10 @@ +FROM quay.io/projectquay/golang:1.23 AS build-env +WORKDIR /go/src/app +COPY . . + +RUN CGO_ENABLED=0 go build -o /go/bin/app main.go + +# Copy the executable into a smaller base image +FROM gcr.io/distroless/static-debian12 +COPY --from=build-env /go/bin/app / +ENTRYPOINT ["/app"] diff --git a/logging/go-zap/build b/logging/go-zap/build new file mode 100755 index 00000000..bb863eb9 --- /dev/null +++ b/logging/go-zap/build @@ -0,0 +1,15 @@ +#!/bin/bash + +# Env Vars: +# REGISTRY: name of the image registry/namespace to store the images +# +# NOTE: to run this you MUST set the REGISTRY environment variable to +# your own image registry/namespace otherwise the `docker push` commands +# will fail due to an auth failure. Which means, you also need to be logged +# into that registry before you run it. + +set -ex +export REGISTRY=${REGISTRY:-icr.io/codeengine} + +# Build and push the image +KO_DOCKER_REPO="${REGISTRY}/logging/go-zap" ko build . --bare --image-user 1001 --platform linux/amd64 --sbom=none diff --git a/logging/go-zap/go.mod b/logging/go-zap/go.mod new file mode 100644 index 00000000..bedaab98 --- /dev/null +++ b/logging/go-zap/go.mod @@ -0,0 +1,10 @@ +module github.com/IBM/CodeEngine/logging/go-zap + +go 1.23.0 + +require go.uber.org/zap v1.27.0 + +require ( + github.com/stretchr/testify v1.10.0 // indirect + go.uber.org/multierr v1.10.0 // indirect +) diff --git a/logging/go-zap/go.sum b/logging/go-zap/go.sum new file mode 100644 index 00000000..0b9b705a --- /dev/null +++ b/logging/go-zap/go.sum @@ -0,0 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +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.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logging/go-zap/main.go b/logging/go-zap/main.go new file mode 100644 index 00000000..e50790da --- /dev/null +++ b/logging/go-zap/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "os" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +func init() { + stdout := zapcore.AddSync(os.Stdout) + + level := zap.NewAtomicLevelAt(zap.InfoLevel) + + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + encoderCfg.MessageKey = "message" + consoleEncoder := zapcore.NewJSONEncoder(encoderCfg) + + zap.ReplaceGlobals(zap.New(zapcore.NewCore(consoleEncoder, stdout, level))) +} + +func main() { + logger := zap.L() + + logger.Info("This is a simple log message") + + logger.Info("A log entry that adds another key-value pair", + zap.String("extra_key", "extra_value"), + ) + +} diff --git a/logging/go/go.mod b/logging/go/go.mod index a2e1f827..7d75cde5 100644 --- a/logging/go/go.mod +++ b/logging/go/go.mod @@ -1,10 +1,3 @@ module github.com/IBM/CodeEngine/logging/go go 1.23.0 - -require go.uber.org/zap v1.27.0 - -require ( - github.com/stretchr/testify v1.10.0 // indirect - go.uber.org/multierr v1.10.0 // indirect -) diff --git a/logging/go/go.sum b/logging/go/go.sum index 0b9b705a..e69de29b 100644 --- a/logging/go/go.sum +++ b/logging/go/go.sum @@ -1,14 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= -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.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= -go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logging/go/main.go b/logging/go/main.go index e50790da..6c50ba8f 100644 --- a/logging/go/main.go +++ b/logging/go/main.go @@ -1,33 +1,45 @@ package main import ( + "log/slog" "os" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" ) func init() { - stdout := zapcore.AddSync(os.Stdout) - - level := zap.NewAtomicLevelAt(zap.InfoLevel) - - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "timestamp" - encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder - encoderCfg.MessageKey = "message" - consoleEncoder := zapcore.NewJSONEncoder(encoderCfg) - zap.ReplaceGlobals(zap.New(zapcore.NewCore(consoleEncoder, stdout, level))) + logHandlerOptions := &slog.HandlerOptions{ + + Level: slog.Level(0), + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + + if a.Key == "msg" { + return slog.Attr{ + Key: "message", + Value: a.Value, + } + } + if a.Key == "time" { + return slog.Attr{ + Key: "timestamp", + Value: a.Value, + } + } + return a + + }, + } + handler := slog.NewJSONHandler(os.Stdout, logHandlerOptions) + + slog.SetDefault(slog.New(handler)) } func main() { - logger := zap.L() + logger := slog.With() logger.Info("This is a simple log message") logger.Info("A log entry that adds another key-value pair", - zap.String("extra_key", "extra_value"), + "extra_key", "extra_value", ) }