forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_hook.go
61 lines (55 loc) · 1.27 KB
/
logger_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cli
import (
"io"
"github.com/briandowns/spinner"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
)
type spinnerHook struct {
logger *logrus.Logger
spinner *spinner.Spinner
}
func newSpinnerHandlerHook(parent *logrus.Logger, spinner *spinner.Spinner, isTerminal, noColor bool) *spinnerHook {
logger := logrus.New()
logger.Out = parent.Out
if parent.Out != io.Discard {
if isTerminal {
if noColor {
logger.Formatter = &logrus.TextFormatter{
DisableColors: true,
DisableTimestamp: true,
}
} else {
logger.Formatter = &logrus.TextFormatter{
ForceColors: true,
DisableTimestamp: true,
}
}
logger.Out = colorable.NewColorableStderr()
} else {
logger.Formatter = &logrus.JSONFormatter{
PrettyPrint: false,
}
}
logger.Level = parent.GetLevel()
}
return &spinnerHook{
logger: logger,
spinner: spinner,
}
}
// Levels returns all levels this hook should be registered to
func (hook *spinnerHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// Fire is triggered on new log entries
func (hook *spinnerHook) Fire(entry *logrus.Entry) error {
if hook.spinner.Active() {
hook.spinner.Stop()
defer func() {
hook.spinner.Start()
}()
}
entry.Logger = hook.logger
return nil
}