draincloud-core/internal/logger/logger.go

82 lines
1.7 KiB
Go
Raw Normal View History

2024-09-27 22:37:58 +00:00
package logger
import (
"context"
"log/slog"
"os"
)
//nolint:gochecknoglobals // ...
2024-11-23 08:52:06 +00:00
var globalLogger *slog.Logger = newLogger(LevelDebug, os.Stdout)
func SetLevel(l slog.Level) {
globalLogger = newLogger(l, os.Stdout)
}
2024-09-27 22:37:58 +00:00
const (
LevelEmergency = slog.Level(10000)
LevelAlert = slog.Level(1000)
LevelCritial = slog.Level(100)
LevelError = slog.LevelError
LevelWarn = slog.LevelWarn
LevelNotice = slog.Level(2)
LevelInfo = slog.LevelInfo
LevelDebug = slog.LevelDebug
)
func Fatal(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelEmergency, message, attrs...)
os.Exit(1)
}
func Emergency(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelEmergency, message, attrs...)
}
func Alert(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelAlert, message, attrs...)
}
func Critial(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelCritial, message, attrs...)
}
func Error(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.ErrorContext(ctx, message, attrs...)
}
func Warn(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.WarnContext(ctx, message, attrs...)
}
func Notice(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelNotice, message, attrs...)
}
func Info(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.InfoContext(ctx, message, attrs...)
}
func Debug(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
2024-11-23 08:52:06 +00:00
l.DebugContext(ctx, message, attrs...)
2024-09-27 22:37:58 +00:00
}