Skip to content

Commit ee72ee8

Browse files
authored
feat: LOG_LEVEL env var in go (#4852)
discussions were had
1 parent 2dfc134 commit ee72ee8

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

deployment/clouddeploy/gke-workers/base/gitter.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ spec:
3232
env:
3333
- name: GOMEMLIMIT
3434
value: "100GiB"
35-
- name: DEBUG_LOG
36-
value: "true"
35+
- name: LOG_LEVEL
36+
value: "debug"
3737
volumeMounts:
3838
- mountPath: /work
3939
name: disk-data

go/logger/init.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"strconv"
10+
"strings"
1011
"sync"
1112

1213
"cloud.google.com/go/errorreporting"
@@ -119,8 +120,17 @@ func initTracing(ctx context.Context, projectID, serviceName string) {
119120

120121
func cloudHandlerOptions() *slog.HandlerOptions {
121122
level := slog.LevelInfo
122-
if os.Getenv("DEBUG_LOG") != "" {
123-
level = slog.LevelDebug
123+
if lvl := os.Getenv("LOG_LEVEL"); lvl != "" {
124+
switch strings.ToLower(lvl) {
125+
case "debug":
126+
level = slog.LevelDebug
127+
case "info":
128+
level = slog.LevelInfo
129+
case "warn":
130+
level = slog.LevelWarn
131+
case "error":
132+
level = slog.LevelError
133+
}
124134
}
125135

126136
return &slog.HandlerOptions{

go/logger/localhandler.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"log/slog"
8+
"os"
89
"strings"
910

1011
"github.com/charmbracelet/lipgloss"
@@ -26,17 +27,35 @@ var (
2627
)
2728

2829
type localHandler struct {
29-
w io.Writer
30+
w io.Writer
31+
level slog.Level
3032
}
3133

34+
var _ slog.Handler = (*localHandler)(nil)
35+
3236
func newLocalHandler(w io.Writer) *localHandler {
37+
level := slog.LevelInfo
38+
if lvl := os.Getenv("LOG_LEVEL"); lvl != "" {
39+
switch strings.ToLower(lvl) {
40+
case "debug":
41+
level = slog.LevelDebug
42+
case "info":
43+
level = slog.LevelInfo
44+
case "warn":
45+
level = slog.LevelWarn
46+
case "error":
47+
level = slog.LevelError
48+
}
49+
}
50+
3351
return &localHandler{
34-
w: w,
52+
w: w,
53+
level: level,
3554
}
3655
}
3756

38-
func (h *localHandler) Enabled(_ context.Context, _ slog.Level) bool {
39-
return true
57+
func (h *localHandler) Enabled(_ context.Context, level slog.Level) bool {
58+
return level >= h.level
4059
}
4160

4261
func (h *localHandler) Handle(_ context.Context, r slog.Record) error {

0 commit comments

Comments
 (0)