-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage.go
More file actions
50 lines (42 loc) · 908 Bytes
/
message.go
File metadata and controls
50 lines (42 loc) · 908 Bytes
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 logger
import (
"bytes"
"fmt"
"html/template"
"time"
)
const defaultTimeFormat = "15:04:05.000"
//go:generate evt2gogen -t Message
type Message struct {
Timestamp time.Time
Prefix string
Level Level
Msg string
Nr uint64
tmpl *template.Template
}
func NewMessage(nr uint64, prefix string, lvl Level, tmpl *template.Template, format string, val ...interface{}) (m Message) {
m = Message{
Timestamp: time.Now(),
Prefix: prefix,
Level: lvl,
Msg: fmt.Sprintf(format, val...),
Nr: nr,
tmpl: tmpl,
}
return
}
func (m Message) Format() string {
var buf bytes.Buffer
m.tmpl.Execute(&buf, m)
return Paint(
fmt.Sprintf(buf.String(), m.Msg),
m.Level.Foreground,
m.Level.ForegroundHi,
m.Level.Background,
m.Level.BackgroundHi,
)
}
func (m Message) FormattedTime() string {
return m.Timestamp.Format(defaultTimeFormat)
}