From 2764d67c3a85cf638e211a1cfb0808e9eba7edb9 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 12 May 2016 11:18:08 -0300 Subject: [PATCH] added logentries driver Signed-off-by: Carlos Alexandro Becker --- daemon/logdrivers_linux.go | 1 + daemon/logdrivers_windows.go | 1 + daemon/logger/logentries/logentries.go | 94 ++++++++++++++++++++++++++ docs/admin/logging/logentries.md | 42 ++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 daemon/logger/logentries/logentries.go create mode 100644 docs/admin/logging/logentries.md diff --git a/daemon/logdrivers_linux.go b/daemon/logdrivers_linux.go index 89fe49a858..ad343c1e8e 100644 --- a/daemon/logdrivers_linux.go +++ b/daemon/logdrivers_linux.go @@ -9,6 +9,7 @@ import ( _ "github.com/docker/docker/daemon/logger/gelf" _ "github.com/docker/docker/daemon/logger/journald" _ "github.com/docker/docker/daemon/logger/jsonfilelog" + _ "github.com/docker/docker/daemon/logger/logentries" _ "github.com/docker/docker/daemon/logger/splunk" _ "github.com/docker/docker/daemon/logger/syslog" ) diff --git a/daemon/logdrivers_windows.go b/daemon/logdrivers_windows.go index 8337c0f0a8..646046814f 100644 --- a/daemon/logdrivers_windows.go +++ b/daemon/logdrivers_windows.go @@ -6,6 +6,7 @@ import ( _ "github.com/docker/docker/daemon/logger/awslogs" _ "github.com/docker/docker/daemon/logger/etwlogs" _ "github.com/docker/docker/daemon/logger/jsonfilelog" + _ "github.com/docker/docker/daemon/logger/logentries" _ "github.com/docker/docker/daemon/logger/splunk" _ "github.com/docker/docker/daemon/logger/syslog" ) diff --git a/daemon/logger/logentries/logentries.go b/daemon/logger/logentries/logentries.go new file mode 100644 index 0000000000..e794b1ed08 --- /dev/null +++ b/daemon/logger/logentries/logentries.go @@ -0,0 +1,94 @@ +// Package logentries provides the log driver for forwarding server logs +// to logentries endpoints. +package logentries + +import ( + "fmt" + + "github.com/Sirupsen/logrus" + "github.com/bsphere/le_go" + "github.com/docker/docker/daemon/logger" +) + +type logentries struct { + tag string + containerID string + containerName string + writer *le_go.Logger + extra map[string]string +} + +const ( + name = "logentries" + token = "logentries-token" +) + +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(ctx logger.Context) (logger.Logger, error) { + logrus.WithField("container", ctx.ContainerID). + WithField("token", ctx.Config[token]). + Debug("logging driver logentries configured") + + log, err := le_go.Connect(ctx.Config[token]) + if err != nil { + return nil, err + } + return &logentries{ + containerID: ctx.ContainerID, + containerName: ctx.ContainerName, + writer: log, + }, nil +} + +func (f *logentries) Log(msg *logger.Message) error { + 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 + } + f.writer.Println(f.tag, msg.Timestamp, data) + 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 "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 +} diff --git a/docs/admin/logging/logentries.md b/docs/admin/logging/logentries.md new file mode 100644 index 0000000000..6bbee5d544 --- /dev/null +++ b/docs/admin/logging/logentries.md @@ -0,0 +1,42 @@ + + +# Logentries logging driver + +The `logentries` logging driver sends container logs to the Logentries server. + +## Usage + +You can configure the default logging driver by passing the `--log-driver` +option to the Docker daemon: + + docker daemon --log-driver=logentries + +You can set the logging driver for a specific container by using the +`--log-driver` option to `docker run`: + + docker run --log-driver=logentries ... + +## Logentries options + +You can use the `--log-opt NAME=VALUE` flag to specify these additional +Logentries logging driver options: + +| Option | Required | Description | +|-----------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `logentries-token` | required | Logentries token. | + +An example usage would be somethig like: + + docker run --log-driver=logentries \ + --log-opt logentries-token=176FCEBF-4CF5-4EDF-91BC-703796522D20 \ + your/application