Skip to content
Draft
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
1 change: 1 addition & 0 deletions logging/go-zap/.ceignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
4 changes: 4 additions & 0 deletions logging/go-zap/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dockerignore
build
Dockerfile
vendor
10 changes: 10 additions & 0 deletions logging/go-zap/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
15 changes: 15 additions & 0 deletions logging/go-zap/build
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions logging/go-zap/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 14 additions & 0 deletions logging/go-zap/go.sum
Original file line number Diff line number Diff line change
@@ -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=
33 changes: 33 additions & 0 deletions logging/go-zap/main.go
Original file line number Diff line number Diff line change
@@ -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"),
)

}
7 changes: 0 additions & 7 deletions logging/go/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 0 additions & 14 deletions logging/go/go.sum
Original file line number Diff line number Diff line change
@@ -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=
42 changes: 27 additions & 15 deletions logging/go/main.go
Original file line number Diff line number Diff line change
@@ -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",
)

}