-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
65 lines (57 loc) · 1.31 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
package immusql
import (
"context"
"log"
"time"
)
// LogLevel defines the logging level to be used by a logger.
type LogLevel int
// Define log levels. The default level is debug.
const (
LogLevelDebug LogLevel = iota
LogLevelInfo
LogLevelWarn
LogLevelError
LogLevelFatal
LogLevelPanic
LogLevelNone
LogLevelTrace LogLevel = -1
)
// Logger is the interface for logging debug messages of the driver.
type Logger interface {
Log(ctx context.Context, level LogLevel, msg string)
}
// defaultLogger is the default logger used if no custom logger has been set.
type defaultLogger struct {
level LogLevel
}
func (dl defaultLogger) Log(ctx context.Context, level LogLevel, msg string) {
// Only log the message if the level of the message is above the level of the logger.
if level < dl.level {
return
}
levelName := logLevelName(level)
log.Printf("%v %s: %s", time.Now(), levelName, msg)
}
// logLevelName creates a printable name for a loglevel.
func logLevelName(level LogLevel) (name string) {
switch level {
case LogLevelDebug:
name = "debug"
case LogLevelInfo:
name = "info"
case LogLevelWarn:
name = "warn"
case LogLevelError:
name = "error"
case LogLevelFatal:
name = "fatal"
case LogLevelPanic:
name = "panic"
case LogLevelNone:
name = "none"
case LogLevelTrace:
name = "trace"
}
return
}