2017-09-22 14:13:58 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-09-24 14:34:50 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-11-14 21:51:19 +01:00
|
|
|
"github.com/mattn/go-isatty"
|
2017-09-22 14:13:58 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/pkg/errors"
|
2017-09-23 18:20:22 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-09-24 14:34:50 +02:00
|
|
|
"io/ioutil"
|
2017-09-23 11:24:36 +02:00
|
|
|
"os"
|
2017-09-23 12:58:13 +02:00
|
|
|
"time"
|
2017-09-22 14:13:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LoggingConfig struct {
|
2017-11-16 23:49:47 +01:00
|
|
|
Outlets *logger.Outlets
|
2017-09-22 14:13:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 21:51:19 +01:00
|
|
|
type MetadataFlags int64
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2017-11-14 21:51:19 +01:00
|
|
|
const (
|
|
|
|
MetadataTime MetadataFlags = 1 << iota
|
|
|
|
MetadataLevel
|
|
|
|
|
|
|
|
MetadataNone MetadataFlags = 0
|
|
|
|
MetadataAll MetadataFlags = ^0
|
|
|
|
)
|
2017-10-05 13:31:16 +02:00
|
|
|
|
2017-11-16 23:49:47 +01:00
|
|
|
func LoggerErrorOutlet() logger.Outlet {
|
|
|
|
|
|
|
|
formatter := &HumanFormatter{}
|
|
|
|
writer := os.Stderr
|
|
|
|
formatter.SetMetadataFlags(MetadataAll)
|
|
|
|
return WriterOutlet{
|
|
|
|
formatter, writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
func parseLogging(i interface{}) (c *LoggingConfig, err error) {
|
|
|
|
|
|
|
|
c = &LoggingConfig{}
|
2017-11-16 23:49:47 +01:00
|
|
|
|
|
|
|
c.Outlets = logger.NewOutlets(LoggerErrorOutlet())
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
var asList []interface{}
|
|
|
|
if err = mapstructure.Decode(i, &asList); err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
return nil, errors.Wrap(err, "mapstructure error")
|
|
|
|
}
|
2017-10-05 13:31:16 +02:00
|
|
|
if len(asList) == 0 {
|
|
|
|
// Default config
|
|
|
|
out := WriterOutlet{&HumanFormatter{}, os.Stdout}
|
|
|
|
c.Outlets.Add(out, logger.Warn)
|
|
|
|
return
|
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
var syslogOutlets, stdoutOutlets int
|
|
|
|
for lei, le := range asList {
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
outlet, minLevel, err := parseOutlet(le)
|
2017-09-22 14:13:58 +02:00
|
|
|
if err != nil {
|
2017-10-05 13:31:16 +02:00
|
|
|
return nil, errors.Wrapf(err, "cannot parse outlet #%d", lei)
|
2017-09-22 14:13:58 +02:00
|
|
|
}
|
2017-10-05 13:31:16 +02:00
|
|
|
var _ logger.Outlet = WriterOutlet{}
|
|
|
|
var _ logger.Outlet = &SyslogOutlet{}
|
|
|
|
switch outlet.(type) {
|
|
|
|
case *SyslogOutlet:
|
|
|
|
syslogOutlets++
|
|
|
|
case WriterOutlet:
|
|
|
|
stdoutOutlets++
|
2017-09-23 11:24:36 +02:00
|
|
|
}
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
c.Outlets.Add(outlet, minLevel)
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-09-23 11:24:36 +02:00
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
if syslogOutlets > 1 {
|
|
|
|
return nil, errors.Errorf("can only define one 'syslog' outlet")
|
|
|
|
}
|
|
|
|
if stdoutOutlets > 1 {
|
|
|
|
return nil, errors.Errorf("can only define one 'stdout' outlet")
|
|
|
|
}
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
return c, nil
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
func parseLogFormat(i interface{}) (f EntryFormatter, err error) {
|
|
|
|
var is string
|
|
|
|
switch j := i.(type) {
|
|
|
|
case string:
|
|
|
|
is = j
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid log format: wrong type: %T", i)
|
|
|
|
}
|
2017-09-23 12:58:13 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
switch is {
|
|
|
|
case "human":
|
|
|
|
return &HumanFormatter{}, nil
|
|
|
|
case "logfmt":
|
|
|
|
return &LogfmtFormatter{}, nil
|
|
|
|
case "json":
|
|
|
|
return &JSONFormatter{}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid log format: '%s'", is)
|
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
func parseOutlet(i interface{}) (o logger.Outlet, level logger.Level, err error) {
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
var in struct {
|
|
|
|
Outlet string
|
|
|
|
Level string
|
|
|
|
Format string
|
|
|
|
}
|
|
|
|
if err = mapstructure.Decode(i, &in); err != nil {
|
|
|
|
err = errors.Wrap(err, "mapstructure error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if in.Outlet == "" || in.Level == "" || in.Format == "" {
|
|
|
|
err = errors.Errorf("must specify 'outlet', 'level' and 'format' field")
|
|
|
|
return
|
|
|
|
}
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2017-11-14 21:51:19 +01:00
|
|
|
minLevel, err := logger.ParseLevel(in.Level)
|
2017-10-05 13:31:16 +02:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'level' field")
|
|
|
|
return
|
|
|
|
}
|
2017-11-14 21:51:19 +01:00
|
|
|
formatter, err := parseLogFormat(in.Format)
|
2017-10-05 13:31:16 +02:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse")
|
|
|
|
return
|
|
|
|
}
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
switch in.Outlet {
|
|
|
|
case "stdout":
|
2017-11-14 21:51:19 +01:00
|
|
|
o, err = parseStdoutOutlet(i, formatter)
|
2017-10-05 13:31:16 +02:00
|
|
|
case "tcp":
|
2017-11-14 21:51:19 +01:00
|
|
|
o, err = parseTCPOutlet(i, formatter)
|
2017-10-05 13:31:16 +02:00
|
|
|
case "syslog":
|
2017-11-14 21:51:19 +01:00
|
|
|
o, err = parseSyslogOutlet(i, formatter)
|
2017-10-05 13:31:16 +02:00
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown outlet type '%s'", in.Outlet)
|
|
|
|
}
|
2017-11-14 21:51:19 +01:00
|
|
|
return o, minLevel, err
|
2017-09-24 16:25:41 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2017-11-14 21:51:19 +01:00
|
|
|
func parseStdoutOutlet(i interface{}, formatter EntryFormatter) (WriterOutlet, error) {
|
|
|
|
|
|
|
|
var in struct {
|
2017-11-15 11:14:20 +01:00
|
|
|
Time bool
|
2017-11-14 21:51:19 +01:00
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(i, &in); err != nil {
|
|
|
|
return WriterOutlet{}, errors.Wrap(err, "invalid structure for stdout outlet")
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := MetadataAll
|
|
|
|
writer := os.Stdout
|
2017-11-15 11:14:20 +01:00
|
|
|
if !isatty.IsTerminal(writer.Fd()) && !in.Time {
|
2017-11-14 21:51:19 +01:00
|
|
|
flags &= ^MetadataTime
|
|
|
|
}
|
|
|
|
|
|
|
|
formatter.SetMetadataFlags(flags)
|
2017-10-05 13:31:16 +02:00
|
|
|
return WriterOutlet{
|
2017-11-14 21:51:19 +01:00
|
|
|
formatter,
|
2017-10-05 13:31:16 +02:00
|
|
|
os.Stdout,
|
|
|
|
}, nil
|
|
|
|
}
|
2017-09-23 11:24:36 +02:00
|
|
|
|
2017-11-14 21:51:19 +01:00
|
|
|
func parseTCPOutlet(i interface{}, formatter EntryFormatter) (out *TCPOutlet, err error) {
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
out = &TCPOutlet{}
|
2017-11-14 21:51:19 +01:00
|
|
|
out.Formatter = formatter
|
|
|
|
out.Formatter.SetMetadataFlags(MetadataAll)
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
var in struct {
|
|
|
|
Net string
|
|
|
|
Address string
|
|
|
|
RetryInterval string `mapstructure:"retry_interval"`
|
|
|
|
TLS *struct {
|
|
|
|
CA string
|
|
|
|
Cert string
|
|
|
|
Key string
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
|
|
|
if err = mapstructure.Decode(i, &in); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "mapstructure error")
|
|
|
|
}
|
|
|
|
|
|
|
|
out.RetryInterval, err = time.ParseDuration(in.RetryInterval)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot parse 'retry_interval'")
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Net, out.Address = in.Net, in.Address
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
if in.TLS != nil {
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(in.TLS.Cert, in.TLS.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot load client cert")
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
var rootCAs *x509.CertPool
|
|
|
|
if in.TLS.CA == "" {
|
|
|
|
if rootCAs, err = x509.SystemCertPool(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot open system cert pool")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rootCAs = x509.NewCertPool()
|
|
|
|
rootCAPEM, err := ioutil.ReadFile(in.TLS.CA)
|
2017-09-24 02:05:41 +02:00
|
|
|
if err != nil {
|
2017-10-05 13:31:16 +02:00
|
|
|
return nil, errors.Wrap(err, "cannot load CA cert")
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
2017-10-05 13:31:16 +02:00
|
|
|
if !rootCAs.AppendCertsFromPEM(rootCAPEM) {
|
|
|
|
return nil, errors.New("cannot parse CA cert")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil && in.TLS.CA == "" {
|
|
|
|
return nil, errors.Wrap(err, "cannot load root ca pool")
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
out.TLS = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
}
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
out.TLS.BuildNameToCertificate()
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
return
|
2017-09-23 11:24:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:51:19 +01:00
|
|
|
func parseSyslogOutlet(i interface{}, formatter EntryFormatter) (out *SyslogOutlet, err error) {
|
2017-10-05 13:31:16 +02:00
|
|
|
|
|
|
|
var in struct {
|
|
|
|
RetryInterval string `mapstructure:"retry_interval"`
|
|
|
|
}
|
|
|
|
if err = mapstructure.Decode(i, &in); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "mapstructure error")
|
2017-09-23 11:24:36 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
out = &SyslogOutlet{}
|
2017-11-14 21:51:19 +01:00
|
|
|
out.Formatter = formatter
|
|
|
|
out.Formatter.SetMetadataFlags(MetadataNone)
|
2017-10-05 13:31:16 +02:00
|
|
|
|
|
|
|
out.RetryInterval = 0 // default to 0 as we assume local syslog will just work
|
|
|
|
if in.RetryInterval != "" {
|
|
|
|
out.RetryInterval, err = time.ParseDuration(in.RetryInterval)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot parse 'retry_interval'")
|
|
|
|
}
|
2017-09-23 11:24:36 +02:00
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
return
|
2017-09-22 14:13:58 +02:00
|
|
|
}
|