zen/core/zlog/exported.go
2024-08-27 15:22:42 +07:00

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