zen/core/zlog/exported.go

167 lines
3.8 KiB
Go
Raw Permalink Normal View History

2024-08-27 15:22:42 +07:00
package zlog
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"gitlab.bareksa.com/backend/zen/core/zcaller"
)
var Logger = New(os.Stderr, defaultOpts)
func Info(ctx context.Context, msg string) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelInfo) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelInfo)
2024-08-27 15:22:42 +07:00
}
func Infof(ctx context.Context, msg string, args ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelInfo) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelInfo)
2024-08-27 15:22:42 +07:00
}
func Infow(ctx context.Context, msg string, fields ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelInfo) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelInfo, fields...)
2024-08-27 15:22:42 +07:00
}
func Debug(ctx context.Context, msg string) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelDebug) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelDebug)
2024-08-27 15:22:42 +07:00
}
func Debugf(ctx context.Context, msg string, args ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelDebug) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelDebug)
2024-08-27 15:22:42 +07:00
}
func Debugw(ctx context.Context, msg string, fields ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelDebug) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelDebug, fields...)
2024-08-27 15:22:42 +07:00
}
func Warn(ctx context.Context, msg string) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelWarn) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelWarn)
2024-08-27 15:22:42 +07:00
}
func Warnf(ctx context.Context, msg string, args ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelWarn) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelWarn)
2024-08-27 15:22:42 +07:00
}
func Warnw(ctx context.Context, msg string, fields ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelWarn) {
return notifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelWarn, fields...)
2024-08-27 15:22:42 +07:00
}
func Error(ctx context.Context, msg string) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelError) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelError)
2024-08-27 15:22:42 +07:00
}
func Errorf(ctx context.Context, msg string, args ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelError) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelError)
2024-08-27 15:22:42 +07:00
}
func Errorw(ctx context.Context, msg string, fields ...any) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, slog.LevelError) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelError, fields...)
2024-08-27 15:22:42 +07:00
}
func LogRecord(ctx context.Context, record slog.Record) Notifier {
2024-08-27 15:22:42 +07:00
if !Logger.Enabled(ctx, record.Level) {
return nullNotifier{}
2024-08-27 15:22:42 +07:00
}
if err := Logger.Handle(ctx, record); err != nil {
slog.ErrorContext(ctx, "failed to log", "error", err)
}
notifier := &notifier{
ctx: ctx,
notifier: Logger.opts.NotificationHandler,
record: record,
}
return notifier
2024-08-27 15:22:42 +07:00
}
func Log(ctx context.Context, msg string, pc uintptr, t time.Time, level slog.Level, fields ...any) Notifier {
2024-08-27 15:22:42 +07:00
record := slog.NewRecord(t, level, msg, pc)
record.Add(fields...)
// error is only returned if the output writer fails.
if err := Logger.Handle(ctx, record); err != nil {
slog.ErrorContext(ctx, "failed to log", "error", err)
}
return &notifier{
ctx: ctx,
notifier: Logger.opts.NotificationHandler,
record: record,
}
2024-08-27 15:22:42 +07:00
}
func SetDefault(log *ZLog) {
Logger = log
}