-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
91 lines (76 loc) · 3.39 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package logger
import "context"
// Log level constants match slog levels for consistency with applications that use it.
// These values are used to determine which logs to write based on minimum level configuration.
const (
LevelDebug int64 = -4 // matches slog.LevelDebug
LevelInfo int64 = 0 // matches slog.LevelInfo
LevelWarn int64 = 4 // matches slog.LevelWarn
LevelError int64 = 8 // matches slog.LevelError
)
// Init initializes the logger with the provided configuration and context.
func Init(ctx context.Context, cfg ...*LoggerConfig) error {
return configLogger(ctx, cfg...)
}
// Debug logs a message at debug level with the given context and additional arguments.
// Messages are dropped if the logger's level is higher than debug or if logger is not initialized.
func Debug(logCtx context.Context, args ...any) {
log(logCtx, flags, LevelDebug, traceDepth, args...)
}
// Info logs a message at info level with the given context and additional arguments.
// Messages are dropped if the logger's level is higher than info or if logger is not initialized.
func Info(logCtx context.Context, args ...any) {
log(logCtx, flags, LevelInfo, traceDepth, args...)
}
// Warn logs a message at warning level with the given context and additional arguments.
// Messages are dropped if the logger's level is higher than warn or if logger is not initialized.
func Warn(logCtx context.Context, args ...any) {
log(logCtx, flags, LevelWarn, traceDepth, args...)
}
// Error logs a message at error level with the given context and additional arguments.
// Messages are dropped if the logger's level is higher than error or if logger is not initialized.
func Error(logCtx context.Context, args ...any) {
log(logCtx, flags, LevelError, traceDepth, args...)
}
// Shutdown gracefully shuts down the logger, ensuring all buffered messages are written
// and files are properly closed. It respects context cancellation for timeout control.
func Shutdown(ctx ...context.Context) error {
shutdownCtx := context.Background()
if len(ctx) > 0 {
shutdownCtx = ctx[0]
}
return shutdownLogger(shutdownCtx)
}
// DebugTrace is Debug log with trace.
func DebugTrace(logCtx context.Context, depth int, args ...any) {
log(logCtx, flags, LevelDebug, int64(depth), args...)
}
// InfoTrace is Info log with trace.
func InfoTrace(logCtx context.Context, depth int, args ...any) {
log(logCtx, flags, LevelInfo, int64(depth), args...)
}
// WarnTrace is Warn log with trace.
func WarnTrace(logCtx context.Context, depth int, args ...any) {
log(logCtx, flags, LevelWarn, int64(depth), args...)
}
// ErrorTrace is Error log with trace.
func ErrorTrace(logCtx context.Context, depth int, args ...any) {
log(logCtx, flags, LevelError, int64(depth), args...)
}
// Config initializes the logger with the provided configuration.
func Config(cfg *LoggerConfig) error {
return configLogger(context.Background(), cfg)
}
// EnsureInitialized checks if the logger is initialized, and initializes if not.
// returns true if it was already initialized or initialization attempt was successful.
// returns false if logger cannot be initialized.
func EnsureInitialized() bool {
return ensureInitialized()
}
// LogWithFlags allows custom flag control for logging with specified flags, level and trace depth
func LogWithFlags(ctx context.Context, flags int64, level int64, depth int64, args ...any) {
if depth == -1 {
depth = traceDepth
}
log(ctx, flags, level, depth, args...)
}