-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
107 lines (92 loc) · 2.41 KB
/
logging.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
package main
import (
"os"
"github.com/kpango/glg"
)
// log level constants to determine which labels to enable and which to disable
const (
all uint = iota
debug
log
info
warning
success
err
)
var infolog *os.File
// ConfigureLogging will setup the glg logging package with the correct file destination
// coloring, etc. as desired for the entire application.
func ConfigureLogging(level string, logPath string) {
if logPath != "" {
infolog = glg.FileWriter(logPath, 0644)
glg.Get().AddWriter(infolog)
}
glg.Get().
SetMode(glg.BOTH).
EnableColor().
SetLevelMode(glg.LOG, glg.NONE)
// Map the config log level value to an internal representation that is easier
// to perform equality operations on.
desiredLevel := map[string]uint{
"all": all,
"debug": debug,
"log": log,
"info": info,
"warning": warning,
"success": success,
"error": err,
}[level]
for _, glgLevel := range []glg.LEVEL{glg.DEBG, glg.INFO, glg.WARN, glg.OK, glg.ERR, glg.LOG} {
glg.Get().SetLevelMode(glgLevel, glgDestination(glgLevel, desiredLevel))
}
//glg.Info("info")
// glg.Infof("%s : %s", "info", "formatted")
// glg.Log("log")
// glg.Logf("%s : %s", "info", "formatted")
// glg.Debug("debug")
// glg.Debugf("%s : %s", "info", "formatted")
// glg.Warn("warn")
// glg.Warnf("%s : %s", "info", "formatted")
// glg.Error("error")
// glg.Errorf("%s : %s", "info", "formatted")
// glg.Success("ok")
// glg.Successf("%s : %s", "info", "formatted")
// glg.Fail("fail")
// glg.Failf("%s : %s", "info", "formatted")
// glg.Print("Print")
// glg.Println("Println")
// glg.Printf("%s : %s", "printf", "formatted")
}
func glgDestination(glgLevel glg.LEVEL, desiredLevel uint) glg.MODE {
// Special case where we want ALL logging.
if desiredLevel == all {
return glg.BOTH
}
enabled := false
switch glgLevel {
case glg.DEBG:
enabled = desiredLevel <= debug
case glg.LOG:
enabled = desiredLevel <= log
case glg.INFO:
enabled = desiredLevel <= info
case glg.WARN:
enabled = desiredLevel <= warning
case glg.OK:
enabled = desiredLevel <= success
case glg.ERR:
enabled = desiredLevel <= err
}
// NOTE: Currently does not support sending log to ONLY the Writer,
// needs to be all or nothing right now
if enabled {
return glg.BOTH
}
return glg.NONE
}
// CloseLogger is responsible for closing any resources used for logging.
func CloseLogger() {
if infolog != nil {
infolog.Close()
}
}