forked from koding/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
132 lines (111 loc) · 2.92 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package kite
import (
"os"
"os/signal"
"strings"
"syscall"
"github.com/koding/logging"
)
type Level int
var debugMode bool
// Logging levels.
const (
FATAL Level = iota
ERROR
WARNING
INFO
DEBUG
)
// Logger is the interface used to log messages in different levels.
type Logger interface {
// Fatal logs to the FATAL, ERROR, WARNING, INFO and DEBUG levels,
// including a stack trace of all running goroutines, then calls
// os.Exit(1).
Fatal(format string, args ...interface{})
// Error logs to the ERROR, WARNING, INFO and DEBUG level.
Error(format string, args ...interface{})
// Warning logs to the WARNING, INFO and DEBUG level.
Warning(format string, args ...interface{})
// Info logs to the INFO and DEBUG level.
Info(format string, args ...interface{})
// Debug logs to the DEBUG level.
Debug(format string, args ...interface{})
}
// getLogLevel returns the logging level defined via the KITE_LOG_LEVEL
// environment. It returns Info by default if no environment variable
// is set.
func getLogLevel() Level {
switch strings.ToUpper(os.Getenv("KITE_LOG_LEVEL")) {
case "DEBUG":
return DEBUG
case "WARNING":
return WARNING
case "ERROR":
return ERROR
case "FATAL":
return FATAL
default:
return INFO
}
}
// convertLevel converst a kite level into logging level
func convertLevel(l Level) logging.Level {
switch l {
case DEBUG:
return logging.DEBUG
case WARNING:
return logging.WARNING
case ERROR:
return logging.ERROR
case FATAL:
return logging.CRITICAL
default:
return logging.INFO
}
}
// newLogger returns a new kite logger based on koding/logging package and a
// SetLogLvel function. The current logLevel is INFO by default, which can be
// changed with KITE_LOG_LEVEL environment variable.
func newLogger(name string) (Logger, func(Level)) {
logger := logging.NewLogger(name)
logger.SetLevel(convertLevel(getLogLevel()))
if os.Getenv("KITE_LOG_NOCOLOR") != "" {
logging.StdoutHandler.Colorize = false
logging.StderrHandler.Colorize = false
}
setLevel := func(l Level) {
logger.SetLevel(convertLevel(l))
logging.DefaultHandler.SetLevel(convertLevel(l))
}
return logger, setLevel
}
// SetupSignalHandler listens to signals and toggles the log level to DEBUG
// mode when it received a SIGUSR2 signal. Another SIGUSR2 toggles the log
// level back to the old level.
func (k *Kite) SetupSignalHandler() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR2)
go func() {
for s := range c {
k.Log.Info("Got signal: %s", s)
if debugMode {
// toogle back to old settings.
k.Log.Info("Disabling debug mode")
if k.SetLogLevel == nil {
k.Log.Error("SetLogLevel is not defined")
continue
}
k.SetLogLevel(getLogLevel())
debugMode = false
} else {
k.Log.Info("Enabling debug mode")
if k.SetLogLevel == nil {
k.Log.Error("SetLogLevel is not defined")
continue
}
k.SetLogLevel(DEBUG)
debugMode = true
}
}
}()
}