-
Notifications
You must be signed in to change notification settings - Fork 3
/
entry.go
80 lines (65 loc) · 1.7 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
69
70
71
72
73
74
75
76
77
78
79
80
package notify
// Level is the notification level
type Level uint32
const (
// LevelDebug is for development notifications
LevelDebug Level = iota
// LevelInfo is for notifictions which are non-critical
LevelInfo
// LevelWarn is for notifications which may be troublesome
LevelWarn
// LevelError is for notifications which is crtical
LevelError
// LevelFatal is for notifications which are fatal
LevelFatal
)
func (l Level) String() string {
switch l {
case LevelDebug:
return "debug"
case LevelInfo:
return "info"
case LevelWarn:
return "warn"
case LevelFatal:
return "fatal"
default:
return "unknown"
}
}
// MarshalJSON returns the notification level as a string
func (l Level) MarshalJSON() ([]byte, error) {
return []byte("\"" + l.String() + "\""), nil
}
// Fields is a collection of key, value pairs
type Fields map[string]interface{}
// Entry is a single instance of a notification
type Entry struct {
Context
Message string `json:"message"`
Level Level `json:"level"`
}
// Error returns a string representation of the notification
func (e *Entry) Error() string {
return e.Message
}
// WithField creates a new context with a single field
func WithField(name string, value interface{}) Context {
return empty().addField(name, value)
}
// WithFields creates a new context with multiple fields
func WithFields(f Fields) Context {
c := empty()
for k, v := range f {
c.addField(k, v)
}
return c
}
// WithError creates a new context with an error
func WithError(err error) Context {
return empty().addField("error", err)
}
// WithExtra creates a new context with an extra property
func WithExtra(name string, value interface{}) Context {
return empty().addExtra(name, value)
}