-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
67 lines (58 loc) · 1.13 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
package logd
import (
"fmt"
"io"
"log"
"os"
)
const (
LINFO = "INFO"
LWARN = "WARNING"
LERR = "ERROR"
LFATAL = "FATAL"
)
type Logger struct {
l *log.Logger
out io.Writer
fail func(msg ...interface{})
}
func NewLogger(out io.Writer, f func(...interface{}), flag int) *Logger {
return &Logger{
l: log.New(out, "", flag),
out: out,
fail: f,
}
}
func (l *Logger) Close() error {
out, ok := l.out.(io.Closer)
if ok {
return out.Close()
}
return nil
}
func (l *Logger) InfoPrintln(msg ...interface{}) {
l.l.SetPrefix(fmt.Sprintf("%s: ", LINFO))
err := l.l.Output(2, fmt.Sprintln(msg...))
if err != nil {
l.fail(err)
}
}
func (l *Logger) WarnPrintln(msg ...interface{}) {
l.l.SetPrefix(fmt.Sprintf("%s: ", LWARN))
err := l.l.Output(2, fmt.Sprintln(msg...))
if err != nil {
l.fail(err)
}
}
func (l *Logger) ErrPrintln(msg ...interface{}) {
l.l.SetPrefix(fmt.Sprintf("%s: ", LERR))
err := l.l.Output(2, fmt.Sprintln(msg...))
if err != nil {
l.fail(err)
}
}
func (l *Logger) Fatal(msg ...interface{}) {
l.l.SetPrefix(fmt.Sprintf("%s: ", LFATAL))
l.l.Output(2, fmt.Sprintln(msg...))
os.Exit(1)
}