Skip to content

Commit 7f608bb

Browse files
committed
refactoring logging
implements #115 Signed-off-by: Markus Blaschke <[email protected]>
1 parent 3c9d9c0 commit 7f608bb

File tree

11 files changed

+50
-23
lines changed

11 files changed

+50
-23
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ RUN ["./azure-metrics-exporter", "--help"]
3131
# final-static
3232
#############################################
3333
FROM gcr.io/distroless/static AS final-static
34-
ENV LOG_JSON=1
34+
ENV LOG_FORMAT=json \
35+
LOG_SOURCE=file
3536
WORKDIR /
3637
COPY --from=test /app .
3738
USER 1000:1000

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PROJECT_NAME := $(shell basename $(CURDIR))
22
GIT_TAG := $(shell git describe --dirty --tags --always)
33
GIT_COMMIT := $(shell git rev-parse --short HEAD)
44
LDFLAGS := -X "main.gitTag=$(GIT_TAG)" -X "main.gitCommit=$(GIT_COMMIT)" -extldflags "-static" -s -w
5+
BUILDFLAGS := -trimpath
56

67
FIRST_GOPATH := $(firstword $(subst :, ,$(shell go env GOPATH)))
78
GOLANGCI_LINT_BIN := $(FIRST_GOPATH)/bin/golangci-lint
@@ -25,13 +26,13 @@ vendor:
2526

2627
.PHONY: build-all
2728
build-all:
28-
GOOS=linux GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME)' .
29-
GOOS=darwin GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME).darwin' .
30-
GOOS=windows GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME).exe' .
29+
GOOS=linux GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' $(BUILDFLAGS) -o '$(PROJECT_NAME)' .
30+
GOOS=darwin GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' $(BUILDFLAGS) -o '$(PROJECT_NAME).darwin' .
31+
GOOS=windows GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' $(BUILDFLAGS) -o '$(PROJECT_NAME).exe' .
3132

3233
.PHONY: build
3334
build:
34-
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) .
35+
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' $(BUILDFLAGS) -o $(PROJECT_NAME) .
3536

3637
.PHONY: image
3738
image: image

common.logger.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"log/slog"
56
"os"
7+
"strings"
68

79
"github.com/webdevops/go-common/log/slogger"
810
)
@@ -13,22 +15,43 @@ var (
1315
)
1416

1517
func initLogger() *slogger.Logger {
18+
ShowSource := Opts.Logger.Source != "off"
19+
1620
loggerOpts := slogger.NewHandlerOptions(&slog.HandlerOptions{
17-
AddSource: Opts.Logger.Debug,
21+
AddSource: ShowSource,
1822
Level: LoggerLevel,
1923
})
2024

21-
loggerOpts.ShortenSourcePath = true
25+
if Opts.Logger.Source != "off" {
26+
loggerOpts.SourceMode = slogger.SourceMode(Opts.Logger.Source)
27+
}
2228

23-
if Opts.Logger.Json {
24-
loggerOpts.ShowTime = false
25-
logger = slogger.New(slog.NewJSONHandler(os.Stderr, loggerOpts.HandlerOptions))
26-
} else {
29+
loggerOpts.ShowTime = Opts.Logger.Time
30+
31+
switch strings.ToLower(Opts.Logger.Format) {
32+
case "text":
2733
logger = slogger.New(slog.NewTextHandler(os.Stdout, loggerOpts.HandlerOptions))
34+
case "json":
35+
logger = slogger.New(slog.NewJSONHandler(os.Stderr, loggerOpts.HandlerOptions))
36+
default:
37+
fmt.Println("Unknown log format:", Opts.Logger.Format)
38+
os.Exit(1)
2839
}
2940

30-
if Opts.Logger.Debug {
41+
switch strings.ToLower(Opts.Logger.Level) {
42+
case "trace":
43+
LoggerLevel.Set(slogger.LevelTrace)
44+
case "debug":
3145
LoggerLevel.Set(slog.LevelDebug)
46+
case "info":
47+
LoggerLevel.Set(slog.LevelInfo)
48+
case "warning":
49+
LoggerLevel.Set(slog.LevelWarn)
50+
case "error":
51+
LoggerLevel.Set(slog.LevelError)
52+
default:
53+
fmt.Println("Unknown log level:", Opts.Logger.Level)
54+
os.Exit(1)
3255
}
3356

3457
slog.SetDefault(logger.Logger)

config/opts.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ type (
99
Opts struct {
1010
// logger
1111
Logger struct {
12-
Debug bool `long:"log.debug" env:"LOG_DEBUG" description:"debug mode"`
13-
Development bool `long:"log.devel" env:"LOG_DEVEL" description:"development mode"`
14-
Json bool `long:"log.json" env:"LOG_JSON" description:"Switch log output to json format"`
12+
Level string `long:"log.level" env:"LOG_LEVEL" description:"Log level" choice:"trace" choice:"debug" choice:"info" choice:"warning" choice:"error" default:"info"`
13+
Format string `long:"log.format" env:"LOG_FORMAT" description:"Log format" choice:"text" choice:"json" default:"text"`
14+
Source string `long:"log.source" env:"LOG_SOURCE" description:"Show source for every log message (useful for debugging and bug reports)" choice:"off" choice:"short" choice:"file" choice:"full" default:"off"`
15+
Time bool `long:"log.time" env:"LOG_TIME" description:"Show time"`
16+
Json bool `long:"log.json" env:"LOG_JSON" description:"Switch log output to json format"`
1517
}
1618

1719
// azure

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/patrickmn/go-cache v2.1.0+incompatible
1818
github.com/prometheus/client_golang v1.22.0
1919
github.com/remeh/sizedwaitgroup v1.0.0
20-
github.com/webdevops/go-common v0.0.0-20250720220738-a468ce107f36
20+
github.com/webdevops/go-common v0.0.0-20250723211346-409b62d063f4
2121
)
2222

2323
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
108108
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
109109
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
110110
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
111-
github.com/webdevops/go-common v0.0.0-20250720220738-a468ce107f36 h1:N7tsRqWOSeYvtgioX4G9X5FpqiiwXadI7tzybv+IUAU=
112-
github.com/webdevops/go-common v0.0.0-20250720220738-a468ce107f36/go.mod h1:wnrzgLXCXbaZM8FWwqCSzeJYOU68vpf7KbuDgyWQAsw=
111+
github.com/webdevops/go-common v0.0.0-20250723211346-409b62d063f4 h1:Yw539F+cOATcd5JKtxl/oDD+CZySaui8e5OpPVZTKaY=
112+
github.com/webdevops/go-common v0.0.0-20250723211346-409b62d063f4/go.mod h1:wnrzgLXCXbaZM8FWwqCSzeJYOU68vpf7KbuDgyWQAsw=
113113
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
114114
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
115115
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=

probe_metrics_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ func probeMetricsListHandler(w http.ResponseWriter, r *http.Request) {
9797
slog.String("method", r.Method),
9898
slog.Int("status", http.StatusOK),
9999
slog.Duration("latency", latency),
100-
).Info("request handled")
100+
).Debug("request handled")
101101
}

probe_metrics_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,5 @@ func probeMetricsResourceHandler(w http.ResponseWriter, r *http.Request) {
108108
slog.String("method", r.Method),
109109
slog.Int("status", http.StatusOK),
110110
slog.Duration("latency", latency),
111-
).Info("request handled")
111+
).Debug("request handled")
112112
}

probe_metrics_resourcegraph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ func probeMetricsResourceGraphHandler(w http.ResponseWriter, r *http.Request) {
107107
slog.String("method", r.Method),
108108
slog.Int("status", http.StatusOK),
109109
slog.Duration("latency", latency),
110-
).Info("request handled")
110+
).Debug("request handled")
111111
}

probe_metrics_scrape.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@ func probeMetricsScrapeHandler(w http.ResponseWriter, r *http.Request) {
109109
slog.String("method", r.Method),
110110
slog.Int("status", http.StatusOK),
111111
slog.Duration("latency", latency),
112-
).Info("request handled")
112+
).Debug("request handled")
113113
}

0 commit comments

Comments
 (0)