-
Notifications
You must be signed in to change notification settings - Fork 22
/
logger.go
103 lines (88 loc) · 2.63 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
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
package goptuna
import (
"fmt"
"log"
)
// Logger is the interface for logging messages.
// If you want to log nothing, please set Logger as nil.
// If you want to print more verbose logs,
// it might StudyOptionTrialNotifyChannel option are useful.
type Logger interface {
Debug(msg string, fields ...interface{})
Info(msg string, fields ...interface{})
Warn(msg string, fields ...interface{})
Error(msg string, fields ...interface{})
}
// LoggerLevel represents Level is a logging priority.
// Higher levels are more important.
type LoggerLevel int
const (
// LoggerLevelDebug logs are typically voluminous, and are usually disabled in production.
LoggerLevelDebug LoggerLevel = iota - 1
// LoggerLevelInfo is the default logging priority.
LoggerLevelInfo
// LoggerLevelWarn logs are more important than Info, but don't need individual human review.
LoggerLevelWarn
// LoggerLevelError logs are high-priority.
LoggerLevelError
)
var _ Logger = &StdLogger{}
// StdLogger wraps 'log' standard library.
type StdLogger struct {
Logger *log.Logger
Level LoggerLevel
Color bool
}
func (l *StdLogger) write(msg string, fields ...interface{}) {
if l.Logger == nil {
return
}
fields = append([]interface{}{msg}, fields...)
l.Logger.Println(fields...)
}
// Debug logs a message at LoggerLevelDebug which are usually disabled in production.
func (l *StdLogger) Debug(msg string, fields ...interface{}) {
if l.Level > LoggerLevelDebug {
return
}
prefix := "[DEBUG] "
if l.Color {
prefix = "\033[1;36m" + prefix + "\033[0m"
}
l.write(fmt.Sprintf("%s%s", prefix, msg), fields...)
}
// Info logs a message at LoggerLevelInfo. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
func (l *StdLogger) Info(msg string, fields ...interface{}) {
if l.Level > LoggerLevelInfo {
return
}
prefix := "[INFO] "
if l.Color {
prefix = "\033[1;34m" + prefix + "\033[0m"
}
l.write(fmt.Sprintf("%s%s:", prefix, msg), fields...)
}
// Warn logs a message at LoggerLevelWarn which more important than Info,
// but don't need individual human review.
func (l *StdLogger) Warn(msg string, fields ...interface{}) {
if l.Level > LoggerLevelWarn {
return
}
prefix := "[WARN] "
if l.Color {
prefix = "\033[1;33m" + prefix + "\033[0m"
}
l.write(fmt.Sprintf("%s%s:", prefix, msg), fields...)
}
// Error logs a message at LoggerLevelError which are high-priority.
func (l *StdLogger) Error(msg string, fields ...interface{}) {
if l.Level > LoggerLevelError {
return
}
prefix := "[ERROR] "
if l.Color {
prefix = "\033[1;31m" + prefix + "\033[0m"
}
l.write(fmt.Sprintf("%s%s:", prefix, msg), fields...)
}