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 --- 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 +- 12 files changed, 11 insertions(+), 98 deletions(-) delete mode 100644 internal/log/log.go (limited to 'internal') 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