From 4ecaef1ff998830d20aee34ed7d7e2a17d74c4f4 Mon Sep 17 00:00:00 2001 From: Unlock Music Dev Date: Sat, 19 Nov 2022 07:25:41 +0800 Subject: [PATCH] refactor: remove global logger --- cmd/um/main.go | 18 ++++++++-------- internal/logging/logging.go | 41 ------------------------------------- internal/logging/zap.go | 13 ++++++++++++ 3 files changed, 23 insertions(+), 49 deletions(-) delete mode 100644 internal/logging/logging.go create mode 100644 internal/logging/zap.go diff --git a/cmd/um/main.go b/cmd/um/main.go index 2357149..de38e44 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -26,6 +26,8 @@ import ( var AppVersion = "v0.0.6" +var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger + func main() { module, ok := debug.ReadBuildInfo() if ok && module.Main.Version != "(devel)" { @@ -51,11 +53,11 @@ func main() { } err := app.Run(os.Args) if err != nil { - logging.Log().Fatal("run app failed", zap.Error(err)) + logger.Fatal("run app failed", zap.Error(err)) } } func printSupportedExtensions() { - exts := []string{} + var exts []string for ext := range common.DecoderRegistry { exts = append(exts, ext) } @@ -121,7 +123,7 @@ func appMain(c *cli.Context) (err error) { } else { allDec := common.GetDecoder(inputStat.Name(), skipNoop) if len(allDec) == 0 { - logging.Log().Fatal("skipping while no suitable decoder") + logger.Fatal("skipping while no suitable decoder") } return tryDecFile(input, output, allDec, removeSource) } @@ -138,13 +140,13 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc } allDec := common.GetDecoder(item.Name(), skipNoop) if len(allDec) == 0 { - logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name())) + logger.Info("skipping while no suitable decoder", zap.String("file", item.Name())) continue } err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource) if err != nil { - logging.Log().With(zap.Error(err)).Error("conversion failed", zap.String("source", item.Name())) + logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) } } return nil @@ -162,7 +164,7 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu if err := dec.Validate(); err == nil { break } else { - logging.Log().With(zap.Error(err)).Warn("try decode failed") + logger.Warn("try decode failed", zap.Error(err)) dec = nil } } @@ -196,9 +198,9 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu if err != nil { return err } - logging.Log().Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath)) + logger.Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath)) } else { - logging.Log().Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath)) + logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath)) } return nil diff --git a/internal/logging/logging.go b/internal/logging/logging.go deleted file mode 100644 index b14012d..0000000 --- a/internal/logging/logging.go +++ /dev/null @@ -1,41 +0,0 @@ -package logging - -import ( - "os" - "sync" - "time" -) - -import ( - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -// newDefaultProductionLog configures a custom log that is -// intended for use by default if no other log is specified -// in a config. It writes to stderr, uses the console encoder, -// and enables INFO-level logs and higher. -func newDefaultProductionLog() *zap.Logger { - encCfg := zap.NewProductionEncoderConfig() - // if interactive terminal, make output more human-readable by default - encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) { - encoder.AppendString(ts.Format("2006/01/02 15:04:05.000")) - } - encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder - enc := zapcore.NewConsoleEncoder(encCfg) - core := zapcore.NewCore(enc, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zap.DebugLevel)) - return zap.New(core) - -} - -// Log returns the current default logger. -func Log() *zap.Logger { - defaultLoggerMu.RLock() - defer defaultLoggerMu.RUnlock() - return defaultLogger -} - -var ( - defaultLogger = newDefaultProductionLog() - defaultLoggerMu sync.RWMutex -) diff --git a/internal/logging/zap.go b/internal/logging/zap.go new file mode 100644 index 0000000..3c21c37 --- /dev/null +++ b/internal/logging/zap.go @@ -0,0 +1,13 @@ +package logging + +import ( + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +func NewZapLogger() (*zap.Logger, error) { + zapCfg := zap.NewDevelopmentConfig() + zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + zapCfg.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006/01/02 15:04:05.000") + return zapCfg.Build() +}