extract logging library
Alan Pearce alan@alanpearce.eu
Sun, 30 Jun 2024 22:14:37 +0200
16 files changed, 21 insertions(+), 105 deletions(-)
jump to
- cmd/build/main.go
- cmd/server/main.go
- go.mod
- go.sum
- internal/builder/builder.go
- internal/config/config.go
- internal/content/posts.go
- internal/listenfd/listenfd.go
- internal/server/dev.go
- internal/server/logging.go
- internal/server/mime.go
- internal/server/server.go
- internal/vcs/repository.go
- internal/website/filemap.go
- internal/website/mux.go
M cmd/build/main.go → cmd/build/main.go
@@ -6,7 +6,7 @@ "os" "go.alanpearce.eu/website/internal/builder" "go.alanpearce.eu/website/internal/config" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/ardanlabs/conf/v3" "gitlab.com/tozd/go/errors"
M cmd/server/main.go → cmd/server/main.go
@@ -6,7 +6,7 @@ "fmt" "os" "os/signal" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "go.alanpearce.eu/website/internal/server" "github.com/ardanlabs/conf/v3"
M go.mod → go.mod
@@ -1,6 +1,6 @@ module go.alanpearce.eu/website -go 1.22.1 +go 1.22.3 require ( github.com/BurntSushi/toml v1.4.0 @@ -24,12 +24,9 @@ github.com/osdevisnot/sorvor v0.4.4 github.com/pberkel/caddy-storage-redis v1.2.0 github.com/snabb/sitemap v1.0.4 github.com/stefanfritsch/goldmark-fences v1.0.0 - github.com/sykesm/zap-logfmt v0.0.4 - github.com/thessem/zap-prettyconsole v0.5.0 github.com/yuin/goldmark v1.7.4 gitlab.com/tozd/go/errors v0.8.1 - go.uber.org/zap v1.27.0 - moul.io/zapfilter v1.7.0 + go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60 ) require ( @@ -85,11 +82,14 @@ github.com/skeema/knownhosts v1.2.2 // indirect github.com/snabb/diagio v1.0.4 // indirect github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/sykesm/zap-logfmt v0.0.4 // indirect + github.com/thessem/zap-prettyconsole v0.5.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/zeebo/blake3 v0.2.3 // indirect go.uber.org/automaxprocs v1.5.3 // indirect go.uber.org/mock v0.4.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect go.uber.org/zap/exp v0.2.0 // indirect golang.org/x/crypto v0.24.0 // indirect golang.org/x/crypto/x509roots/fallback v0.0.0-20240624163532-1c7450041f58 // indirect @@ -106,4 +106,5 @@ google.golang.org/protobuf v1.34.2 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + moul.io/zapfilter v1.7.0 // indirect )
M go.sum → go.sum
@@ -225,6 +225,8 @@ github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo= github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= gitlab.com/tozd/go/errors v0.8.1 h1:RfylffRAsl3PbDdHNUBEkTleTCiL/RIT+Ef8p0HRNCI= gitlab.com/tozd/go/errors v0.8.1/go.mod h1:PvIdUMLpPwxr+KEBxghQaCMydHXGYdJQn/PhdMqYREY= +go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60 h1:a4PJum9vqboETepA81QlBXAqgC9Zeu3F9EojnGiHauA= +go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60/go.mod h1:GaYgUfXSlaHBvdrInLYyKDMKo2Bmx1+IIFrlnZkZW+A= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.8.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
M internal/builder/builder.go → internal/builder/builder.go
@@ -12,7 +12,7 @@ "time" "go.alanpearce.eu/website/internal/config" "go.alanpearce.eu/website/internal/content" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "go.alanpearce.eu/website/internal/sitemap" "go.alanpearce.eu/website/templates"
M internal/config/config.go → internal/config/config.go
@@ -5,7 +5,7 @@ "io/fs" "net/url" "path/filepath" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/BurntSushi/toml" "gitlab.com/tozd/go/errors"
M internal/content/posts.go → internal/content/posts.go
@@ -9,7 +9,7 @@ "slices" "strings" "time" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/adrg/frontmatter" mapset "github.com/deckarep/golang-set/v2"
M internal/listenfd/listenfd.go → internal/listenfd/listenfd.go
@@ -6,7 +6,7 @@ "net" "os" "strconv" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "gitlab.com/tozd/go/errors" )
D internal/log/log.go
@@ -1,87 +0,0 @@-package log - -import ( - "os" - - zaplogfmt "github.com/sykesm/zap-logfmt" - prettyconsole "github.com/thessem/zap-prettyconsole" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "moul.io/zapfilter" -) - -type Logger struct { - logger *zap.SugaredLogger -} - -func (l Logger) DPanic(msg string, rest ...any) { - l.logger.DPanicw(msg, rest...) -} -func (l Logger) Debug(msg string, rest ...any) { - l.logger.Debugw(msg, rest...) -} -func (l Logger) Info(msg string, rest ...any) { - l.logger.Infow(msg, rest...) -} -func (l Logger) Warn(msg string, rest ...any) { - l.logger.Warnw(msg, rest...) -} -func (l Logger) Error(msg string, rest ...any) { - l.logger.Errorw(msg, rest...) -} -func (l Logger) Panic(msg string, rest ...any) { - l.logger.Panicw(msg, rest...) -} -func (l Logger) Fatal(msg string, rest ...any) { - l.logger.Fatalw(msg, rest...) -} - -func (l Logger) Named(name string) *Logger { - return &Logger{ - logger: l.logger.Named(name), - } -} - -func (l Logger) GetLogger() *zap.Logger { - return l.logger.Desugar() -} - -func getLevelFromEnv() (zapcore.Level, error) { - if str, found := os.LookupEnv("LOG_LEVEL"); found { - l, err := zap.ParseAtomicLevel(str) - - return l.Level(), err - } - - return zap.InfoLevel, nil -} - -func Configure(isProduction bool) *Logger { - var l *zap.Logger - level, err := getLevelFromEnv() - if err != nil { - panic(err) - } - if isProduction { - cfg := zap.NewProductionEncoderConfig() - cfg.TimeKey = "" - l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, level)) - } else { - cfg := prettyconsole.NewEncoderConfig() - cfg.TimeKey = "" - var filter zapfilter.FilterFunc - if debug := os.Getenv("DEBUG"); debug != "" { - filter = zapfilter.Any(zapfilter.MinimumLevel(level), zapfilter.ByNamespaces(debug)) - } else { - filter = zapfilter.MinimumLevel(level) - } - l = zap.New(zapfilter.NewFilteringCore( - zapcore.NewCore(prettyconsole.NewEncoder(cfg), os.Stderr, zap.DebugLevel), - filter, - )) - } - - return &Logger{ - logger: l.WithOptions(zap.AddCallerSkip(1)).Sugar(), - } -}
M internal/server/dev.go → internal/server/dev.go
@@ -9,7 +9,7 @@ "path/filepath" "slices" "time" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/fsnotify/fsnotify" "gitlab.com/tozd/go/errors"
M internal/server/logging.go → internal/server/logging.go
@@ -3,7 +3,7 @@ import ( "net/http" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" ) type LoggingResponseWriter struct {
M internal/server/mime.go → internal/server/mime.go
@@ -3,7 +3,7 @@ import ( "mime" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" ) var newMIMEs = map[string]string{
M internal/server/server.go → internal/server/server.go
@@ -15,7 +15,7 @@ "time" "go.alanpearce.eu/website/internal/builder" cfg "go.alanpearce.eu/website/internal/config" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "go.alanpearce.eu/website/internal/vcs" "go.alanpearce.eu/website/internal/website"
M internal/vcs/repository.go → internal/vcs/repository.go
@@ -4,7 +4,7 @@ import ( "os" "go.alanpearce.eu/website/internal/config" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/go-git/go-git/v5" "gitlab.com/tozd/go/errors"
M internal/website/filemap.go → internal/website/filemap.go
@@ -10,7 +10,7 @@ "os" "path/filepath" "strings" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "gitlab.com/tozd/go/errors" )
M internal/website/mux.go → internal/website/mux.go
@@ -7,7 +7,7 @@ "strings" "go.alanpearce.eu/website/internal/config" ihttp "go.alanpearce.eu/website/internal/http" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "go.alanpearce.eu/website/templates" "github.com/benpate/digit"