-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
67 lines (61 loc) · 1.27 KB
/
message.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
67
package loggo
import (
"bytes"
"fmt"
"github.com/mgutz/ansi"
"strings"
"text/template"
"time"
)
// Message is the structure representing a single log message
type Message struct {
// The name of the logger
Name string
// The level of the message
Level Level
// The content of the log
Content interface{}
// The time of the log
Time time.Time
// The file of the log call
File string
// The line number of the log call
Line int
// The function name of the log call
FuncName string
dateFormat string
padding bool
color bool
tpl *template.Template
}
// NameUp returns the logger name upper cased
func (m *Message) NameUp() string {
return strings.ToUpper(m.Name)
}
// LevelStr formats the log level
func (m *Message) LevelStr() string {
str := m.Level.String()
if m.padding {
for len(str) < 7 {
str += " "
}
}
return str
}
// String returns a formatted representation of the message
func (m *Message) String() string {
buffer := bytes.NewBufferString("")
err := m.tpl.Execute(buffer, m)
if err != nil {
return fmt.Sprintf("%s\n", m.Content)
}
str := buffer.String()
if m.color {
str = ansi.Color(str, Colors[m.Level])
}
return str
}
// TimeStr formats the time
func (m *Message) TimeStr() string {
return m.Time.Format(m.dateFormat)
}