-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrus_writer.go
146 lines (117 loc) · 2.99 KB
/
logrus_writer.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package log
import (
"errors"
"github.com/sirupsen/logrus"
"gopkg.in/go-mixed/framework.v1/facades/config"
"io"
"gopkg.in/go-mixed/framework.v1/contracts/log"
"gopkg.in/go-mixed/framework.v1/log/logger"
)
type Writer struct {
instance *logrus.Entry
}
func NewWriter(instance *logrus.Entry) log.Writer {
return &Writer{instance: instance}
}
func (r *Writer) Debug(args ...any) {
r.instance.Debug(args...)
}
func (r *Writer) Debugf(format string, args ...any) {
r.instance.Debugf(format, args...)
}
func (r *Writer) Info(args ...any) {
r.instance.Info(args...)
}
func (r *Writer) Infof(format string, args ...any) {
r.instance.Infof(format, args...)
}
func (r *Writer) Warning(args ...any) {
r.instance.Warning(args...)
}
func (r *Writer) Warningf(format string, args ...any) {
r.instance.Warningf(format, args...)
}
func (r *Writer) Error(args ...any) {
r.instance.Error(args...)
}
func (r *Writer) Errorf(format string, args ...any) {
r.instance.Errorf(format, args...)
}
func (r *Writer) Fatal(args ...any) {
r.instance.Fatal(args...)
}
func (r *Writer) Fatalf(format string, args ...any) {
r.instance.Fatalf(format, args...)
}
func (r *Writer) Panic(args ...any) {
r.instance.Panic(args...)
}
func (r *Writer) Panicf(format string, args ...any) {
r.instance.Panicf(format, args...)
}
func registerHook(instance *logrus.Logger, channel string) error {
channelPath := "logging.channels." + channel
driver := config.GetString(channelPath + ".driver")
var hook logrus.Hook
var err error
switch driver {
case log.StackDriver:
for _, stackChannel := range config.Get(channelPath + ".channels").([]string) {
if stackChannel == channel {
return errors.New("stack drive can't include self channel")
}
if err := registerHook(instance, stackChannel); err != nil {
return err
}
}
return nil
case log.SingleDriver:
if !config.GetBool(channelPath + ".print") {
instance.SetOutput(io.Discard)
}
logLogger := &logger.Single{}
hook, err = logLogger.Handle(channelPath)
if err != nil {
return err
}
case log.DailyDriver:
if !config.GetBool(channelPath + ".print") {
instance.SetOutput(io.Discard)
}
logLogger := &logger.Daily{}
hook, err = logLogger.Handle(channelPath)
if err != nil {
return err
}
case log.CustomDriver:
logLogger := config.Get(channelPath + ".via").(log.ILogger)
logHook, err := logLogger.Handle(channelPath)
if err != nil {
return err
}
hook = &Hook{logHook}
default:
return errors.New("Error logging channel: " + channel)
}
instance.AddHook(hook)
return nil
}
type Hook struct {
instance log.Hook
}
func (h *Hook) Levels() []logrus.Level {
levels := h.instance.Levels()
var logrusLevels []logrus.Level
for _, item := range levels {
logrusLevels = append(logrusLevels, logrus.Level(item))
}
return logrusLevels
}
func (h *Hook) Fire(entry *logrus.Entry) error {
return h.instance.Fire(&Entry{
ctx: entry.Context,
level: log.Level(entry.Level),
time: entry.Time,
message: entry.Message,
})
}