generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
72 lines (61 loc) · 1.89 KB
/
api.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
package log
import (
"context"
"os"
)
type Sink interface {
Log(entry Entry) error
}
type Interface interface {
Log(entry Entry)
Logf(level Level, format string, args ...interface{})
Tracef(format string, args ...interface{})
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
// Errorf conditionally logs an error. If err is nil, nothing is logged.
Errorf(err error, format string, args ...interface{})
}
// Level is the log level.
//
//go:generate enumer -type=Level -text -transform=lower -output log_level_string.go
type Level int
// Log levels.
const (
// Default is a special value that means the log level will use a default.
Default Level = 0
Trace Level = 1
Debug Level = 5
Info Level = 9
Warn Level = 13
Error Level = 17
)
// Severity returns the open telemetry severity of the log level.
func (l Level) Severity() int {
return int(l)
}
// ParseLevel parses a log level from text.
func ParseLevel(input string) (Level, error) {
var level Level
err := level.UnmarshalText([]byte(input))
return level, err
}
type contextKey struct{}
// FromContext retrieves the current logger from the context or panics
func FromContext(ctx context.Context) *Logger {
logger, ok := ctx.Value(contextKey{}).(*Logger)
if ok {
return logger
}
panic("no logger in context")
}
// ContextWithLogger returns a new context with the given logger attached. Use
// FromContext to retrieve it.
func ContextWithLogger(ctx context.Context, logger *Logger) context.Context {
return context.WithValue(ctx, contextKey{}, logger)
}
func ContextWithNewDefaultLogger(ctx context.Context) context.Context {
// Matches LOG_COLOR in log.Config. This is a special case for the default logger in testing.
color := os.Getenv("LOG_COLOR") != ""
return ContextWithLogger(ctx, Configure(os.Stderr, Config{Level: Debug, Color: color}))
}