aboutsummaryrefslogtreecommitdiff
path: root/cmd/mobius-hotline-server
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2022-11-03 16:52:20 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2022-11-03 16:52:20 -0700
commit4434fc431bef6b49b25ba5c5a0fd76b950c78ae1 (patch)
treefb1bdcc4b5c77975e3797c94a3f509059e8d4a4b /cmd/mobius-hotline-server
parentdaffb50bd98caf31813b2eaebee4f95d20caf9b7 (diff)
Add optional logging to file
Diffstat (limited to 'cmd/mobius-hotline-server')
-rw-r--r--cmd/mobius-hotline-server/main.go30
1 files changed, 29 insertions, 1 deletions
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,