-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
93 lines (76 loc) · 1.72 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gestalt
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
)
type Logger interface {
Log() logrus.FieldLogger
CloneFor(string) Logger
Clone() Logger
Start()
Message(string, ...interface{})
Dump(string)
Stop(error)
}
type logger struct {
path string
log logrus.FieldLogger
out io.Writer
logOut io.Writer
}
func (l *logger) Log() logrus.FieldLogger {
return l.log
}
func (l *logger) Start() {
fmt.Fprintf(l.out, "%v [start]\n", l.path)
}
func (l *logger) Message(msg string, args ...interface{}) {
fmt.Fprintf(l.out, "%v: %v\n", l.path, fmt.Sprintf(msg, args...))
}
func (l *logger) Dump(msg string) {
scanner := bufio.NewScanner(bytes.NewBuffer([]byte(msg)))
for scanner.Scan() {
color.New(color.FgWhite, color.Bold).Fprintf(l.logOut, "%v: ", l.path)
l.logOut.Write(scanner.Bytes())
l.logOut.Write([]byte("\n"))
}
}
func (l *logger) Stop(err error) {
if err == nil {
fmt.Fprintf(l.out, "%v: [complete]\n", l.path)
} else {
fmt.Fprintf(l.out, "%v: [error: %v]\n", l.path, err)
}
}
func (l *logger) CloneFor(path string) Logger {
return &logger{path, l.log.WithField("path", path), l.out, l.logOut}
}
func (l *logger) Clone() Logger {
return &logger{l.path, l.log, l.out, l.logOut}
}
type logBuilder struct {
log *logrus.Logger
}
func newLogBuilder() *logBuilder {
l := logrus.New()
l.Level = logrus.PanicLevel
return &logBuilder{
log: l,
}
}
func (lb *logBuilder) WithLogOut(o io.Writer) *logBuilder {
lb.log.Out = o
return lb
}
func (lb *logBuilder) WithLevel(level string) *logBuilder {
lb.log.Level, _ = logrus.ParseLevel(level)
return lb
}
func (lb *logBuilder) Logger() Logger {
return &logger{"", lb.log, os.Stdout, lb.log.Out}
}