1
0
forked from um/cli

refactor: remove global logger

This commit is contained in:
Unlock Music Dev 2022-11-19 07:25:41 +08:00
parent e2fc56ddb2
commit 4ecaef1ff9
Signed by untrusted user: um-dev
GPG Key ID: 95202E10D3413A1D
3 changed files with 23 additions and 49 deletions

View File

@ -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

View File

@ -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
)

13
internal/logging/zap.go Normal file
View File

@ -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()
}