forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logentries.go
113 lines (100 loc) · 2.56 KB
/
logentries.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Package logentries provides the log driver for forwarding server logs
// to logentries endpoints.
package logentries
import (
"fmt"
"strconv"
"github.com/bsphere/le_go"
"github.com/docker/docker/daemon/logger"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type logentries struct {
tag string
containerID string
containerName string
writer *le_go.Logger
extra map[string]string
lineOnly bool
}
const (
name = "logentries"
token = "logentries-token"
lineonly = "line-only"
)
func init() {
if err := logger.RegisterLogDriver(name, New); err != nil {
logrus.Fatal(err)
}
if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
logrus.Fatal(err)
}
}
// New creates a logentries logger using the configuration passed in on
// the context. The supported context configuration variable is
// logentries-token.
func New(info logger.Info) (logger.Logger, error) {
logrus.WithField("container", info.ContainerID).
WithField("token", info.Config[token]).
WithField("line-only", info.Config[lineonly]).
Debug("logging driver logentries configured")
log, err := le_go.Connect(info.Config[token])
if err != nil {
return nil, errors.Wrap(err, "error connecting to logentries")
}
var lineOnly bool
if lineOnly, err = strconv.ParseBool(info.Config[lineonly]); err != nil {
return nil, errors.Wrap(err, "error parsing lineonly option")
}
return &logentries{
containerID: info.ContainerID,
containerName: info.ContainerName,
writer: log,
lineOnly: lineOnly,
}, nil
}
func (f *logentries) Log(msg *logger.Message) error {
if !f.lineOnly {
data := map[string]string{
"container_id": f.containerID,
"container_name": f.containerName,
"source": msg.Source,
"log": string(msg.Line),
}
for k, v := range f.extra {
data[k] = v
}
ts := msg.Timestamp
logger.PutMessage(msg)
f.writer.Println(f.tag, ts, data)
} else {
line := msg.Line
logger.PutMessage(msg)
f.writer.Println(line)
}
return nil
}
func (f *logentries) Close() error {
return f.writer.Close()
}
func (f *logentries) Name() string {
return name
}
// ValidateLogOpt looks for logentries specific log option logentries-address.
func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
case "env":
case "env-regex":
case "labels":
case "tag":
case key:
default:
return fmt.Errorf("unknown log opt '%s' for logentries log driver", key)
}
}
if cfg[token] == "" {
return fmt.Errorf("Missing logentries token")
}
return nil
}