X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/00913df3521d967ac31ee22c23447b5aa711f2e6..4e22add664e9de2b48876c5cae18a7c26458066a:/cmd/mobius-hotline-server/main.go diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go index 8407449..11e3baf 100644 --- a/cmd/mobius-hotline-server/main.go +++ b/cmd/mobius-hotline-server/main.go @@ -9,6 +9,7 @@ import ( "github.com/jhalter/mobius/hotline" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" "io" "log" "math/rand" @@ -51,6 +52,8 @@ func main() { configDir := flag.String("config", defaultConfigPath(), "Path to config root") version := flag.Bool("version", false, "print version and exit") logLevel := flag.String("log-level", "info", "Log level") + logFile := flag.String("log-file", "", "Path to log file") + init := flag.Bool("init", false, "Populate the config dir with default configuration") flag.Parse() @@ -66,7 +69,14 @@ func main() { os.Exit(0) } - cores := []zapcore.Core{newStdoutCore(zapLvl)} + cores := []zapcore.Core{ + newStdoutCore(zapLvl), + } + + if *logFile != "" { + cores = append(cores, newLogFileCore(*logFile, zapLvl)) + } + l := zap.New(zapcore.NewTee(cores...)) defer func() { _ = l.Sync() }() logger := l.Sugar() @@ -138,6 +148,24 @@ func newStdoutCore(level zapcore.Level) zapcore.Core { ) } +func newLogFileCore(path string, level zapcore.Level) zapcore.Core { + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + writer := zapcore.AddSync(&lumberjack.Logger{ + Filename: path, + MaxSize: 100, // MB + MaxBackups: 3, + MaxAge: 365, // days + }) + + return zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderCfg), + writer, + level, + ) +} + var zapLogLevel = map[string]zapcore.Level{ "debug": zap.DebugLevel, "info": zap.InfoLevel,