diff --git a/internal/config/config.go b/internal/config/config.go index 744f4c31..b3c3e101 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "github.com/creasty/defaults" "github.com/goccy/go-yaml" + "github.com/icinga/icingadb/pkg/logging" "github.com/jessevdk/go-flags" "github.com/pkg/errors" "os" @@ -12,10 +13,10 @@ import ( // Config defines Icinga DB config. type Config struct { - Database Database `yaml:"database"` - Redis Redis `yaml:"redis"` - Logging Logging `yaml:"logging"` - Retention Retention `yaml:"retention"` + Database Database `yaml:"database"` + Redis Redis `yaml:"redis"` + Logging logging.Config `yaml:"logging"` + Retention Retention `yaml:"retention"` } // Validate checks constraints in the supplied configuration and returns an error if they are violated. diff --git a/internal/config/logging.go b/pkg/logging/config.go similarity index 57% rename from internal/config/logging.go rename to pkg/logging/config.go index 9ccd35e0..00eb1406 100644 --- a/internal/config/logging.go +++ b/pkg/logging/config.go @@ -1,28 +1,31 @@ -package config +package logging import ( - "github.com/icinga/icingadb/pkg/logging" + "fmt" "github.com/pkg/errors" "go.uber.org/zap/zapcore" "os" "time" ) -// Logging defines Logger configuration. -type Logging struct { +// Options define child loggers with their desired log level. +type Options map[string]zapcore.Level + +// Config defines Logger configuration. +type Config struct { // zapcore.Level at 0 is for info level. Level zapcore.Level `yaml:"level" default:"0"` Output string `yaml:"output"` // Interval for periodic logging. Interval time.Duration `yaml:"interval" default:"20s"` - logging.Options `yaml:"options"` + Options `yaml:"options"` } -// Validate checks constraints in the supplied Logging configuration and returns an error if they are violated. +// Validate checks constraints in the configuration and returns an error if they are violated. // Also configures the log output if it is not configured: // systemd-journald is used when Icinga DB is running under systemd, otherwise stderr. -func (l *Logging) Validate() error { +func (l *Config) Validate() error { if l.Interval <= 0 { return errors.New("periodic logging interval must be positive") } @@ -32,13 +35,26 @@ func (l *Logging) Validate() error { // When started by systemd, NOTIFY_SOCKET is set by systemd for Type=notify supervised services, // which is the default setting for the Icinga DB service. // This assumes that Icinga DB is running under systemd, so set output to systemd-journald. - l.Output = logging.JOURNAL + l.Output = JOURNAL } else { // Otherwise set it to console, i.e. write log messages to stderr. - l.Output = logging.CONSOLE + l.Output = CONSOLE } } // To be on the safe side, always call AssertOutput. - return logging.AssertOutput(l.Output) + return AssertOutput(l.Output) +} + +// AssertOutput returns an error if output is not a valid logger output. +func AssertOutput(o string) error { + if o == CONSOLE || o == JOURNAL { + return nil + } + + return invalidOutput(o) +} + +func invalidOutput(o string) error { + return fmt.Errorf("%s is not a valid logger output. Must be either %q or %q", o, CONSOLE, JOURNAL) } diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go index e3106956..93121465 100644 --- a/pkg/logging/logging.go +++ b/pkg/logging/logging.go @@ -1,7 +1,6 @@ package logging import ( - "fmt" "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" @@ -29,9 +28,6 @@ var defaultEncConfig = zapcore.EncoderConfig{ EncodeCaller: zapcore.ShortCallerEncoder, } -// Options define child loggers with their desired log level. -type Options map[string]zapcore.Level - // Logging implements access to a default logger and named child loggers. // Log levels can be configured per named child via Options which, if not configured, // fall back on a default log level. @@ -116,16 +112,3 @@ func (l *Logging) GetChildLogger(name string) *Logger { func (l *Logging) GetLogger() *Logger { return l.logger } - -// AssertOutput returns an error if output is not a valid logger output. -func AssertOutput(o string) error { - if o == CONSOLE || o == JOURNAL { - return nil - } - - return invalidOutput(o) -} - -func invalidOutput(o string) error { - return fmt.Errorf("%s is not a valid logger output. Must be either %q or %q", o, CONSOLE, JOURNAL) -}