2015-05-14 13:20:00 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-03-09 14:40:57 -04:00
|
|
|
package syslog
|
|
|
|
|
|
|
|
import (
|
2015-04-09 00:23:30 -04:00
|
|
|
"io"
|
2015-03-09 14:40:57 -04:00
|
|
|
"log/syslog"
|
2015-05-21 16:20:25 -04:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2015-03-09 14:40:57 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2015-05-21 16:20:25 -04:00
|
|
|
"strings"
|
2015-03-09 14:40:57 -04:00
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-03-09 14:40:57 -04:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-05-21 16:20:25 -04:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2015-03-09 14:40:57 -04:00
|
|
|
)
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
const name = "syslog"
|
|
|
|
|
2015-03-09 14:40:57 -04:00
|
|
|
type Syslog struct {
|
|
|
|
writer *syslog.Writer
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func init() {
|
|
|
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx logger.Context) (logger.Logger, error) {
|
|
|
|
tag := ctx.ContainerID[:12]
|
2015-05-21 16:20:25 -04:00
|
|
|
|
|
|
|
proto, address, err := parseAddress(ctx.Config["syslog-address"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log, err := syslog.Dial(
|
|
|
|
proto,
|
|
|
|
address,
|
|
|
|
syslog.LOG_DAEMON,
|
|
|
|
path.Base(os.Args[0])+"/"+tag,
|
|
|
|
)
|
2015-03-09 14:40:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-09 00:23:30 -04:00
|
|
|
|
2015-03-09 14:40:57 -04:00
|
|
|
return &Syslog{
|
|
|
|
writer: log,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Syslog) Log(msg *logger.Message) error {
|
|
|
|
if msg.Source == "stderr" {
|
2015-04-13 05:56:12 -04:00
|
|
|
return s.writer.Err(string(msg.Line))
|
2015-03-09 14:40:57 -04:00
|
|
|
}
|
2015-04-13 05:56:12 -04:00
|
|
|
return s.writer.Info(string(msg.Line))
|
2015-03-09 14:40:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Syslog) Close() error {
|
2015-03-23 15:03:24 -04:00
|
|
|
return s.writer.Close()
|
2015-03-09 14:40:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Syslog) Name() string {
|
2015-04-09 00:23:30 -04:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Syslog) GetReader() (io.Reader, error) {
|
|
|
|
return nil, logger.ReadLogsNotSupported
|
2015-03-09 14:40:57 -04:00
|
|
|
}
|
2015-05-21 16:20:25 -04:00
|
|
|
|
|
|
|
func parseAddress(address string) (string, string, error) {
|
|
|
|
if urlutil.IsTransportURL(address) {
|
|
|
|
url, err := url.Parse(address)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unix socket validation
|
|
|
|
if url.Scheme == "unix" {
|
|
|
|
if _, err := os.Stat(url.Path); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return url.Scheme, url.Path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we process tcp|udp
|
|
|
|
host := url.Host
|
|
|
|
if _, _, err := net.SplitHostPort(host); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
host = host + ":514"
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.Scheme, host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", nil
|
|
|
|
}
|