-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.go
More file actions
84 lines (73 loc) · 1.95 KB
/
redirection.go
File metadata and controls
84 lines (73 loc) · 1.95 KB
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
package errorutils
import (
"bytes"
"fmt"
"io"
"github.com/sirupsen/logrus"
)
type logAction int
const (
Redirect logAction = iota
Suppress
)
func Change(l logrus.Level, a logAction) {
customFormatter.Actions[l] = a
}
func ChangeWriter(l logrus.Level, w io.Writer) {
customFormatter.Writers[l] = w
}
func ToggleColor() (isColorfull bool) {
customFormatter.Color = !customFormatter.Color
logrus.SetFormatter(customFormatter)
return customFormatter.Color
}
func init() {
logrus.SetFormatter(customFormatter)
centralB_ch = make(chan activityEvent, 200)
//fix duplication issue
// logrus.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr
// Writer: os.Stderr,
// LogLevels: []logrus.Level{
// logrus.PanicLevel,
// logrus.FatalLevel,
// logrus.ErrorLevel,
// logrus.WarnLevel,
// },
// })
// logrus.AddHook(&writer.Hook{ // Send info and debug logs to stdout
// Writer: os.Stdout,
// LogLevels: []logrus.Level{
// logrus.InfoLevel,
// logrus.DebugLevel,
// },
// })
}
type MyFormatter struct {
F logrus.Formatter
Actions map[logrus.Level]logAction
TimestampFormat string
Writers map[logrus.Level]io.Writer
Color bool
}
var customFormatter = &MyFormatter{
F: nil,
Actions: make(map[logrus.Level]logAction),
//human time
TimestampFormat: "2006-01-02 15:04:05",
Color: true,
}
func (f *MyFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
if action, ok := f.Actions[entry.Level]; ok {
switch action {
case Redirect:
//alternatively user writter hooks.
f.Writers[entry.Level].Write([]byte(fmt.Sprintf("%s [%s] %s\n", entry.Time.Format(f.TimestampFormat), makeupL(entry.Level, false), entry.Message)))
return nil, nil
case Suppress:
return nil, nil
}
}
b.WriteString(fmt.Sprintf("%s [%s] %s\n", entry.Time.Format(f.TimestampFormat), makeupL(entry.Level, f.Color), entry.Message))
return b.Bytes(), nil
}