From a14e030b41799ea65e026d600ce18a48507bebd0 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 30 Jun 2024 22:14:37 +0200 Subject: extract logging library --- cmd/build/main.go | 2 +- cmd/server/main.go | 2 +- go.mod | 11 +++--- go.sum | 2 + internal/builder/builder.go | 2 +- internal/config/config.go | 2 +- internal/content/posts.go | 2 +- internal/listenfd/listenfd.go | 2 +- internal/log/log.go | 87 ------------------------------------------- internal/server/dev.go | 2 +- internal/server/logging.go | 2 +- internal/server/mime.go | 2 +- internal/server/server.go | 2 +- internal/vcs/repository.go | 2 +- internal/website/filemap.go | 2 +- internal/website/mux.go | 2 +- 16 files changed, 21 insertions(+), 105 deletions(-) delete mode 100644 internal/log/log.go diff --git a/cmd/build/main.go b/cmd/build/main.go index 39cbc1b..84de2dc 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -6,7 +6,7 @@ import ( "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" diff --git a/cmd/server/main.go b/cmd/server/main.go index b74a0cc..ca69ba2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -6,7 +6,7 @@ import ( "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" diff --git a/go.mod b/go.mod index 35f61c5..05bb1fa 100644 --- a/go.mod +++ b/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 @@ require ( 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 @@ require ( 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 @@ require ( 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 ) diff --git a/go.sum b/go.sum index a579a5a..bbafba5 100644 --- a/go.sum +++ b/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= diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 63a9999..b99d919 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -12,7 +12,7 @@ import ( "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" diff --git a/internal/config/config.go b/internal/config/config.go index 42e36d7..47d5de8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -5,7 +5,7 @@ import ( "net/url" "path/filepath" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/BurntSushi/toml" "gitlab.com/tozd/go/errors" diff --git a/internal/content/posts.go b/internal/content/posts.go index dd14d1e..f4c6c76 100644 --- a/internal/content/posts.go +++ b/internal/content/posts.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/adrg/frontmatter" mapset "github.com/deckarep/golang-set/v2" diff --git a/internal/listenfd/listenfd.go b/internal/listenfd/listenfd.go index 8c723eb..5287898 100644 --- a/internal/listenfd/listenfd.go +++ b/internal/listenfd/listenfd.go @@ -6,7 +6,7 @@ import ( "os" "strconv" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "gitlab.com/tozd/go/errors" ) diff --git a/internal/log/log.go b/internal/log/log.go deleted file mode 100644 index f644e6b..0000000 --- a/internal/log/log.go +++ /dev/null @@ -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(), - } -} diff --git a/internal/server/dev.go b/internal/server/dev.go index 4db9465..6fcc93e 100644 --- a/internal/server/dev.go +++ b/internal/server/dev.go @@ -9,7 +9,7 @@ import ( "slices" "time" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "github.com/fsnotify/fsnotify" "gitlab.com/tozd/go/errors" diff --git a/internal/server/logging.go b/internal/server/logging.go index 6393099..f744931 100644 --- a/internal/server/logging.go +++ b/internal/server/logging.go @@ -3,7 +3,7 @@ package server import ( "net/http" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" ) type LoggingResponseWriter struct { diff --git a/internal/server/mime.go b/internal/server/mime.go index 340e4c5..cb1b1cf 100644 --- a/internal/server/mime.go +++ b/internal/server/mime.go @@ -3,7 +3,7 @@ package server import ( "mime" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" ) var newMIMEs = map[string]string{ diff --git a/internal/server/server.go b/internal/server/server.go index f01f939..203c5c5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -15,7 +15,7 @@ import ( "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" diff --git a/internal/vcs/repository.go b/internal/vcs/repository.go index 017b3d8..e034ea4 100644 --- a/internal/vcs/repository.go +++ b/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" diff --git a/internal/website/filemap.go b/internal/website/filemap.go index f9ce462..64b914f 100644 --- a/internal/website/filemap.go +++ b/internal/website/filemap.go @@ -10,7 +10,7 @@ import ( "path/filepath" "strings" - "go.alanpearce.eu/website/internal/log" + "go.alanpearce.eu/x/log" "gitlab.com/tozd/go/errors" ) diff --git a/internal/website/mux.go b/internal/website/mux.go index ebdf2d0..6844551 100644 --- a/internal/website/mux.go +++ b/internal/website/mux.go @@ -7,7 +7,7 @@ import ( "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" -- cgit 1.4.1