2017-09-22 14:13:58 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-08-25 21:30:25 +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/pkg/errors"
|
2018-08-26 16:44:34 +02:00
|
|
|
"github.com/zrepl/zrepl/cmd/config"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/zrepl/zrepl/cmd/tlsconf"
|
2017-09-23 18:20:22 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-09-23 11:24:36 +02:00
|
|
|
"os"
|
2017-09-22 14:13:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LoggingConfig struct {
|
2017-12-23 00:55:02 +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
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
func parseLogging(in []config.LoggingOutletEnum) (c *LoggingConfig, err error) {
|
2017-09-22 14:13:58 +02:00
|
|
|
|
|
|
|
c = &LoggingConfig{}
|
2017-11-17 00:24:22 +01:00
|
|
|
c.Outlets = logger.NewOutlets()
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
if len(in) == 0 {
|
2017-10-05 13:31:16 +02:00
|
|
|
// 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
|
2018-08-26 16:44:34 +02:00
|
|
|
for lei, le := range in {
|
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
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
func parseOutlet(in config.LoggingOutletEnum) (o logger.Outlet, level logger.Level, err error) {
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
parseCommon := func(common config.LoggingOutletCommon) (logger.Level, EntryFormatter, error) {
|
|
|
|
if common.Level == "" || common.Format == "" {
|
|
|
|
return 0, nil, errors.Errorf("must specify 'level' and 'format' field")
|
|
|
|
}
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
minLevel, err := logger.ParseLevel(common.Level)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, errors.Wrap(err, "cannot parse 'level' field")
|
|
|
|
}
|
|
|
|
formatter, err := parseLogFormat(common.Format)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, errors.Wrap(err, "cannot parse 'formatter' field")
|
|
|
|
}
|
|
|
|
return minLevel, formatter, nil
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
var f EntryFormatter
|
|
|
|
|
|
|
|
switch v := in.Ret.(type) {
|
|
|
|
case config.StdoutLoggingOutlet:
|
|
|
|
level, f, err = parseCommon(v.LoggingOutletCommon)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
o, err = parseStdoutOutlet(v, f)
|
|
|
|
case config.TCPLoggingOutlet:
|
|
|
|
level, f, err = parseCommon(v.LoggingOutletCommon)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
o, err = parseTCPOutlet(v, f)
|
|
|
|
case config.SyslogLoggingOutlet:
|
|
|
|
level, f, err = parseCommon(v.LoggingOutletCommon)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
o, err = parseSyslogOutlet(v, f)
|
2017-10-05 13:31:16 +02:00
|
|
|
default:
|
2018-08-26 16:44:34 +02:00
|
|
|
panic(v)
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2018-08-26 16:44:34 +02:00
|
|
|
return o, level, err
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-24 14:34:50 +02:00
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
func parseStdoutOutlet(in config.StdoutLoggingOutlet, formatter EntryFormatter) (WriterOutlet, error) {
|
2017-11-14 21:51:19 +01:00
|
|
|
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
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
func parseTCPOutlet(in config.TCPLoggingOutlet, formatter EntryFormatter) (out *TCPOutlet, err error) {
|
2017-12-29 17:19:07 +01:00
|
|
|
var tlsConfig *tls.Config
|
2017-10-05 13:31:16 +02:00
|
|
|
if in.TLS != nil {
|
2018-08-26 16:44:34 +02:00
|
|
|
tlsConfig, err = func(m *config.TCPLoggingOutletTLS, host string) (*tls.Config, error) {
|
|
|
|
clientCert, err := tls.LoadX509KeyPair(m.Cert, m.Key)
|
2017-09-24 02:05:41 +02:00
|
|
|
if err != nil {
|
2018-08-25 12:58:17 +02:00
|
|
|
return nil, errors.Wrap(err, "cannot load client cert")
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
2018-08-25 12:58:17 +02:00
|
|
|
|
|
|
|
var rootCAs *x509.CertPool
|
2018-08-26 16:44:34 +02:00
|
|
|
if m.CA == "" {
|
2018-08-25 12:58:17 +02:00
|
|
|
if rootCAs, err = x509.SystemCertPool(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot open system cert pool")
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-26 16:44:34 +02:00
|
|
|
rootCAs, err = tlsconf.ParseCAFile(m.CA)
|
2018-08-25 12:58:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot parse CA cert")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if rootCAs == nil {
|
|
|
|
panic("invariant violated")
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-24 02:05:41 +02:00
|
|
|
|
2018-08-25 12:58:17 +02:00
|
|
|
return tlsconf.ClientAuthClient(host, rootCAs, clientCert)
|
|
|
|
}(in.TLS, in.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("cannot not parse TLS config in field 'tls'")
|
2017-10-05 13:31:16 +02:00
|
|
|
}
|
2017-09-24 02:05:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-29 17:19:07 +01:00
|
|
|
formatter.SetMetadataFlags(MetadataAll)
|
2018-08-26 16:44:34 +02:00
|
|
|
return NewTCPOutlet(formatter, in.Net, in.Address, tlsConfig, in.RetryInterval), nil
|
2017-09-23 11:24:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
func parseSyslogOutlet(in config.SyslogLoggingOutlet, formatter EntryFormatter) (out *SyslogOutlet, err error) {
|
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)
|
2018-08-26 16:44:34 +02:00
|
|
|
out.RetryInterval = in.RetryInterval
|
|
|
|
return out, nil
|
2017-09-22 14:13:58 +02:00
|
|
|
}
|