-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
integration.go
151 lines (135 loc) · 4.1 KB
/
integration.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package golog
import (
"context"
"log/slog"
)
func integrade(logger any) Handler {
switch v := logger.(type) {
case *slog.Logger:
return integradeSlog(v)
case ExternalLogger:
return integrateExternalLogger(v)
case StdLogger:
return integrateStdLogger(v)
default:
panic("not supported logger integration, please open a feature request at: https://github.com/kataras/golog/issues/new")
}
}
/*
func (*slog.Logger).Debug(msg string, args ...any)
func (*slog.Logger).DebugContext(ctx context.Context, msg string, args ...any)
func (*slog.Logger).Enabled(ctx context.Context, level slog.Level) bool
func (*slog.Logger).Error(msg string, args ...any)
func (*slog.Logger).ErrorContext(ctx context.Context, msg string, args ...any)
func (*slog.Logger).Handler() slog.Handler
func (*slog.Logger).Info(msg string, args ...any)
func (*slog.Logger).InfoContext(ctx context.Context, msg string, args ...any)
func (*slog.Logger).Log(ctx context.Context, level slog.Level, msg string, args ...any)
func (*slog.Logger).LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
func (*slog.Logger).Warn(msg string, args ...any)
func (*slog.Logger).WarnContext(ctx context.Context, msg string, args ...any)
func (*slog.Logger).With(args ...any) *slog.Logger
func (*slog.Logger).WithGroup(name string) *slog.Logger
*/
func integradeSlog(logger *slog.Logger) Handler {
return func(log *Log) bool {
// golog level to slog level.
level := getSlogLevel(log.Level)
// golog fields to slog attributes.
if len(log.Fields) > 0 {
attrs := make([]slog.Attr, 0, len(log.Fields))
for k, v := range log.Fields {
attrs = append(attrs, slog.Any(k, v))
}
// log the message with attrs.
logger.LogAttrs(context.Background(), level, log.Message, attrs...)
} else {
logger.Log(context.Background(), level, log.Message)
}
return true
}
}
// ExternalLogger is a typical logger interface.
// Any logger or printer that completes this interface
// can be used to intercept and handle the golog's messages.
//
// See `Logger#Install` and `Logger#Handle` for more.
type ExternalLogger interface {
Print(...interface{})
Println(...interface{})
Error(...interface{})
Warn(...interface{})
Info(...interface{})
Debug(...interface{})
}
// integrateExternalLogger is a Handler which
// intercepts all messages from print functions,
// between print action and actual write to the output,
// and sends these (messages) to the external "logger".
//
// In short terms, when this handler is passed via `Handle`
// then, instead of printing from the logger's Printer
// it prints from the given "logger".
func integrateExternalLogger(logger ExternalLogger) Handler {
return func(log *Log) bool {
printFunc := getExternalPrintFunc(logger, log)
printFunc(log.Message)
return true
}
}
func getSlogLevel(level Level) slog.Level {
switch level {
case ErrorLevel:
return slog.LevelError
case WarnLevel:
return slog.LevelWarn
case InfoLevel:
return slog.LevelInfo
case DebugLevel:
return slog.LevelDebug
}
return slog.LevelDebug
}
func getExternalPrintFunc(logger ExternalLogger, log *Log) func(...interface{}) {
switch log.Level {
case ErrorLevel:
return logger.Error
case WarnLevel:
return logger.Warn
case InfoLevel:
return logger.Info
case DebugLevel:
return logger.Debug
}
// disable level or use of golog#Print/Println functions:
// passed with Println
if log.NewLine {
return logger.Println
}
return logger.Print
}
// StdLogger is the standard log.Logger interface.
// Any logger or printer that completes this interface
// can be used to intercept and handle the golog's messages.
//
// See `Logger#Install` and `Logger#Handle` for more.
type StdLogger interface {
Printf(format string, v ...interface{})
Print(v ...interface{})
Println(v ...interface{})
}
func integrateStdLogger(logger StdLogger) Handler {
return func(log *Log) bool {
printFunc := getStdPrintFunc(logger, log)
printFunc(log.Message)
return true
}
}
func getStdPrintFunc(logger StdLogger, log *Log) func(...interface{}) {
// no levels here
// passed with Println
if log.NewLine {
return logger.Println
}
return logger.Print
}