forked from cnk3x/xunlei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
73 lines (62 loc) · 1.48 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
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"io"
"os"
"sync"
)
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
func Standard(prefix string, writers ...io.Writer) Logger {
var w io.Writer
if len(writers) > 1 {
w = io.MultiWriter(writers...)
} else if len(writers) == 1 {
w = writers[0]
}
return &std{prefix: prefix, out: w}
}
type std struct {
prefix string
out io.Writer
once sync.Once
}
func (l *std) output(tag, format string, args ...interface{}) {
l.once.Do(func() {
if l.out == nil {
l.out = os.Stderr
}
})
if format == l.prefix {
format = ""
}
sPrinter := fmt.Sprintf
// switch tag {
// case "DEBU":
// case "INFO":
// sPrinter = color.GreenString
// case "WARN":
// sPrinter = color.BlueString
// case "FATA":
// sPrinter = color.RedString
// }
// prefix := sPrinter("[%s] ", tag) + time.Now().Format(time.RFC3339) + sPrinter(" [%s] ", l.prefix)
fmt.Fprintf(l.out, sPrinter("[%s] ", l.prefix)+format+"\n", args...)
}
func (l *std) Debugf(format string, args ...interface{}) {
l.output("DEBU", format, args...)
}
func (l *std) Infof(format string, args ...interface{}) {
l.output("INFO", format, args...)
}
func (l *std) Warnf(format string, args ...interface{}) {
l.output("WARN", format, args...)
}
func (l *std) Fatalf(format string, args ...interface{}) {
l.output("FATA", format, args...)
os.Exit(1)
}