<commit_before>package cmd

import (
	"crypto/tls"
	"crypto/x509"
	"github.com/mitchellh/mapstructure"
	"github.com/pkg/errors"
	"github.com/zrepl/zrepl/logger"
	"io/ioutil"
	"os"
	"time"
)

type LoggingConfig struct {
	Outlets logger.Outlets
}

type SetNoMetadataFormatter interface {
	SetNoMetadata(noMetadata bool)
}

func parseLogging(i interface{}) (c *LoggingConfig, err error) {

	c = &LoggingConfig{}
	if i == nil {
		return c, nil
	}

	var asMap struct {
		Stdout struct {
			Level  string
			Format string
		}
		TCP struct {
			Level         string
			Format        string
			Net           string
			Address       string
			RetryInterval string `mapstructure:"retry_interval"`
			TLS           *struct {
				CA   string
				Cert string
				Key  string
			}
		}
		Syslog struct {
			Enable        bool
			Format        string
			RetryInterval string `mapstructure:"retry_interval"`
		}
	}
	if err = mapstructure.Decode(i, &asMap); err != nil {
		return nil, errors.Wrap(err, "mapstructure error")
	}

	c.Outlets = logger.NewOutlets()

	if asMap.Stdout.Level != "" {

		out := WriterOutlet{
			&HumanFormatter{},
			os.Stdout,
		}

		level, err := logger.ParseLevel(asMap.Stdout.Level)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'level'")
		}

		if asMap.Stdout.Format != "" {
			out.Formatter, err = parseLogFormat(asMap.Stdout.Format)
			if err != nil {
				return nil, errors.Wrap(err, "cannot parse 'format'")
			}
		}

		c.Outlets.Add(out, level)

	}

	if asMap.TCP.Address != "" {

		out := &TCPOutlet{}

		out.Formatter, err = parseLogFormat(asMap.TCP.Format)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'format'")
		}

		lvl, err := logger.ParseLevel(asMap.TCP.Level)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'level'")
		}

		out.RetryInterval, err = time.ParseDuration(asMap.TCP.RetryInterval)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'retry_interval'")
		}

		out.Net, out.Address = asMap.TCP.Net, asMap.TCP.Address

		if asMap.TCP.TLS != nil {

			cert, err := tls.LoadX509KeyPair(asMap.TCP.TLS.Cert, asMap.TCP.TLS.Key)
			if err != nil {
				return nil, errors.Wrap(err, "cannot load client cert")
			}

			var rootCAs *x509.CertPool
			if asMap.TCP.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(asMap.TCP.TLS.CA)
				if err != nil {
					return nil, errors.Wrap(err, "cannot load CA cert")
				}
				if !rootCAs.AppendCertsFromPEM(rootCAPEM) {
					return nil, errors.New("cannot parse CA cert")
				}
			}
			if err != nil && asMap.TCP.TLS.CA == "" {
				return nil, errors.Wrap(err, "cannot load root ca pool")
			}

			out.TLS = &tls.Config{
				Certificates: []tls.Certificate{cert},
				RootCAs:      rootCAs,
			}
		}

		c.Outlets.Add(out, lvl)
	}

	if asMap.Syslog.Enable {
<commit_msg>cmd: config: build identity map<commit_after>package cmd

import (
	"crypto/tls"
	"crypto/x509"
	"github.com/mitchellh/mapstructure"
	"github.com/pkg/errors"
	"github.com/zrepl/zrepl/logger"
	"io/ioutil"
	"os"
	"time"
)

type LoggingConfig struct {
	Outlets logger.Outlets
}

type SetNoMetadataFormatter interface {
	SetNoMetadata(noMetadata bool)
}

func parseLogging(i interface{}) (c *LoggingConfig, err error) {

	c = &LoggingConfig{}
	if i == nil {
		return c, nil
	}

	var asMap struct {
		Stdout struct {
			Level  string
			Format string
		}
		TCP struct {
			Level         string
			Format        string
			Net           string
			Address       string
			RetryInterval string `mapstructure:"retry_interval"`
			TLS           *struct {
				CA   string
				Cert string
				Key  string
			}
		}
		Syslog struct {
			Enable        bool
			Format        string
			RetryInterval string `mapstructure:"retry_interval"`
		}
	}
	if err = mapstructure.Decode(i, &asMap); err != nil {
		return nil, errors.Wrap(err, "mapstructure error")
	}

	c.Outlets = logger.NewOutlets()

	if asMap.Stdout.Level != "" {

		out := WriterOutlet{
			&HumanFormatter{},
			os.Stdout,
		}

		level, err := logger.ParseLevel(asMap.Stdout.Level)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'level'")
		}

		if asMap.Stdout.Format != "" {
			out.Formatter, err = parseLogFormat(asMap.Stdout.Format)
			if err != nil {
				return nil, errors.Wrap(err, "cannot parse 'format'")
			}
		}

		c.Outlets.Add(out, level)

	}

	if asMap.TCP.Address != "" {

		out := &TCPOutlet{}

		out.Formatter, err = parseLogFormat(asMap.TCP.Format)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'format'")
		}

		lvl, err := logger.ParseLevel(asMap.TCP.Level)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'level'")
		}

		out.RetryInterval, err = time.ParseDuration(asMap.TCP.RetryInterval)
		if err != nil {
			return nil, errors.Wrap(err, "cannot parse 'retry_interval'")
		}

		out.Net, out.Address = asMap.TCP.Net, asMap.TCP.Address

		if asMap.TCP.TLS != nil {

			cert, err := tls.LoadX509KeyPair(asMap.TCP.TLS.Cert, asMap.TCP.TLS.Key)
			if err != nil {
				return nil, errors.Wrap(err, "cannot load client cert")
			}

			var rootCAs *x509.CertPool
			if asMap.TCP.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(asMap.TCP.TLS.CA)
				if err != nil {
					return nil, errors.Wrap(err, "cannot load CA cert")
				}
				if !rootCAs.AppendCertsFromPEM(rootCAPEM) {
					return nil, errors.New("cannot parse CA cert")
				}
			}
			if err != nil && asMap.TCP.TLS.CA == "" {
				return nil, errors.Wrap(err, "cannot load root ca pool")
			}

			out.TLS = &tls.Config{
				Certificates: []tls.Certificate{cert},
				RootCAs:      rootCAs,
			}

			out.TLS.BuildNameToCertificate()
		}

		c.Outlets.Add(out, lvl)
	}

	if asMap.Syslog.Enable {

{'fraction_non_alphanumeric': 0.13320046503903007, 'fraction_numerical': 0.004982561036372696, 'mean_word_length': 2.835668789808917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '770898', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2159, 'n_words': 519}