Documentation
¶
Index ¶
- Constants
- Variables
- func CloseLogFile()
- func Debug(msg string)
- func DebugWith(msg string, args ...any)
- func Debugf(format string, args ...any)
- func Error(msg string)
- func ErrorWith(msg string, args ...any)
- func Errorf(format string, args ...any)
- func Info(msg string)
- func InfoWith(msg string, args ...any)
- func Infof(format string, args ...any)
- func SetLevel(level string) error
- func Warn(msg string)
- func WarnWith(msg string, args ...any)
- func Warnf(format string, args ...any)
- type Log
Constants ¶
View Source
const ( // log file LOG_FILE_MAX_SIZE = 100 // LOG_FILE_MAX_SIZE is the maximum size of a single log file in megabytes before rotation. LOG_FILE_MAX_BACKUPS = 100 // LOG_FILE_MAX_BACKUPS is the maximum number of rotated log backup files to retain. LOG_FILE_MAX_AGE = 0 // LOG_FILE_MAX_AGE is the maximum number of days to retain old log files; 0 means never delete. )
Variables ¶
View Source
var Instance = sync.OnceValue(func() *Log { log := &Log{ Logger: nil, logLevel: &slog.LevelVar{}, logFile: &lumberjack.Logger{}, } level := "info" if env.Env() != nil { level = strings.ToLower(strings.TrimSpace(env.Env().String("log.level"))) } switch level { case "error": log.logLevel.Set(slog.LevelError) case "warn": log.logLevel.Set(slog.LevelWarn) case "debug": log.logLevel.Set(slog.LevelDebug) default: log.logLevel.Set(slog.LevelInfo) } opts := &slog.HandlerOptions{ AddSource: false, Level: log.logLevel, } if env.Env() != nil && env.Env().Bool("log.writefile") { log.logFile.Filename = env.Env().String("log.path") log.logFile.MaxSize = env.Env().Int("log.file.max.size") if log.logFile.MaxSize == 0 { log.logFile.MaxSize = LOG_FILE_MAX_SIZE } log.logFile.MaxBackups = env.Env().Int("log.file.max.backups") if log.logFile.MaxBackups == 0 { log.logFile.MaxBackups = LOG_FILE_MAX_BACKUPS } log.logFile.MaxAge = env.Env().Int("log.file.max.age") if log.logFile.MaxAge == 0 { log.logFile.MaxAge = LOG_FILE_MAX_AGE } log.logFile.Compress = true log.logFile.LocalTime = false log.asyncWriter = newAsyncWriter(log.logFile) log.Logger = slog.New( slogFormatter.NewFormatterHandler( slogFormatter.TimezoneConverter(time.UTC), slogFormatter.TimeFormatter(time.RFC3339, nil), )( slog.NewJSONHandler(log.asyncWriter, opts), ), ) } else { log.Logger = slog.New( slogFormatter.NewFormatterHandler( slogFormatter.TimezoneConverter(time.UTC), slogFormatter.TimeFormatter(time.RFC3339, nil), )( slog.NewJSONHandler(os.Stdout, opts), ), ) } slog.SetDefault(log.Logger) return log })
Instance is the package-level singleton Log, initialized exactly once on first access. It reads log level and file rotation settings from the environment configuration.
Functions ¶
func CloseLogFile ¶
func CloseLogFile()
CloseLogFile closes the underlying rotating log file used by the singleton logger. It should be called during graceful shutdown to flush and release file resources.
Types ¶
Click to show internal directories.
Click to hide internal directories.