forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
50 lines (41 loc) · 1.08 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
package slog
//
// Formatter interface
//
// Formatter interface
type Formatter interface {
// Format you can format record and write result to record.Buffer
Format(record *Record) ([]byte, error)
}
// FormatterFunc wrapper definition
type FormatterFunc func(r *Record) ([]byte, error)
// Format a log record
func (fn FormatterFunc) Format(r *Record) ([]byte, error) {
return fn(r)
}
// FormattableHandler interface
type FormattableHandler interface {
// Formatter get the log formatter
Formatter() Formatter
// SetFormatter set the log formatter
SetFormatter(Formatter)
}
// Formattable definition
type Formattable struct {
formatter Formatter
}
// Formatter get formatter. if not set, will return TextFormatter
func (f *Formattable) Formatter() Formatter {
if f.formatter == nil {
f.formatter = NewTextFormatter()
}
return f.formatter
}
// SetFormatter to handler
func (f *Formattable) SetFormatter(formatter Formatter) {
f.formatter = formatter
}
// Format log record to bytes
func (f *Formattable) Format(record *Record) ([]byte, error) {
return f.Formatter().Format(record)
}