2015-05-14 13:20:00 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-04-20 14:26:39 -04:00
|
|
|
package journald
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-09 00:23:30 -04:00
|
|
|
"io"
|
2015-04-20 14:26:39 -04:00
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-20 14:26:39 -04:00
|
|
|
"github.com/coreos/go-systemd/journal"
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
)
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
const name = "journald"
|
|
|
|
|
2015-04-20 14:26:39 -04:00
|
|
|
type Journald struct {
|
|
|
|
Jmap map[string]string
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func init() {
|
|
|
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx logger.Context) (logger.Logger, error) {
|
2015-04-20 14:26:39 -04:00
|
|
|
if !journal.Enabled() {
|
|
|
|
return nil, fmt.Errorf("journald is not enabled on this host")
|
|
|
|
}
|
2015-04-28 21:23:27 -04:00
|
|
|
// Strip a leading slash so that people can search for
|
|
|
|
// CONTAINER_NAME=foo rather than CONTAINER_NAME=/foo.
|
2015-04-09 00:23:30 -04:00
|
|
|
name := ctx.ContainerName
|
2015-04-28 21:23:27 -04:00
|
|
|
if name[0] == '/' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
jmap := map[string]string{
|
2015-04-09 00:23:30 -04:00
|
|
|
"CONTAINER_ID": ctx.ContainerID[:12],
|
|
|
|
"CONTAINER_ID_FULL": ctx.ContainerID,
|
2015-04-28 21:23:27 -04:00
|
|
|
"CONTAINER_NAME": name}
|
2015-04-20 14:26:39 -04:00
|
|
|
return &Journald{Jmap: jmap}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Journald) Log(msg *logger.Message) error {
|
|
|
|
if msg.Source == "stderr" {
|
|
|
|
return journal.Send(string(msg.Line), journal.PriErr, s.Jmap)
|
|
|
|
}
|
|
|
|
return journal.Send(string(msg.Line), journal.PriInfo, s.Jmap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Journald) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Journald) Name() string {
|
2015-04-09 00:23:30 -04:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Journald) GetReader() (io.Reader, error) {
|
|
|
|
return nil, logger.ReadLogsNotSupported
|
2015-04-20 14:26:39 -04:00
|
|
|
}
|