57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
// Copyright 2024 Patial Tech. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package logger
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
)
|
|
|
|
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...)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
return a, b
|
|
}
|