github.com/kunnos/engine@v1.13.1/daemon/logger/logentries/logentries.go (about)

     1  // Package logentries provides the log driver for forwarding server logs
     2  // to logentries endpoints.
     3  package logentries
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/bsphere/le_go"
    10  	"github.com/docker/docker/daemon/logger"
    11  )
    12  
    13  type logentries struct {
    14  	tag           string
    15  	containerID   string
    16  	containerName string
    17  	writer        *le_go.Logger
    18  	extra         map[string]string
    19  }
    20  
    21  const (
    22  	name  = "logentries"
    23  	token = "logentries-token"
    24  )
    25  
    26  func init() {
    27  	if err := logger.RegisterLogDriver(name, New); err != nil {
    28  		logrus.Fatal(err)
    29  	}
    30  	if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
    31  		logrus.Fatal(err)
    32  	}
    33  }
    34  
    35  // New creates a logentries logger using the configuration passed in on
    36  // the context. The supported context configuration variable is
    37  // logentries-token.
    38  func New(ctx logger.Context) (logger.Logger, error) {
    39  	logrus.WithField("container", ctx.ContainerID).
    40  		WithField("token", ctx.Config[token]).
    41  		Debug("logging driver logentries configured")
    42  
    43  	log, err := le_go.Connect(ctx.Config[token])
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return &logentries{
    48  		containerID:   ctx.ContainerID,
    49  		containerName: ctx.ContainerName,
    50  		writer:        log,
    51  	}, nil
    52  }
    53  
    54  func (f *logentries) Log(msg *logger.Message) error {
    55  	data := map[string]string{
    56  		"container_id":   f.containerID,
    57  		"container_name": f.containerName,
    58  		"source":         msg.Source,
    59  		"log":            string(msg.Line),
    60  	}
    61  	for k, v := range f.extra {
    62  		data[k] = v
    63  	}
    64  	f.writer.Println(f.tag, msg.Timestamp, data)
    65  	return nil
    66  }
    67  
    68  func (f *logentries) Close() error {
    69  	return f.writer.Close()
    70  }
    71  
    72  func (f *logentries) Name() string {
    73  	return name
    74  }
    75  
    76  // ValidateLogOpt looks for logentries specific log option logentries-address.
    77  func ValidateLogOpt(cfg map[string]string) error {
    78  	for key := range cfg {
    79  		switch key {
    80  		case "env":
    81  		case "labels":
    82  		case "tag":
    83  		case key:
    84  		default:
    85  			return fmt.Errorf("unknown log opt '%s' for logentries log driver", key)
    86  		}
    87  	}
    88  
    89  	if cfg[token] == "" {
    90  		return fmt.Errorf("Missing logentries token")
    91  	}
    92  
    93  	return nil
    94  }