Add way to specify log level

This commit is contained in:
Ben Sarmiento
2023-12-07 16:19:35 +01:00
parent 9f08262ddc
commit 94230ab4dc
2 changed files with 21 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/pkg/logutil"
cmap "github.com/orcaman/concurrent-map/v2"
)

View File

@@ -16,7 +16,26 @@ type Logger struct {
}
func NewLogger(logPath string) *Logger {
envLogLevel := os.Getenv("LOG_LEVEL")
var zapLevel zapcore.Level
switch envLogLevel {
case "DEBUG":
zapLevel = zapcore.DebugLevel
case "INFO":
zapLevel = zapcore.InfoLevel
case "WARN":
zapLevel = zapcore.WarnLevel
case "ERROR":
zapLevel = zapcore.ErrorLevel
case "FATAL":
zapLevel = zapcore.FatalLevel
default:
zapLevel = zapcore.InfoLevel
}
zapCfg := zap.NewDevelopmentConfig()
zapCfg.Level.SetLevel(zapLevel)
consoleCfg := zapcore.EncoderConfig{
TimeKey: "time",
@@ -45,6 +64,7 @@ func NewLogger(logPath string) *Logger {
EncodeDuration: zapcore.StringDurationEncoder,
}
fileEncoder := zapcore.NewConsoleEncoder(fileCfg)
// Set up file logging with overwrite mode
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {