Skip to content

Commit

Permalink
Implement structured logging (#331)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Helmich <[email protected]>
  • Loading branch information
jkroepke and martin-helmich authored Jul 15, 2023
1 parent 950e9e4 commit 76552e7
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 121 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM golang:1.18
FROM golang:1.20

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

FROM scratch

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

EXPOSE 4040
USER 65532
ENTRYPOINT ["/prometheus-nginxlog-exporter"]
37 changes: 20 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
module github.com/martin-helmich/prometheus-nginxlog-exporter

go 1.20

require (
github.com/hashicorp/consul/api v1.20.0
github.com/hashicorp/consul/api v1.22.0
github.com/hashicorp/hcl v1.0.0
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/nxadm/tail v1.4.8
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/common v0.44.0
github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5
github.com/satyrius/gonx v1.4.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.24.0
gopkg.in/mcuadros/go-syslog.v2 v2.3.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/armon/go-metrics v0.3.10 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/sys v0.8.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.18
92 changes: 54 additions & 38 deletions go.sum

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions log/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package log

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type Logger struct {
zap *zap.SugaredLogger
}

func New(logLevel, logFormat string) (*Logger, error) {
level, err := zap.ParseAtomicLevel(logLevel)
if err != nil {
return nil, err
}

config := zap.NewProductionConfig()
config.Level = level
config.Encoding = logFormat
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

// build logger
log, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
return nil, err
}

return &Logger{
zap: log.Sugar(),
}, nil
}

func (log *Logger) Print(args ...interface{}) {
log.zap.Info(args...)
}

func (log *Logger) Debug(msg string, fields ...zap.Field) {
log.zap.Debug(msg, fields)
}

func (log *Logger) Info(msg string, fields ...zap.Field) {
log.zap.Info(msg, fields)
}

func (log *Logger) Warn(msg string, fields ...zap.Field) {
log.zap.Warn(msg, fields)
}

func (log *Logger) Error(msg string, fields ...zap.Field) {
log.zap.Error(msg, fields)
}

func (log *Logger) Fatal(args ...interface{}) {
log.zap.Fatal(args...)
}

func (log *Logger) Panic(args ...interface{}) {
log.zap.Panic(args...)
}

func (log *Logger) Printf(template string, args ...interface{}) {
log.zap.Infof(template, args...)
}

func (log *Logger) Debugf(template string, args ...interface{}) {
log.zap.Debugf(template, args...)
}
func (log *Logger) Infof(template string, args ...interface{}) {
log.zap.Infof(template, args...)
}

func (log *Logger) Warnf(template string, args ...interface{}) {
log.zap.Warnf(template, args...)
}

func (log *Logger) Errorf(template string, args ...interface{}) {
log.zap.Errorf(template, args...)
}

func (log *Logger) Fatalf(template string, args ...interface{}) {
log.zap.Fatalf(template, args...)
}

func (log *Logger) Panicf(template string, args ...interface{}) {
log.zap.Panicf(template, args...)
}

func (log *Logger) Println(args ...interface{}) {
log.zap.Infoln(args...)
}

func (log *Logger) Debugln(args ...interface{}) {
log.zap.Debugln(args...)
}

func (log *Logger) Infoln(args ...interface{}) {
log.zap.Infoln(args...)
}

func (log *Logger) Warnln(args ...interface{}) {
log.zap.Warnln(args...)
}

func (log *Logger) Errorln(args ...interface{}) {
log.zap.Errorln(args...)
}

func (log *Logger) Fatalln(args ...interface{}) {
log.zap.Fatalln(args...)
}

func (log *Logger) Panicln(args ...interface{}) {
log.zap.Panicln(args...)
}
Loading

0 comments on commit 76552e7

Please sign in to comment.