-
Notifications
You must be signed in to change notification settings - Fork 114
/
log.go
42 lines (35 loc) · 970 Bytes
/
log.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
package resource
import (
"context"
"fmt"
"log"
)
// LogLevel defines log levels
type LogLevel int
// Log levels
const (
LogLevelDebug LogLevel = iota
LogLevelInfo
LogLevelWarn
LogLevelError
LogLevelFatal
)
// LoggerLevel sets the logging level of the framework.
var LoggerLevel = LogLevelInfo
// Logger is the function used by rest-layer to log messages. By default
// it does nothing but you can customize it to plug any logger.
var Logger = func(ctx context.Context, level LogLevel, msg string, fields map[string]interface{}) {
log.Output(2, msg)
}
func logErrorf(ctx context.Context, format string, a ...interface{}) {
if LoggerLevel <= LogLevelError && Logger != nil {
Logger(ctx, LogLevelError, fmt.Sprintf(format, a...), nil)
}
}
func logPanicf(ctx context.Context, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
if LoggerLevel <= LogLevelFatal && Logger != nil {
Logger(ctx, LogLevelFatal, msg, nil)
}
panic(msg)
}