-
Notifications
You must be signed in to change notification settings - Fork 3
/
logrus_lumberjack.go
54 lines (45 loc) · 1.09 KB
/
logrus_lumberjack.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 logrus_lumberjack
import (
"fmt"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/sirupsen/logrus"
)
// LumberjackHook stores the configuration of the hook
type LumberjackHook struct {
logger *lumberjack.Logger
}
// NewLumberjackHook creates a new LumberjackHook
func NewLumberjackHook(logger *lumberjack.Logger) (*LumberjackHook, error) {
if logger == nil {
return nil, fmt.Errorf("Logger cannot be nil")
}
return &LumberjackHook{
logger: logger,
}, nil
}
// Fire is called when a log event is fired.
func (hook *LumberjackHook) Fire(entry *logrus.Entry) error {
// Convert the line to string
line, err := entry.String()
if err != nil {
return err
}
// Write the the logger
_, err = hook.logger.Write([]byte(line))
if err != nil {
return err
}
return nil
}
// Levels returns the available logging levels
func (hook *LumberjackHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.TraceLevel,
logrus.DebugLevel,
logrus.InfoLevel,
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}