diff options
author | Alan Pearce | 2024-06-30 22:14:37 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-30 22:14:37 +0200 |
commit | a14e030b41799ea65e026d600ce18a48507bebd0 (patch) | |
tree | d1471a9cb5a335457f42b10a1e7150210ab00be2 /internal/log/log.go | |
parent | c4d5654e6360e90be2106439463f49acb55dffc8 (diff) | |
download | website-a14e030b41799ea65e026d600ce18a48507bebd0.tar.lz website-a14e030b41799ea65e026d600ce18a48507bebd0.tar.zst website-a14e030b41799ea65e026d600ce18a48507bebd0.zip |
extract logging library
Diffstat (limited to 'internal/log/log.go')
-rw-r--r-- | internal/log/log.go | 87 |
1 files changed, 0 insertions, 87 deletions
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(), - } -} |