Skip to content

Commit 76552e7

Browse files
Implement structured logging (#331)
Co-authored-by: Martin Helmich <[email protected]>
1 parent 950e9e4 commit 76552e7

12 files changed

+279
-121
lines changed

Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
FROM golang:1.18
1+
FROM golang:1.20
22

33
COPY . /work
44
WORKDIR /work
5-
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o prometheus-nginxlog-exporter
5+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-w -s' -a -installsuffix cgo -o prometheus-nginxlog-exporter
66

77
FROM scratch
88

99
COPY --from=0 /work/prometheus-nginxlog-exporter /prometheus-nginxlog-exporter
1010

1111
EXPOSE 4040
12+
USER 65532
1213
ENTRYPOINT ["/prometheus-nginxlog-exporter"]

go.mod

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
module github.com/martin-helmich/prometheus-nginxlog-exporter
22

3+
go 1.20
4+
35
require (
4-
github.com/hashicorp/consul/api v1.20.0
6+
github.com/hashicorp/consul/api v1.22.0
57
github.com/hashicorp/hcl v1.0.0
6-
github.com/mitchellh/mapstructure v1.4.1 // indirect
78
github.com/nxadm/tail v1.4.8
89
github.com/pkg/errors v0.9.1
910
github.com/prometheus/client_golang v1.16.0
1011
github.com/prometheus/common v0.44.0
11-
github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5
12+
github.com/satyrius/gonx v1.4.0
1213
github.com/stretchr/testify v1.8.4
14+
go.uber.org/zap v1.24.0
1315
gopkg.in/mcuadros/go-syslog.v2 v2.3.0
14-
gopkg.in/yaml.v2 v2.4.0
16+
gopkg.in/yaml.v3 v3.0.1
1517
)
1618

1719
require (
18-
github.com/armon/go-metrics v0.3.10 // indirect
20+
github.com/armon/go-metrics v0.4.1 // indirect
1921
github.com/beorn7/perks v1.0.1 // indirect
2022
github.com/cespare/xxhash/v2 v2.2.0 // indirect
21-
github.com/davecgh/go-spew v1.1.1 // indirect
22-
github.com/fatih/color v1.9.0 // indirect
23+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
24+
github.com/fatih/color v1.14.1 // indirect
2325
github.com/fsnotify/fsnotify v1.4.9 // indirect
2426
github.com/golang/protobuf v1.5.3 // indirect
25-
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
26-
github.com/hashicorp/go-hclog v0.14.1 // indirect
27-
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
27+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
28+
github.com/hashicorp/go-hclog v1.5.0 // indirect
29+
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
2830
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
2931
github.com/hashicorp/golang-lru v0.5.4 // indirect
3032
github.com/hashicorp/serf v0.10.1 // indirect
31-
github.com/mattn/go-colorable v0.1.6 // indirect
32-
github.com/mattn/go-isatty v0.0.12 // indirect
33+
github.com/mattn/go-colorable v0.1.13 // indirect
34+
github.com/mattn/go-isatty v0.0.17 // indirect
3335
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
3436
github.com/mitchellh/go-homedir v1.1.0 // indirect
35-
github.com/pmezard/go-difflib v1.0.0 // indirect
37+
github.com/mitchellh/mapstructure v1.5.0 // indirect
38+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
3639
github.com/prometheus/client_model v0.4.0 // indirect
3740
github.com/prometheus/procfs v0.10.1 // indirect
38-
github.com/smartystreets/goconvey v1.7.2 // indirect
41+
github.com/smartystreets/goconvey v1.8.1 // indirect
42+
go.uber.org/atomic v1.7.0 // indirect
43+
go.uber.org/multierr v1.6.0 // indirect
44+
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
3945
golang.org/x/sys v0.8.0 // indirect
4046
google.golang.org/protobuf v1.30.0 // indirect
4147
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
42-
gopkg.in/yaml.v3 v3.0.1 // indirect
4348
)
44-
45-
go 1.18

go.sum

+54-38
Large diffs are not rendered by default.

log/main.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package log
2+
3+
import (
4+
"go.uber.org/zap"
5+
"go.uber.org/zap/zapcore"
6+
)
7+
8+
type Logger struct {
9+
zap *zap.SugaredLogger
10+
}
11+
12+
func New(logLevel, logFormat string) (*Logger, error) {
13+
level, err := zap.ParseAtomicLevel(logLevel)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
config := zap.NewProductionConfig()
19+
config.Level = level
20+
config.Encoding = logFormat
21+
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
22+
23+
// build logger
24+
log, err := config.Build(zap.AddCallerSkip(1))
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
return &Logger{
30+
zap: log.Sugar(),
31+
}, nil
32+
}
33+
34+
func (log *Logger) Print(args ...interface{}) {
35+
log.zap.Info(args...)
36+
}
37+
38+
func (log *Logger) Debug(msg string, fields ...zap.Field) {
39+
log.zap.Debug(msg, fields)
40+
}
41+
42+
func (log *Logger) Info(msg string, fields ...zap.Field) {
43+
log.zap.Info(msg, fields)
44+
}
45+
46+
func (log *Logger) Warn(msg string, fields ...zap.Field) {
47+
log.zap.Warn(msg, fields)
48+
}
49+
50+
func (log *Logger) Error(msg string, fields ...zap.Field) {
51+
log.zap.Error(msg, fields)
52+
}
53+
54+
func (log *Logger) Fatal(args ...interface{}) {
55+
log.zap.Fatal(args...)
56+
}
57+
58+
func (log *Logger) Panic(args ...interface{}) {
59+
log.zap.Panic(args...)
60+
}
61+
62+
func (log *Logger) Printf(template string, args ...interface{}) {
63+
log.zap.Infof(template, args...)
64+
}
65+
66+
func (log *Logger) Debugf(template string, args ...interface{}) {
67+
log.zap.Debugf(template, args...)
68+
}
69+
func (log *Logger) Infof(template string, args ...interface{}) {
70+
log.zap.Infof(template, args...)
71+
}
72+
73+
func (log *Logger) Warnf(template string, args ...interface{}) {
74+
log.zap.Warnf(template, args...)
75+
}
76+
77+
func (log *Logger) Errorf(template string, args ...interface{}) {
78+
log.zap.Errorf(template, args...)
79+
}
80+
81+
func (log *Logger) Fatalf(template string, args ...interface{}) {
82+
log.zap.Fatalf(template, args...)
83+
}
84+
85+
func (log *Logger) Panicf(template string, args ...interface{}) {
86+
log.zap.Panicf(template, args...)
87+
}
88+
89+
func (log *Logger) Println(args ...interface{}) {
90+
log.zap.Infoln(args...)
91+
}
92+
93+
func (log *Logger) Debugln(args ...interface{}) {
94+
log.zap.Debugln(args...)
95+
}
96+
97+
func (log *Logger) Infoln(args ...interface{}) {
98+
log.zap.Infoln(args...)
99+
}
100+
101+
func (log *Logger) Warnln(args ...interface{}) {
102+
log.zap.Warnln(args...)
103+
}
104+
105+
func (log *Logger) Errorln(args ...interface{}) {
106+
log.zap.Errorln(args...)
107+
}
108+
109+
func (log *Logger) Fatalln(args ...interface{}) {
110+
log.zap.Fatalln(args...)
111+
}
112+
113+
func (log *Logger) Panicln(args ...interface{}) {
114+
log.zap.Panicln(args...)
115+
}

0 commit comments

Comments
 (0)