rano/util/logger/logger.go

53 lines
979 B
Go
Raw Normal View History

2024-11-01 14:25:30 +00:00
package logger
import (
2024-11-17 16:58:29 +00:00
"context"
2024-11-01 14:25:30 +00:00
"fmt"
"log/slog"
2024-11-10 09:22:33 +00:00
"os"
2024-11-01 14:25:30 +00:00
)
func Info(msg string, args ...any) {
a, b := getArgs(args)
slog.Info(fmt.Sprintf(msg, a...), b...)
}
func Warn(msg string, args ...any) {
a, b := getArgs(args)
slog.Warn(fmt.Sprintf(msg, a...), b...)
}
2024-11-17 16:58:29 +00:00
func Incident(ctx context.Context, name, msg string, args ...any) {
a, b := getArgs(args)
slog.Warn(fmt.Sprintf(msg, a...), b...)
// TODO: save incident for alert purpose
}
2024-11-01 14:25:30 +00:00
func Error(err error, args ...any) {
a, b := getArgs(args)
slog.Error(fmt.Sprintf(err.Error(), a...), b...)
// TODO: save error log for later scrutiny
}
2024-11-10 09:22:33 +00:00
// Fatal error will exit with os.Exit(1)
func Fatal(msg string, args ...any) {
a, b := getArgs(args)
slog.Error(fmt.Sprintf(msg, a...), b...)
os.Exit(1)
}
2024-11-01 14:25:30 +00:00
func getArgs(args []any) ([]any, []any) {
var a []any
var b []any
for _, arg := range args {
switch arg.(type) {
case slog.Attr:
b = append(b, arg)
default:
a = append(a, arg)
}
}
2024-11-01 14:25:30 +00:00
return a, b
}