zrepl/cmd/config_logging.go

249 lines
5.4 KiB
Go
Raw Normal View History

2017-09-22 14:13:58 +02:00
package cmd
import (
2017-09-24 14:34:50 +02:00
"crypto/tls"
"crypto/x509"
"github.com/mattn/go-isatty"
2017-09-22 14:13:58 +02:00
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/zrepl/zrepl/logger"
2017-09-24 14:34:50 +02:00
"io/ioutil"
"os"
2017-09-23 12:58:13 +02:00
"time"
2017-09-22 14:13:58 +02:00
)
type LoggingConfig struct {
Outlets logger.Outlets
2017-09-22 14:13:58 +02:00
}
type MetadataFlags int64
2017-09-24 02:05:41 +02:00
const (
MetadataTime MetadataFlags = 1 << iota
MetadataLevel
MetadataNone MetadataFlags = 0
MetadataAll MetadataFlags = ^0
)
2017-09-22 14:13:58 +02:00
func parseLogging(i interface{}) (c *LoggingConfig, err error) {
c = &LoggingConfig{}
c.Outlets = logger.NewOutlets()
2017-09-22 14:13:58 +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")
}
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
var syslogOutlets, stdoutOutlets int
for lei, le := range asList {
outlet, minLevel, err := parseOutlet(le)
2017-09-22 14:13:58 +02:00
if err != nil {
return nil, errors.Wrapf(err, "cannot parse outlet #%d", lei)
2017-09-22 14:13:58 +02:00
}
var _ logger.Outlet = WriterOutlet{}
var _ logger.Outlet = &SyslogOutlet{}
switch outlet.(type) {
case *SyslogOutlet:
syslogOutlets++
case WriterOutlet:
stdoutOutlets++
}
c.Outlets.Add(outlet, minLevel)
}
2017-09-22 14:13:58 +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")
}
return c, nil
}
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
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-09-22 14:13:58 +02:00
func parseOutlet(i interface{}) (o logger.Outlet, level logger.Level, err error) {
2017-09-24 14:34:50 +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
minLevel, err := logger.ParseLevel(in.Level)
if err != nil {
err = errors.Wrap(err, "cannot parse 'level' field")
return
}
formatter, err := parseLogFormat(in.Format)
if err != nil {
err = errors.Wrap(err, "cannot parse")
return
}
2017-09-24 14:34:50 +02:00
switch in.Outlet {
case "stdout":
o, err = parseStdoutOutlet(i, formatter)
case "tcp":
o, err = parseTCPOutlet(i, formatter)
case "syslog":
o, err = parseSyslogOutlet(i, formatter)
default:
err = errors.Errorf("unknown outlet type '%s'", in.Outlet)
}
return o, minLevel, err
}
2017-09-24 14:34:50 +02:00
func parseStdoutOutlet(i interface{}, formatter EntryFormatter) (WriterOutlet, error) {
var in struct {
Time bool
}
if err := mapstructure.Decode(i, &in); err != nil {
return WriterOutlet{}, errors.Wrap(err, "invalid structure for stdout outlet")
}
flags := MetadataAll
writer := os.Stdout
if !isatty.IsTerminal(writer.Fd()) && !in.Time {
flags &= ^MetadataTime
}
formatter.SetMetadataFlags(flags)
return WriterOutlet{
formatter,
os.Stdout,
}, nil
}
func parseTCPOutlet(i interface{}, formatter EntryFormatter) (out *TCPOutlet, err error) {
2017-09-24 02:05:41 +02:00
out = &TCPOutlet{}
out.Formatter = formatter
out.Formatter.SetMetadataFlags(MetadataAll)
2017-09-24 02:05:41 +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
}
}
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
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
}
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 {
return nil, errors.Wrap(err, "cannot load CA cert")
2017-09-24 02:05:41 +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
}
out.TLS = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: rootCAs,
}
2017-09-24 02:05:41 +02:00
out.TLS.BuildNameToCertificate()
2017-09-24 02:05:41 +02:00
}
return
}
func parseSyslogOutlet(i interface{}, formatter EntryFormatter) (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")
}
out = &SyslogOutlet{}
out.Formatter = formatter
out.Formatter.SetMetadataFlags(MetadataNone)
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-22 14:13:58 +02:00
return
2017-09-22 14:13:58 +02:00
}