// New creates a gelf logger using the configuration passed in on the // context. The supported context configuration variable is gelf-address. func New(info logger.Info) (logger.Logger, error) { // parse gelf address address, err := parseAddress(info.Config["gelf-address"]) if err != nil { return nil, err } // collect extra data for GELF message hostname, err := info.Hostname() if err != nil { return nil, fmt.Errorf("gelf: cannot access hostname to set source field") } // parse log tag tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate) if err != nil { return nil, err } extra := map[string]interface{}{ "_container_id": info.ContainerID, "_container_name": info.Name(), "_image_id": info.ContainerImageID, "_image_name": info.ContainerImageName, "_command": info.Command(), "_tag": tag, "_created": info.ContainerCreated, } extraAttrs := info.ExtraAttributes(func(key string) string { if key[0] == '_' { return key } return "_" + key }) for k, v := range extraAttrs { extra[k] = v } rawExtra, err := json.Marshal(extra) if err != nil { return nil, err } // create new gelfWriter gelfWriter, err := gelf.NewWriter(address) if err != nil { return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err) } if v, ok := info.Config["gelf-compression-type"]; ok { switch v { case "gzip": gelfWriter.CompressionType = gelf.CompressGzip case "zlib": gelfWriter.CompressionType = gelf.CompressZlib case "none": gelfWriter.CompressionType = gelf.CompressNone default: return nil, fmt.Errorf("gelf: invalid compression type %q", v) } } if v, ok := info.Config["gelf-compression-level"]; ok { val, err := strconv.Atoi(v) if err != nil { return nil, fmt.Errorf("gelf: invalid compression level %s, err %v", v, err) } gelfWriter.CompressionLevel = val } return &gelfLogger{ writer: gelfWriter, info: info, hostname: hostname, rawExtra: rawExtra, }, nil }
// New creates a new logger that logs to Google Cloud Logging using the application // default credentials. // // See https://developers.google.com/identity/protocols/application-default-credentials func New(info logger.Info) (logger.Logger, error) { initGCP() var project string if projectID != "" { project = projectID } if projectID, found := info.Config[projectOptKey]; found { project = projectID } if project == "" { return nil, fmt.Errorf("No project was specified and couldn't read project from the meatadata server. Please specify a project") } // Issue #29344: gcplogs segfaults (static binary) // If HOME is not set, logging.NewClient() will call os/user.Current() via oauth2/google. // However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed // in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341) // So we forcibly set HOME so as to avoid call to os/user/Current() if err := ensureHomeIfIAmStatic(); err != nil { return nil, err } c, err := logging.NewClient(context.Background(), project) if err != nil { return nil, err } lg := c.Logger("gcplogs-docker-driver") if err := c.Ping(context.Background()); err != nil { return nil, fmt.Errorf("unable to connect or authenticate with Google Cloud Logging: %v", err) } l := &gcplogs{ logger: lg, container: &containerInfo{ Name: info.ContainerName, ID: info.ContainerID, ImageName: info.ContainerImageName, ImageID: info.ContainerImageID, Created: info.ContainerCreated, Metadata: info.ExtraAttributes(nil), }, } if info.Config[logCmdKey] == "true" { l.container.Command = info.Command() } if onGCE { l.instance = &instanceInfo{ Zone: zone, Name: instanceName, ID: instanceID, } } else if info.Config[logZoneKey] != "" || info.Config[logNameKey] != "" || info.Config[logIDKey] != "" { l.instance = &instanceInfo{ Zone: info.Config[logZoneKey], Name: info.Config[logNameKey], ID: info.Config[logIDKey], } } // The logger "overflows" at a rate of 10,000 logs per second and this // overflow func is called. We want to surface the error to the user // without overly spamming /var/log/docker.log so we log the first time // we overflow and every 1000th time after. c.OnError = func(err error) { if err == logging.ErrOverflow { if i := atomic.AddUint64(&droppedLogs, 1); i%1000 == 1 { logrus.Errorf("gcplogs driver has dropped %v logs", i) } } else { logrus.Error(err) } } return l, nil }