This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
loglevel.go
77 lines (63 loc) · 1.78 KB
/
loglevel.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
package logger
import (
"fmt"
"strings"
)
type printers struct {
debug, info, success, warn, error *parameterizableLogger
}
var ls printers
func init() {
configureLogLevel("info")
}
// Debug logging.
func Debug(v ...interface{}) {
ls.debug.print(v...)
}
// Info logging.
func Info(v ...interface{}) {
ls.info.print(v...)
}
// Success logging in green.
func Success(v ...interface{}) {
ls.success.print(v...)
}
// Warn logging in yellow.
func Warn(v ...interface{}) {
ls.warn.print(v...)
}
// Error logging in red.
func Error(v ...interface{}) {
ls.error.print(v...)
}
type levelLookupTable map[string]*printers
var levelNames = levelLookupTable{
"debug": &printers{debug: dark(), info: plain(), success: green(), warn: yellow(), error: red()},
"info": &printers{debug: nop(), info: plain(), success: green(), warn: yellow(), error: red()},
"warn": &printers{debug: nop(), info: nop(), success: nop(), warn: yellow(), error: red()},
"error": &printers{debug: nop(), info: nop(), success: nop(), warn: nop(), error: red()},
"none": &printers{debug: nop(), info: nop(), success: nop(), warn: nop(), error: nop()},
}
// configureLogLevel configures the loglevel identified by its name. It returns an error if the given name is unknown.
func configureLogLevel(name string) error {
l, err := byName(name)
if err != nil {
return fmt.Errorf("error determining loglevel details for name '%s': %v", name, err)
}
ls = *l
return nil
}
func byName(name string) (*printers, error) {
level, ok := levelNames[strings.ToLower(name)]
if !ok {
return nil, fmt.Errorf("'%s' is not a valid loglevel, possible values are %s", name, levelNames.keys())
}
return level, nil
}
func (l *levelLookupTable) keys() []string {
keys := []string{}
for k := range *l {
keys = append(keys, k)
}
return keys
}