tss-rocks/backend/pkg/logger/logger.go
CDN 05ddc1f783
Some checks failed
Build Backend / Build Docker Image (push) Successful in 3m33s
Test Backend / test (push) Failing after 31s
[feature] migrate to monorepo
2025-02-21 00:49:20 +08:00

51 lines
1.3 KiB
Go

package logger
import (
"os"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"tss-rocks-be/internal/config"
)
// Setup configures the logger based on the provided configuration
func Setup(cfg *config.Config) {
// Set log level
level := zerolog.InfoLevel
if cfg.Logging.Level != "" {
switch strings.ToLower(cfg.Logging.Level) {
case "debug":
level = zerolog.DebugLevel
case "info":
level = zerolog.InfoLevel
case "warn":
level = zerolog.WarnLevel
case "error":
level = zerolog.ErrorLevel
case "fatal":
level = zerolog.FatalLevel
case "panic":
level = zerolog.PanicLevel
}
}
zerolog.SetGlobalLevel(level)
// Configure output format
if cfg.Logging.Format == "console" {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
})
} else {
// Use JSON format by default
zerolog.TimeFieldFormat = time.RFC3339
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
}
// GetLogger returns the global logger instance
func GetLogger() *zerolog.Logger {
return &log.Logger
}