github.com/ld86/docker@v1.7.1-rc3/daemon/logger/journald/journald.go (about)

     1  // +build linux
     2  
     3  package journald
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/coreos/go-systemd/journal"
    11  	"github.com/docker/docker/daemon/logger"
    12  )
    13  
    14  const name = "journald"
    15  
    16  type Journald struct {
    17  	Jmap map[string]string
    18  }
    19  
    20  func init() {
    21  	if err := logger.RegisterLogDriver(name, New); err != nil {
    22  		logrus.Fatal(err)
    23  	}
    24  }
    25  
    26  func New(ctx logger.Context) (logger.Logger, error) {
    27  	if !journal.Enabled() {
    28  		return nil, fmt.Errorf("journald is not enabled on this host")
    29  	}
    30  	// Strip a leading slash so that people can search for
    31  	// CONTAINER_NAME=foo rather than CONTAINER_NAME=/foo.
    32  	name := ctx.ContainerName
    33  	if name[0] == '/' {
    34  		name = name[1:]
    35  	}
    36  	jmap := map[string]string{
    37  		"CONTAINER_ID":      ctx.ContainerID[:12],
    38  		"CONTAINER_ID_FULL": ctx.ContainerID,
    39  		"CONTAINER_NAME":    name}
    40  	return &Journald{Jmap: jmap}, nil
    41  }
    42  
    43  func (s *Journald) Log(msg *logger.Message) error {
    44  	if msg.Source == "stderr" {
    45  		return journal.Send(string(msg.Line), journal.PriErr, s.Jmap)
    46  	}
    47  	return journal.Send(string(msg.Line), journal.PriInfo, s.Jmap)
    48  }
    49  
    50  func (s *Journald) Close() error {
    51  	return nil
    52  }
    53  
    54  func (s *Journald) Name() string {
    55  	return name
    56  }
    57  
    58  func (s *Journald) GetReader() (io.Reader, error) {
    59  	return nil, logger.ReadLogsNotSupported
    60  }