forked from codesuki/ecs-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
65 lines (54 loc) · 1.3 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
58
59
60
61
62
63
64
65
package main
import (
"io/ioutil"
"log"
"os"
)
type localLog struct {
infoLog *log.Logger
warningLog *log.Logger
errorLog *log.Logger
fatalLog *log.Logger
}
const defaultFlags = log.Ltime
const (
levelInfo = "info"
levelWarn = "warn"
levelError = "error"
)
func newLogger(level string) *localLog {
//fatal will always output to stderr no matter the logLevel
infoWriter := ioutil.Discard
warnWriter := ioutil.Discard
errorWriter := ioutil.Discard
fatalWriter := os.Stderr
switch level {
case levelInfo:
infoWriter = os.Stdout
fallthrough
case levelWarn:
warnWriter = os.Stdout
fallthrough
case levelError:
errorWriter = os.Stderr
}
Logger := localLog{
infoLog: log.New(infoWriter, "[INFO]\t", defaultFlags),
warningLog: log.New(warnWriter, "[WARN]\t", defaultFlags),
errorLog: log.New(errorWriter, "[ERROR]\t", defaultFlags),
fatalLog: log.New(fatalWriter, "[FATAL]\t", defaultFlags),
}
return &Logger
}
func (l *localLog) Info(format string, v ...interface{}) {
l.infoLog.Printf(format, v...)
}
func (l *localLog) Warning(format string, v ...interface{}) {
l.warningLog.Printf(format, v...)
}
func (l *localLog) Error(v ...interface{}) {
l.errorLog.Print(v...)
}
func (l *localLog) Fatal(format string, v ...interface{}) {
l.fatalLog.Fatalf(format, v...)
}