-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconsole.go
74 lines (61 loc) · 1.76 KB
/
console.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
package clog
import (
"log"
"github.com/fatih/color"
)
// consoleColors is the color set for different levels.
var consoleColors = []func(a ...interface{}) string{
color.New(color.FgBlue).SprintFunc(), // Trace
color.New(color.FgGreen).SprintFunc(), // Info
color.New(color.FgYellow).SprintFunc(), // Warn
color.New(color.FgRed).SprintFunc(), // Error
color.New(color.FgHiRed).SprintFunc(), // Fatal
}
// ConsoleConfig is the config object for the console logger.
type ConsoleConfig struct {
// Minimum logging level of messages to be processed.
Level Level
}
var _ Logger = (*consoleLogger)(nil)
type consoleLogger struct {
*noopLogger
*log.Logger
}
func (l *consoleLogger) Write(m Messager) error {
l.Print(consoleColors[m.Level()](m.String()))
return nil
}
// DefaultConsoleName is the default name for the console logger.
const DefaultConsoleName = "console"
// NewConsole initializes and appends a new console logger with default name
// to the managed list.
func NewConsole(vs ...interface{}) error {
return NewConsoleWithName(DefaultConsoleName, vs...)
}
// NewConsoleWithName initializes and appends a new console logger with given
// name to the managed list.
func NewConsoleWithName(name string, vs ...interface{}) error {
return New(name, ConsoleIniter(), vs...)
}
// ConsoleIniter returns the initer for the console logger.
func ConsoleIniter() Initer {
return func(name string, vs ...interface{}) (Logger, error) {
var cfg *ConsoleConfig
for i := range vs {
switch v := vs[i].(type) {
case ConsoleConfig:
cfg = &v
}
}
if cfg == nil {
cfg = &ConsoleConfig{}
}
return &consoleLogger{
noopLogger: &noopLogger{
name: name,
level: cfg.Level,
},
Logger: log.New(color.Output, "", log.Ldate|log.Ltime),
}, nil
}
}