-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
57 lines (53 loc) · 1.21 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package godemon
import (
"fmt"
"os"
)
var (
logLevels = map[string]int{
"DEBUG": 0,
"INFO": 1,
"NOTIFY": 2,
"WARNING": 3,
"ERROR": 4,
"FATAL": 5,
}
logLevel = logLevels["NOTIFY"]
)
func logf(level, format string, args ...interface{}) {
if logLevels[level] < logLevel {
return
}
// TODO: Disable colors if not writing to terminal
const gray = "\x1b[90m"
const reset = "\x1b[0m"
prefix, ok := os.LookupEnv("GODEMON_LOG_PREFIX")
if !ok {
prefix = "[godemon] "
}
// Don't show NOTIFY prefix since these are very common and are intended
// to be user friendly.
if level != "NOTIFY" {
prefix += level + ": "
}
fmt.Fprintf(os.Stderr, gray+prefix+format+reset+"\n", args...)
}
func debugf(format string, args ...interface{}) {
logf("DEBUG", format, args...)
}
func infof(format string, args ...interface{}) {
logf("INFO", format, args...)
}
func notifyf(format string, args ...interface{}) {
logf("NOTIFY", format, args...)
}
func warnf(format string, args ...interface{}) {
logf("WARNING", format, args...)
}
func errorf(format string, args ...interface{}) {
logf("ERROR", format, args...)
}
func fatalf(format string, args ...interface{}) {
logf("FATAL", format, args...)
os.Exit(1)
}