forked from mongodb/grip
/
base.go
80 lines (64 loc) · 2.05 KB
/
base.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 message
import (
"fmt"
"os"
"time"
"github.com/mongodb/grip/level"
)
// Base provides a simple embedable implementation of some common
// aspects of a message.Composer. Additionally the Collect() method
// collects some simple metadata, that may be useful for some more
// structured logging applications.
type Base struct {
Level level.Priority `bson:"level,omitempty" json:"level,omitempty" yaml:"level,omitempty"`
Hostname string `bson:"hostname,omitempty" json:"hostname,omitempty" yaml:"hostname,omitempty"`
Time time.Time `bson:"time,omitempty" json:"time,omitempty" yaml:"time,omitempty"`
Process string `bson:"process,omitempty" json:"process,omitempty" yaml:"process,omitempty"`
Pid int `bson:"pid,omitempty" json:"pid,omitempty" yaml:"pid,omitempty"`
Context Fields `bson:"context,omitempty" json:"context,omitempty" yaml:"context,omitempty"`
}
// Collect records the time, process name, and hostname. Useful in the
// context of a Raw() method.
func (b *Base) Collect() error {
if b.Pid > 0 {
return nil
}
var err error
b.Hostname, err = os.Hostname()
if err != nil {
return err
}
b.Time = time.Now()
b.Process = os.Args[0]
b.Pid = os.Getpid()
return nil
}
// Priority returns the configured priority of the message.
func (b *Base) Priority() level.Priority {
return b.Level
}
// SetPriority allows you to configure the priority of the
// message. Returns an error if the priority is not valid.
func (b *Base) SetPriority(l level.Priority) error {
if !level.IsValidPriority(l) {
return fmt.Errorf("%s (%d) is not a valid priority", l, l)
}
b.Level = l
return nil
}
// Annotate makes it possible for callers and senders to add
// structured data to a message. This may be overridden for some
// implementations
func (b *Base) Annotate(key string, value interface{}) error {
if b.Context == nil {
b.Context = Fields{
key: value,
}
return nil
}
if _, ok := b.Context[key]; ok {
return fmt.Errorf("key '%s' already exists", key)
}
b.Context[key] = value
return nil
}