diff --git a/daemon/config.go b/daemon/config.go index 4adc025eef..9b38fde4ed 100644 --- a/daemon/config.go +++ b/daemon/config.go @@ -83,7 +83,7 @@ func (config *Config) InstallFlags() { opts.LabelListVar(&config.Labels, []string{"-label"}, "Set key=value labels to the daemon") config.Ulimits = make(map[string]*ulimit.Ulimit) opts.UlimitMapVar(config.Ulimits, []string{"-default-ulimit"}, "Set default ulimits for containers") - flag.StringVar(&config.LogConfig.Type, []string{"-log-driver"}, "json-file", "Containers logging driver(json-file/none)") + flag.StringVar(&config.LogConfig.Type, []string{"-log-driver"}, "json-file", "Containers logging driver") } func getDefaultNetworkMtu() int { diff --git a/daemon/container.go b/daemon/container.go index 0fff3238e9..4868c57e9e 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -23,6 +23,7 @@ import ( "github.com/docker/docker/daemon/execdriver" "github.com/docker/docker/daemon/logger" "github.com/docker/docker/daemon/logger/jsonfilelog" + "github.com/docker/docker/daemon/logger/syslog" "github.com/docker/docker/engine" "github.com/docker/docker/image" "github.com/docker/docker/links" @@ -1380,6 +1381,12 @@ func (container *Container) startLogging() error { return err } l = dl + case "syslog": + dl, err := syslog.New(container.ID[:12]) + if err != nil { + return err + } + l = dl case "none": return nil default: diff --git a/daemon/logger/syslog/syslog.go b/daemon/logger/syslog/syslog.go new file mode 100644 index 0000000000..1f73d91d6e --- /dev/null +++ b/daemon/logger/syslog/syslog.go @@ -0,0 +1,54 @@ +package syslog + +import ( + "fmt" + "log/syslog" + "os" + "path" + "sync" + + "github.com/docker/docker/daemon/logger" +) + +type Syslog struct { + writer *syslog.Writer + tag string + mu sync.Mutex +} + +func New(tag string) (logger.Logger, error) { + log, err := syslog.New(syslog.LOG_USER, path.Base(os.Args[0])) + if err != nil { + return nil, err + } + return &Syslog{ + writer: log, + tag: tag, + }, nil +} + +func (s *Syslog) Log(msg *logger.Message) error { + logMessage := fmt.Sprintf("%s: %s", s.tag, string(msg.Line)) + if msg.Source == "stderr" { + if err := s.writer.Err(logMessage); err != nil { + return err + } + + } else { + if err := s.writer.Info(logMessage); err != nil { + return err + } + } + return nil +} + +func (s *Syslog) Close() error { + if s.writer != nil { + return s.writer.Close() + } + return nil +} + +func (s *Syslog) Name() string { + return "Syslog" +} diff --git a/docs/man/docker-create.1.md b/docs/man/docker-create.1.md index 62a4c60bb1..1a0da1b8f4 100644 --- a/docs/man/docker-create.1.md +++ b/docs/man/docker-create.1.md @@ -121,7 +121,7 @@ IMAGE [COMMAND] [ARG...] **--lxc-conf**=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" -**--log-driver**="|*json-file*|*none*" +**--log-driver**="|*json-file*|*syslog*|*none*" Logging driver for container. Default is defined by daemon `--log-driver` flag. **Warning**: `docker logs` command works only for `json-file` logging driver. diff --git a/docs/man/docker-run.1.md b/docs/man/docker-run.1.md index ef2c9061ba..1831237de2 100644 --- a/docs/man/docker-run.1.md +++ b/docs/man/docker-run.1.md @@ -222,7 +222,7 @@ which interface and port to use. **--lxc-conf**=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" -**--log-driver**="|*json-file*|*none*" +**--log-driver**="|*json-file*|*syslog*|*none*" Logging driver for container. Default is defined by daemon `--log-driver` flag. **Warning**: `docker logs` command works only for `json-file` logging driver. diff --git a/docs/man/docker.1.md b/docs/man/docker.1.md index 530fa95019..c9fe3eae9a 100644 --- a/docs/man/docker.1.md +++ b/docs/man/docker.1.md @@ -89,7 +89,7 @@ unix://[/path/to/socket] to use. **--label**="[]" Set key=value labels to the daemon (displayed in `docker info`) -**--log-driver**="*json-file*|*none*" +**--log-driver**="*json-file*|*syslog*|*none*" Container's logging driver. Default is `default`. **Warning**: `docker logs` command works only for `json-file` logging driver. diff --git a/docs/sources/reference/api/docker_remote_api_v1.18.md b/docs/sources/reference/api/docker_remote_api_v1.18.md index b5d9c0e5f4..2197066d16 100644 --- a/docs/sources/reference/api/docker_remote_api_v1.18.md +++ b/docs/sources/reference/api/docker_remote_api_v1.18.md @@ -259,7 +259,7 @@ Json Parameters: `Ulimits: { "Name": "nofile", "Soft": 1024, "Hard", 2048 }}` - **LogConfig** - Logging configuration to container, format `{ "Type": "", "Config": {"key1": "val1"}} - Available types: `json-file`, `none`. + Available types: `json-file`, `syslog`, `none`. `json-file` logging driver. - **CgroupParent** - Path to cgroups under which the cgroup for the container will be created. If the path is not absolute, the path is considered to be relative to the cgroups path of the init process. Cgroups will be created if they do not already exist. diff --git a/docs/sources/reference/run.md b/docs/sources/reference/run.md index 3c9c6a68d6..5a466461e6 100644 --- a/docs/sources/reference/run.md +++ b/docs/sources/reference/run.md @@ -657,6 +657,11 @@ this driver. Default logging driver for Docker. Writes JSON messages to file. `docker logs` command is available only for this logging driver +## Logging driver: syslog + +Syslog logging driver for Docker. Writes log messages to syslog. `docker logs` +command is not available for this logging driver + ## Overriding Dockerfile image defaults When a developer builds an image from a [*Dockerfile*](/reference/builder)