-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
logger.go
55 lines (46 loc) · 1.07 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
package ecspresso
import (
"encoding/json"
"io"
"log"
"os"
"github.com/fatih/color"
"github.com/fujiwara/logutils"
)
var (
commonLogger *log.Logger
)
func init() {
commonLogger = newLogger()
commonLogger.SetOutput(newLogFilter(os.Stderr, "INFO"))
}
func newLogFilter(w io.Writer, minLevel string) *logutils.LevelFilter {
return &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARNING", "ERROR"},
ModifierFuncs: []logutils.ModifierFunc{
nil, // DEBUG
nil, // default
logutils.Color(color.FgYellow),
logutils.Color(color.FgRed),
},
MinLevel: logutils.LogLevel(minLevel),
Writer: w,
}
}
func newLogger() *log.Logger {
return log.New(io.Discard, "", log.LstdFlags)
}
func Log(f string, v ...interface{}) {
commonLogger.Printf(f, v...)
}
func (d *App) Log(f string, v ...interface{}) {
d.logger.Printf(d.Name()+" "+f, v...)
}
func (d *App) LogJSON(v interface{}) {
b, err := json.Marshal(v)
if err != nil {
d.logger.Printf("[WARNING] failed to marshal json: %s", err)
return
}
d.logger.Println("[DEBUG] " + string(b))
}