-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
68 lines (59 loc) · 1.65 KB
/
entry.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
68
// This file was generated with level_generator. DO NOT EDIT
package verbose
import (
"fmt"
"time"
)
// Entry represents a log message going through the system
type Entry struct {
Level LogLevel
Timestamp time.Time
Logger *Logger
Message string
Data Fields
}
// NewEntry creates a new, empty Entry
func NewEntry(l *Logger) *Entry {
return &Entry{
Logger: l,
Data: make(Fields, 5),
}
}
// WithField adds a single field to the Entry.
func (e *Entry) WithField(key string, value interface{}) *Entry {
return e.WithFields(Fields{key: value})
}
// WithFields adds a map of fields to the Entry.
func (e *Entry) WithFields(fields Fields) *Entry {
data := make(Fields, len(e.Data)+len(fields))
for k, v := range e.Data {
data[k] = v
}
for k, v := range fields {
data[k] = v
}
return &Entry{Logger: e.Logger, Data: data}
}
// Log is the generic function to log a message with the handlers.
// All other logging functions are simply wrappers around this.
func (e *Entry) log(level LogLevel, msg string) {
e.Logger.m.RLock()
e.Level = level
e.Message = msg
e.Timestamp = time.Now()
for _, h := range e.Logger.handlers {
if h.Handles(level) {
h.WriteLog(e)
}
}
e.Logger.m.RUnlock()
}
// sprintlnn take from Logrus: github.com/Sirupsen/logrus entry.go
// Sprintlnn => Sprint no newline. This is to get the behavior of how
// fmt.Sprintln where spaces are always added between operands, regardless of
// their type. Instead of vendoring the Sprintln implementation to spare a
// string allocation, we do the simplest thing.
func (e *Entry) sprintlnn(args ...interface{}) string {
msg := fmt.Sprintln(args...)
return msg[:len(msg)-1]
}