-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger.go
67 lines (55 loc) · 2.19 KB
/
logger.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
package slog
import "context"
// A Logger is a way of outputting events.
type Logger interface {
Log(evs ...Event)
Flush() error
}
// LeveledLogger is a logger which logs at different levels.
type LeveledLogger interface {
Critical(ctx context.Context, msg string, params ...interface{})
Error(ctx context.Context, msg string, params ...interface{})
Warn(ctx context.Context, msg string, params ...interface{})
Info(ctx context.Context, msg string, params ...interface{})
Debug(ctx context.Context, msg string, params ...interface{})
Trace(ctx context.Context, msg string, params ...interface{})
}
// FromErrorLogger is a logger which logs errors.
// The severity of the log is inferred from the error.
type FromErrorLogger interface {
FromError(ctx context.Context, msg string, err error, params ...interface{})
}
// SeverityLogger is a logger which can log at different severity levels.
type SeverityLogger struct {
Logger
}
// NewSeverityLogger creates a SeverityLogger which wraps the default logger.
func NewSeverityLogger() SeverityLogger {
return SeverityLogger{
Logger: DefaultLogger(),
}
}
// Critical writes a Critical event to the logger.
func (s SeverityLogger) Critical(ctx context.Context, msg string, params ...interface{}) {
s.Log(Eventf(CriticalSeverity, ctx, msg, params...))
}
// Error writes a Error event to the logger.
func (s SeverityLogger) Error(ctx context.Context, msg string, params ...interface{}) {
s.Log(Eventf(ErrorSeverity, ctx, msg, params...))
}
// Warn writes a Warn event to the logger.
func (s SeverityLogger) Warn(ctx context.Context, msg string, params ...interface{}) {
s.Log(Eventf(WarnSeverity, ctx, msg, params...))
}
// Info writes a Info event to the logger.
func (s SeverityLogger) Info(ctx context.Context, msg string, params ...interface{}) {
s.Log(Eventf(InfoSeverity, ctx, msg, params...))
}
// Debug writes a Debug event to the logger.
func (s SeverityLogger) Debug(ctx context.Context, msg string, params ...interface{}) {
s.Log(Eventf(DebugSeverity, ctx, msg, params...))
}
// Trace writes a Trace event to the logger.
func (s SeverityLogger) Trace(ctx context.Context, msg string, params ...interface{}) {
s.Log(Eventf(TraceSeverity, ctx, msg, params...))
}