forked from thatguystone/cog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_human.go
66 lines (53 loc) · 1.16 KB
/
format_human.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
package clog
import (
"bytes"
"fmt"
"time"
"github.com/iheartradio/cog/cio/eio"
)
// HumanFormat formats log entries so that a human can quickly decipher them
type HumanFormat struct {
Args struct {
ShortTime bool // Format timestamp as "seconds since start"
}
}
var startTime = time.Now()
func init() {
RegisterFormatter("Human",
func(args eio.Args) (Formatter, error) {
f := HumanFormat{}
err := args.ApplyTo(&f.Args)
if err != nil {
return nil, err
}
return f, nil
})
}
// FormatEntry implements Formatter.FormatEntry
func (f HumanFormat) FormatEntry(e Entry) ([]byte, error) {
b := bytes.Buffer{}
timeS := ""
if f.Args.ShortTime {
timeS = fmt.Sprintf("%0.6d", e.Time.Sub(startTime)/time.Second)
} else {
timeS = e.Time.Format(time.StampMicro)
}
msg := ""
if len(e.Msg) > 0 {
msg = fmt.Sprintf(" %-44s", e.Msg)
}
fmt.Fprintf(&b, "[%s] %c-%s : %s :%s",
timeS,
e.Level.Rune(),
e.Module,
e.Src,
msg)
for k, v := range e.Data {
fmt.Fprintf(&b, " data.%s=%#v", k, v)
}
return bytes.TrimSpace(b.Bytes()), nil
}
// MimeType implements Formatter.MimeType
func (HumanFormat) MimeType() string {
return "text/plain"
}