-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_formatter.go
44 lines (34 loc) · 930 Bytes
/
json_formatter.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
package hook
import (
"fmt"
"github.com/sirupsen/logrus"
)
// JSONFormatterCode formatter code to identify from config.
const JSONFormatterCode = "json"
// DefaultJSONFormatter used as default JSONFormatter.
var DefaultJSONFormatter = &JSONFormatter{
JSONFormatter: logrus.JSONFormatter{
TimestampFormat: DefaultTimeLayout,
},
Quoted: true,
}
// JSONFormatter formats logs into parsable json.
type JSONFormatter struct {
logrus.JSONFormatter
// Quoted will quote string to discord tag json.
Quoted bool
}
// Format renders a single log entry.
func (f *JSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
data, err := f.JSONFormatter.Format(entry)
if err != nil {
return data, fmt.Errorf("discordbotrus: %w", err)
}
if f.Quoted {
data = []byte("```json\n" + string(data) + "```")
}
if len([]rune(string(data))) > DiscordMaxMessageLen {
return data, ErrMessageTooLong
}
return data, nil
}