Add logging#Config

This commit is contained in:
Eric Lippmann 2024-05-21 11:54:42 +02:00
parent 1d9dc74391
commit 563045cc5a
3 changed files with 31 additions and 31 deletions

View file

@ -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.

View file

@ -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)
}

View file

@ -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)
}