2017-09-22 14:13:58 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-09-24 14:34:50 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
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-09-23 18:20:22 +02:00
|
|
|
Outlets logger.Outlets
|
2017-09-22 14:13:58 +02:00
|
|
|
}
|
|
|
|
|
2017-09-24 02:05:41 +02:00
|
|
|
type SetNoMetadataFormatter interface {
|
|
|
|
SetNoMetadata(noMetadata bool)
|
|
|
|
}
|
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
type OutletCommon struct {
|
|
|
|
MinLevel logger.Level
|
|
|
|
Formatter EntryFormatter
|
|
|
|
}
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
func parseLogging(i interface{}) (c *LoggingConfig, err error) {
|
|
|
|
|
|
|
|
c = &LoggingConfig{}
|
2017-10-05 13:31:16 +02:00
|
|
|
c.Outlets = logger.NewOutlets()
|
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-10-05 13:31:16 +02:00
|
|
|
common := &OutletCommon{}
|
|
|
|
common.MinLevel, err = logger.ParseLevel(in.Level)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'level' field")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.Formatter, err = parseLogFormat(in.Format)
|
|
|
|
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":
|
|
|
|
o, err = parseStdoutOutlet(i, common)
|
|
|
|
case "tcp":
|
|
|
|
o, err = parseTCPOutlet(i, common)
|
|
|
|
case "syslog":
|
|
|
|
o, err = parseSyslogOutlet(i, common)
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown outlet type '%s'", in.Outlet)
|
|
|
|
}
|
|
|
|
return o, common.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-10-05 13:31:16 +02:00
|
|
|
func parseStdoutOutlet(i interface{}, common *OutletCommon) (WriterOutlet, error) {
|
|
|
|
return WriterOutlet{
|
|
|
|
common.Formatter,
|
|
|
|
os.Stdout,
|
|
|
|
}, nil
|
|
|
|
}
|
2017-09-23 11:24:36 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
func parseTCPOutlet(i interface{}, common *OutletCommon) (out *TCPOutlet, err error) {
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2017-10-05 13:31:16 +02:00
|
|
|
out = &TCPOutlet{}
|
|
|
|
out.Formatter = common.Formatter
|
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-10-05 13:31:16 +02:00
|
|
|
func parseSyslogOutlet(i interface{}, common *OutletCommon) (out *SyslogOutlet, err error) {
|
|
|
|
|
|
|
|
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{}
|
|
|
|
out.Formatter = common.Formatter
|
|
|
|
if f, ok := out.Formatter.(SetNoMetadataFormatter); ok {
|
|
|
|
f.SetNoMetadata(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|