forked from dogenzaka/ruslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
53 lines (42 loc) · 1.04 KB
/
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
45
46
47
48
49
50
51
52
53
package ruslog
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
)
const defaultTimestampFormat = time.RFC3339
const (
// formatter types
FORMATTER_SIMPLE = "Simple"
FORMATTER_JSON = "JSON"
FORMATTER_TEXT = "Text"
)
type (
Formatter struct {
Name string
Formatter logrus.Formatter
}
SimpleFormatter struct {
// TimestampFormat sets the format used for marshaling timestamps.
TimestampFormat string
}
)
func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = defaultTimestampFormat
}
fmt.Fprintf(b, "[%s]", entry.Time.Format(timestampFormat))
fmt.Fprintf(b, " [%s]", strings.ToUpper(entry.Level.String()))
fmt.Fprintf(b, " %s", entry.Message)
for key, value := range entry.Data {
if key != "time" && key != "level" && key != "msg" {
fmt.Fprintf(b, " %s=%s", key, value)
}
}
b.WriteByte('\n')
return b.Bytes(), nil
}