github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/logger/journald/journald.go (about)

     1  // +build linux
     2  
     3  // Package journald provides the log driver for forwarding server logs
     4  // to endpoints that receive the systemd format.
     5  package journald
     6  
     7  import (
     8  	"fmt"
     9  	"sync"
    10  	"unicode"
    11  
    12  	"github.com/Sirupsen/logrus"
    13  	"github.com/coreos/go-systemd/journal"
    14  	"github.com/docker/docker/daemon/logger"
    15  	"github.com/docker/docker/daemon/logger/loggerutils"
    16  )
    17  
    18  const name = "journald"
    19  
    20  type journald struct {
    21  	vars    map[string]string // additional variables and values to send to the journal along with the log message
    22  	readers readerList
    23  }
    24  
    25  type readerList struct {
    26  	mu      sync.Mutex
    27  	readers map[*logger.LogWatcher]*logger.LogWatcher
    28  }
    29  
    30  func init() {
    31  	if err := logger.RegisterLogDriver(name, New); err != nil {
    32  		logrus.Fatal(err)
    33  	}
    34  	if err := logger.RegisterLogOptValidator(name, validateLogOpt); err != nil {
    35  		logrus.Fatal(err)
    36  	}
    37  }
    38  
    39  // sanitizeKeyMode returns the sanitized string so that it could be used in journald.
    40  // In journald log, there are special requirements for fields.
    41  // Fields must be composed of uppercase letters, numbers, and underscores, but must
    42  // not start with an underscore.
    43  func sanitizeKeyMod(s string) string {
    44  	n := ""
    45  	for _, v := range s {
    46  		if 'a' <= v && v <= 'z' {
    47  			v = unicode.ToUpper(v)
    48  		} else if ('Z' < v || v < 'A') && ('9' < v || v < '0') {
    49  			v = '_'
    50  		}
    51  		// If (n == "" && v == '_'), then we will skip as this is the beginning with '_'
    52  		if !(n == "" && v == '_') {
    53  			n += string(v)
    54  		}
    55  	}
    56  	return n
    57  }
    58  
    59  // New creates a journald logger using the configuration passed in on
    60  // the context.
    61  func New(info logger.Info) (logger.Logger, error) {
    62  	if !journal.Enabled() {
    63  		return nil, fmt.Errorf("journald is not enabled on this host")
    64  	}
    65  
    66  	// parse log tag
    67  	tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	vars := map[string]string{
    73  		"CONTAINER_ID":      info.ContainerID[:12],
    74  		"CONTAINER_ID_FULL": info.ContainerID,
    75  		"CONTAINER_NAME":    info.Name(),
    76  		"CONTAINER_TAG":     tag,
    77  	}
    78  	extraAttrs := info.ExtraAttributes(sanitizeKeyMod)
    79  	for k, v := range extraAttrs {
    80  		vars[k] = v
    81  	}
    82  	return &journald{vars: vars, readers: readerList{readers: make(map[*logger.LogWatcher]*logger.LogWatcher)}}, nil
    83  }
    84  
    85  // We don't actually accept any options, but we have to supply a callback for
    86  // the factory to pass the (probably empty) configuration map to.
    87  func validateLogOpt(cfg map[string]string) error {
    88  	for key := range cfg {
    89  		switch key {
    90  		case "labels":
    91  		case "env":
    92  		case "tag":
    93  		default:
    94  			return fmt.Errorf("unknown log opt '%s' for journald log driver", key)
    95  		}
    96  	}
    97  	return nil
    98  }
    99  
   100  func (s *journald) Log(msg *logger.Message) error {
   101  	vars := map[string]string{}
   102  	for k, v := range s.vars {
   103  		vars[k] = v
   104  	}
   105  	if msg.Partial {
   106  		vars["CONTAINER_PARTIAL_MESSAGE"] = "true"
   107  	}
   108  	if msg.Source == "stderr" {
   109  		return journal.Send(string(msg.Line), journal.PriErr, vars)
   110  	}
   111  	return journal.Send(string(msg.Line), journal.PriInfo, vars)
   112  }
   113  
   114  func (s *journald) Name() string {
   115  	return name
   116  }