zen/core/zlog/exported.go

167 lines
3.8 KiB
Go

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 {
if !Logger.Enabled(ctx, slog.LevelInfo) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelInfo)
}
func Infof(ctx context.Context, msg string, args ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelInfo) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelInfo)
}
func Infow(ctx context.Context, msg string, fields ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelInfo) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelInfo, fields...)
}
func Debug(ctx context.Context, msg string) Notifier {
if !Logger.Enabled(ctx, slog.LevelDebug) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelDebug)
}
func Debugf(ctx context.Context, msg string, args ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelDebug) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelDebug)
}
func Debugw(ctx context.Context, msg string, fields ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelDebug) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelDebug, fields...)
}
func Warn(ctx context.Context, msg string) Notifier {
if !Logger.Enabled(ctx, slog.LevelWarn) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelWarn)
}
func Warnf(ctx context.Context, msg string, args ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelWarn) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelWarn)
}
func Warnw(ctx context.Context, msg string, fields ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelWarn) {
return notifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelWarn, fields...)
}
func Error(ctx context.Context, msg string) Notifier {
if !Logger.Enabled(ctx, slog.LevelError) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelError)
}
func Errorf(ctx context.Context, msg string, args ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelError) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
msg = fmt.Sprintf(msg, args...)
return Log(ctx, msg, pc, t, slog.LevelError)
}
func Errorw(ctx context.Context, msg string, fields ...any) Notifier {
if !Logger.Enabled(ctx, slog.LevelError) {
return nullNotifier{}
}
pc := zcaller.Get(3)
t := time.Now()
return Log(ctx, msg, pc, t, slog.LevelError, fields...)
}
func LogRecord(ctx context.Context, record slog.Record) Notifier {
if !Logger.Enabled(ctx, record.Level) {
return nullNotifier{}
}
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
}
func Log(ctx context.Context, msg string, pc uintptr, t time.Time, level slog.Level, fields ...any) Notifier {
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,
}
}
func SetDefault(log *ZLog) {
Logger = log
}