]> git.r.bdr.sh - rbdr/mobius/blobdiff - cmd/mobius-hotline-server/main.go
Replace deprecated rand.Seed usage
[rbdr/mobius] / cmd / mobius-hotline-server / main.go
index 8407449ab826a3dfc0e0fe051b3c37249b81d2b6..4fa28c2a32e617aa2e01ad176fe20240765892ec 100644 (file)
@@ -9,14 +9,13 @@ import (
        "github.com/jhalter/mobius/hotline"
        "go.uber.org/zap"
        "go.uber.org/zap/zapcore"
+       "gopkg.in/natefinch/lumberjack.v2"
        "io"
        "log"
-       "math/rand"
        "net/http"
        "os"
        "path/filepath"
        "runtime"
-       "time"
 )
 
 //go:embed mobius/config
@@ -27,8 +26,6 @@ const (
 )
 
 func main() {
-       rand.Seed(time.Now().UnixNano())
-
        ctx, cancel := context.WithCancel(context.Background())
 
        // TODO: implement graceful shutdown by closing context
@@ -51,6 +48,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 +65,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 +144,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,