log

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

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.

func Debug

func Debug(msg string)

Debug logs a message at DEBUG level using the singleton logger.

func DebugWith

func DebugWith(msg string, args ...any)

log functions with structured key-value fields

func Debugf

func Debugf(format string, args ...any)

Debugf logs a formatted message at DEBUG level using the singleton logger.

func Error

func Error(msg string)

Error logs a message at ERROR level using the singleton logger.

func ErrorWith

func ErrorWith(msg string, args ...any)

func Errorf

func Errorf(format string, args ...any)

Errorf logs a formatted message at ERROR level using the singleton logger.

func Info

func Info(msg string)

Info logs a message at INFO level using the singleton logger.

func InfoWith

func InfoWith(msg string, args ...any)

func Infof

func Infof(format string, args ...any)

Infof logs a formatted message at INFO level using the singleton logger.

func SetLevel

func SetLevel(level string) error

SetLevel dynamically changes the log level of the singleton logger. Accepted values are "error", "warn", "info", and "debug" (case-insensitive). Returns an error if the provided level string is unrecognised.

func Warn

func Warn(msg string)

Warn logs a message at WARN level using the singleton logger.

func WarnWith

func WarnWith(msg string, args ...any)

func Warnf

func Warnf(format string, args ...any)

Warnf logs a formatted message at WARN level using the singleton logger.

Types

type Log

type Log struct {
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Log holds the structured logger, its dynamic level, and the optional rotating log file writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL