Skip to content

Commit

Permalink
Merge pull request moby#14843 from MHBauer/demonlogger-lint
Browse files Browse the repository at this point in the history
golint fixes for daemon/logger/*
  • Loading branch information
tiborvass committed Jul 29, 2015
2 parents 28b79e4 + ccbe539 commit 6a274e4
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 60 deletions.
19 changes: 13 additions & 6 deletions daemon/logger/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"time"
)

// Creator is a method that builds a logging driver instance with given context
// Creator builds a logging driver instance with given context.
type Creator func(Context) (Logger, error)

//LogOptValidator is a method that validates the log opts provided
// LogOptValidator checks the options specific to the underlying
// logging implementation.
type LogOptValidator func(cfg map[string]string) error

// Context provides enough information for a logging driver to do its function
// Context provides enough information for a logging driver to do its function.
type Context struct {
Config map[string]string
ContainerID string
Expand All @@ -27,7 +28,7 @@ type Context struct {
LogPath string
}

// Hostname returns the hostname from the underlying OS
// Hostname returns the hostname from the underlying OS.
func (ctx *Context) Hostname() (string, error) {
hostname, err := os.Hostname()
if err != nil {
Expand All @@ -36,7 +37,9 @@ func (ctx *Context) Hostname() (string, error) {
return hostname, nil
}

// Command returns the command that the container being logged was started with
// Command returns the command that the container being logged was
// started with. The Entrypoint is prepended to the container
// arguments.
func (ctx *Context) Command() string {
terms := []string{ctx.ContainerEntrypoint}
for _, arg := range ctx.ContainerArgs {
Expand Down Expand Up @@ -68,7 +71,7 @@ func (lf *logdriverFactory) registerLogOptValidator(name string, l LogOptValidat
defer lf.m.Unlock()

if _, ok := lf.optValidator[name]; ok {
return fmt.Errorf("logger: log driver named '%s' is already registered", name)
return fmt.Errorf("logger: log validator named '%s' is already registered", name)
}
lf.optValidator[name] = l
return nil
Expand Down Expand Up @@ -101,6 +104,8 @@ func RegisterLogDriver(name string, c Creator) error {
return factory.register(name, c)
}

// RegisterLogOptValidator registers the logging option validator with
// the given logging driver name.
func RegisterLogOptValidator(name string, l LogOptValidator) error {
return factory.registerLogOptValidator(name, l)
}
Expand All @@ -110,6 +115,8 @@ func GetLogDriver(name string) (Creator, error) {
return factory.get(name)
}

// ValidateLogOpts checks the options for the given log driver. The
// options supported are specific to the LogDriver implementation.
func ValidateLogOpts(name string, cfg map[string]string) error {
l := factory.getLogOptValidator(name)
if l != nil {
Expand Down
20 changes: 13 additions & 7 deletions daemon/logger/fluentd/fluentd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package fluentd provides the log driver for forwarding server logs
// to fluentd endpoints.
package fluentd

import (
Expand All @@ -14,14 +16,14 @@ import (
"github.com/fluent/fluent-logger-golang/fluent"
)

type Fluentd struct {
type fluentd struct {
tag string
containerID string
containerName string
writer *fluent.Fluent
}

type Receiver struct {
type receiver struct {
ID string
FullID string
Name string
Expand Down Expand Up @@ -67,7 +69,7 @@ func parseConfig(ctx logger.Context) (string, int, string, error) {
}

if config["fluentd-tag"] != "" {
receiver := &Receiver{
receiver := &receiver{
ID: ctx.ContainerID[:12],
FullID: ctx.ContainerID,
Name: ctx.ContainerName,
Expand All @@ -86,6 +88,9 @@ func parseConfig(ctx logger.Context) (string, int, string, error) {
return host, port, tag, nil
}

// New creates a fluentd logger using the configuration passed in on
// the context. Supported context configuration variables are
// fluentd-address & fluentd-tag.
func New(ctx logger.Context) (logger.Logger, error) {
host, port, tag, err := parseConfig(ctx)
if err != nil {
Expand All @@ -99,15 +104,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
if err != nil {
return nil, err
}
return &Fluentd{
return &fluentd{
tag: tag,
containerID: ctx.ContainerID,
containerName: ctx.ContainerName,
writer: log,
}, nil
}

func (f *Fluentd) Log(msg *logger.Message) error {
func (f *fluentd) Log(msg *logger.Message) error {
data := map[string]string{
"container_id": f.containerID,
"container_name": f.containerName,
Expand All @@ -119,6 +124,7 @@ func (f *Fluentd) Log(msg *logger.Message) error {
return f.writer.PostWithTime(f.tag, msg.Timestamp, data)
}

// ValidateLogOpt looks for fluentd specific log options fluentd-address & fluentd-tag.
func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
Expand All @@ -131,10 +137,10 @@ func ValidateLogOpt(cfg map[string]string) error {
return nil
}

func (f *Fluentd) Close() error {
func (f *fluentd) Close() error {
return f.writer.Close()
}

func (f *Fluentd) Name() string {
func (f *fluentd) Name() string {
return name
}
35 changes: 21 additions & 14 deletions daemon/logger/gelf/gelf.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// +build linux

// Package gelf provides the log driver for forwarding server logs to
// endpoints that support the Graylog Extended Log Format.
package gelf

import (
Expand All @@ -17,17 +19,17 @@ import (

const name = "gelf"

type GelfLogger struct {
type gelfLogger struct {
writer *gelf.Writer
ctx logger.Context
fields GelfFields
fields gelfFields
}

type GelfFields struct {
type gelfFields struct {
hostname string
containerId string
containerID string
containerName string
imageId string
imageID string
imageName string
command string
tag string
Expand All @@ -43,6 +45,9 @@ func init() {
}
}

// New creates a gelf logger using the configuration passed in on the
// context. Supported context configuration variables are
// gelf-address, & gelf-tag.
func New(ctx logger.Context) (logger.Logger, error) {
// parse gelf address
address, err := parseAddress(ctx.Config["gelf-address"])
Expand All @@ -59,11 +64,11 @@ func New(ctx logger.Context) (logger.Logger, error) {
// remove trailing slash from container name
containerName := bytes.TrimLeft([]byte(ctx.ContainerName), "/")

fields := GelfFields{
fields := gelfFields{
hostname: hostname,
containerId: ctx.ContainerID,
containerID: ctx.ContainerID,
containerName: string(containerName),
imageId: ctx.ContainerImageID,
imageID: ctx.ContainerImageID,
imageName: ctx.ContainerImageName,
command: ctx.Command(),
tag: ctx.Config["gelf-tag"],
Expand All @@ -76,14 +81,14 @@ func New(ctx logger.Context) (logger.Logger, error) {
return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
}

return &GelfLogger{
return &gelfLogger{
writer: gelfWriter,
ctx: ctx,
fields: fields,
}, nil
}

func (s *GelfLogger) Log(msg *logger.Message) error {
func (s *gelfLogger) Log(msg *logger.Message) error {
// remove trailing and leading whitespace
short := bytes.TrimSpace([]byte(msg.Line))

Expand All @@ -99,9 +104,9 @@ func (s *GelfLogger) Log(msg *logger.Message) error {
TimeUnix: float64(msg.Timestamp.UnixNano()/int64(time.Millisecond)) / 1000.0,
Level: level,
Extra: map[string]interface{}{
"_container_id": s.fields.containerId,
"_container_id": s.fields.containerID,
"_container_name": s.fields.containerName,
"_image_id": s.fields.imageId,
"_image_id": s.fields.imageID,
"_image_name": s.fields.imageName,
"_command": s.fields.command,
"_tag": s.fields.tag,
Expand All @@ -115,14 +120,16 @@ func (s *GelfLogger) Log(msg *logger.Message) error {
return nil
}

func (s *GelfLogger) Close() error {
func (s *gelfLogger) Close() error {
return s.writer.Close()
}

func (s *GelfLogger) Name() string {
func (s *gelfLogger) Name() string {
return name
}

// ValidateLogOpt looks for gelf specific log options gelf-address, &
// gelf-tag.
func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
Expand Down
15 changes: 10 additions & 5 deletions daemon/logger/journald/journald.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// +build linux

// Package journald provides the log driver for forwarding server logs
// to endpoints that receive the systemd format.
package journald

import (
Expand All @@ -12,7 +14,7 @@ import (

const name = "journald"

type Journald struct {
type journald struct {
Jmap map[string]string
}

Expand All @@ -22,6 +24,9 @@ func init() {
}
}

// New creates a journald logger using the configuration passed in on
// the context. Supported context configuration variables are
// syslog-address, syslog-facility, & syslog-tag.
func New(ctx logger.Context) (logger.Logger, error) {
if !journal.Enabled() {
return nil, fmt.Errorf("journald is not enabled on this host")
Expand All @@ -36,20 +41,20 @@ func New(ctx logger.Context) (logger.Logger, error) {
"CONTAINER_ID": ctx.ContainerID[:12],
"CONTAINER_ID_FULL": ctx.ContainerID,
"CONTAINER_NAME": name}
return &Journald{Jmap: jmap}, nil
return &journald{Jmap: jmap}, nil
}

func (s *Journald) Log(msg *logger.Message) error {
func (s *journald) Log(msg *logger.Message) error {
if msg.Source == "stderr" {
return journal.Send(string(msg.Line), journal.PriErr, s.Jmap)
}
return journal.Send(string(msg.Line), journal.PriInfo, s.Jmap)
}

func (s *Journald) Close() error {
func (s *journald) Close() error {
return nil
}

func (s *Journald) Name() string {
func (s *journald) Name() string {
return name
}
Loading

0 comments on commit 6a274e4

Please sign in to comment.