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