1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

feat: add log-level flag

This commit is contained in:
Ash Keel 2022-11-24 13:15:59 +01:00
parent bb8f3f754e
commit 668620a090
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 12 additions and 5 deletions

View file

@ -18,14 +18,14 @@ var (
incomingLogs chan LogEntry
)
func initLogger() {
func initLogger(level zapcore.Level) {
lastLogs = make([]LogEntry, 0)
incomingLogs = make(chan LogEntry, 100)
logStorage := NewLogStorage(zap.InfoLevel)
logStorage := NewLogStorage(level)
consoleLogger := zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
zapcore.Lock(os.Stderr),
zap.InfoLevel,
level,
)
fileLogger := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
@ -35,7 +35,7 @@ func initLogger() {
MaxBackups: 3,
MaxAge: 28,
}),
zap.DebugLevel,
level,
)
core := zapcore.NewTee(
consoleLogger,

View file

@ -8,6 +8,8 @@ import (
"os"
"time"
"go.uber.org/zap/zapcore"
"github.com/strimertul/strimertul/modules"
"github.com/strimertul/strimertul/modules/loyalty"
"github.com/strimertul/strimertul/modules/twitch"
@ -45,6 +47,7 @@ func main() {
Version: appVersion,
Action: cliMain,
Flags: []cli.Flag{
&cli.StringFlag{Name: "log-level", Usage: "logging level (debug,info,warn,error)", Value: "info"},
&cli.StringFlag{Name: "driver", Usage: "specify database driver", Value: "auto"},
&cli.StringFlag{Name: "database-dir", Aliases: []string{"db-dir"}, Usage: "specify database directory", Value: "data"},
&cli.StringFlag{Name: "backup-dir", Aliases: []string{"b-dir"}, Usage: "specify backup directory", Value: "backups"},
@ -85,7 +88,11 @@ func main() {
rand.Seed(time.Now().UnixNano())
// Initialize logger with global flags
initLogger()
level, err := zapcore.ParseLevel(ctx.String("log-level"))
if err != nil {
level = zapcore.InfoLevel
}
initLogger(level)
return nil
},
After: func(ctx *cli.Context) error {