/
console_hook.go
45 lines (38 loc) · 1021 Bytes
/
console_hook.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
package multilogger
import (
"io"
"github.com/sirupsen/logrus"
)
type consoleI interface {
SetOut(io.Writer)
SetStdout(io.Writer)
}
type consoleHook struct {
*genericHook
out io.Writer
log io.Writer
}
func (hook *consoleHook) clone() logrus.Hook {
// Duplicate the console hook to ensure that the copy
// has its own attributes when the object is copied.
return &consoleHook{
genericHook: hook.genericHook.clone(),
out: hook.out,
log: hook.log,
}
}
func (hook *consoleHook) Fire(entry *logrus.Entry) (err error) {
return hook.fire(entry, func() error {
const name = "ConsoleHook"
if entry.Level == outputLevel {
return hook.printf(name, hook.out, entry.Message)
}
var formatted string
if formatted, err = hook.formatEntry(name, entry); err != nil {
return err
}
return hook.printf(name, hook.log, string(formatted))
})
}
func (hook *consoleHook) SetOut(out io.Writer) { hook.log = out }
func (hook *consoleHook) SetStdout(out io.Writer) { hook.out = out }