-
Notifications
You must be signed in to change notification settings - Fork 13
/
log.go
54 lines (44 loc) · 1.35 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
package marker
import (
"io"
"os"
"github.com/fatih/color"
)
// WriteMarkerOption is functional option type for WriteMarker
type WriteMarkerOption func(*WriteMarker)
// MarkRule contains marking information to be applied on log stream
type MarkRule struct {
Matcher MatcherFunc
Color *color.Color
}
// WriteMarker contains specified rules for applying them on output
type WriteMarker struct {
rules []MarkRule
out io.Writer
}
// NewWriteMarker creates a Marker that writes out to the given io.Writer
func NewWriteMarker(writer io.Writer) *WriteMarker {
logMarker := &WriteMarker{out: writer}
return logMarker
}
// NewStdoutMarker creates a WriteMarker with default out as os.Stdout
func NewStdoutMarker() *WriteMarker {
return NewWriteMarker(os.Stdout)
}
// AddRule appends a rule to WriteMarker and returns itself
func (s *WriteMarker) AddRule(rule MarkRule) *WriteMarker {
s.rules = append(s.rules, rule)
return s
}
// AddRules appends all rules from given slice to WriteMarker rules
func (s *WriteMarker) AddRules(rules []MarkRule) {
s.rules = append(s.rules, rules...)
}
// Write marks the text with specified rules and writes the output to specifed out
func (s WriteMarker) Write(p []byte) (n int, err error) {
marked := string(p)
for _, rule := range s.rules {
marked = Mark(marked, rule.Matcher, rule.Color)
}
return s.out.Write([]byte(marked))
}