From 1ddeec9225fa41a5ca34f65b2c6222fee25cda4a Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Mon, 6 Mar 2017 13:31:10 +1000 Subject: [PATCH] config: Parse logger config --- irc/config.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- irc/logger.go | 18 ++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/irc/config.go b/irc/config.go index ac354945..1e380eaf 100644 --- a/irc/config.go +++ b/irc/config.go @@ -116,6 +116,17 @@ type ConnectionThrottleConfig struct { Exempted []string } +type LoggingConfig struct { + Method string + Methods map[string]bool + Filename string + TypeString string `yaml:"type"` + Types map[string]bool `yaml:"real-types"` + ExcludedTypes map[string]bool `yaml:"real-excluded-types"` + LevelString string `yaml:"level"` + Level LogLevel `yaml:"level-real"` +} + type LineLenConfig struct { Tags int Rest int @@ -135,7 +146,6 @@ type Config struct { TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"` RestAPI RestAPIConfig `yaml:"rest-api"` CheckIdent bool `yaml:"check-ident"` - Log string MOTD string ConnectionLimits ConnectionLimitsConfig `yaml:"connection-limits"` ConnectionThrottle ConnectionThrottleConfig `yaml:"connection-throttling"` @@ -154,6 +164,8 @@ type Config struct { Opers map[string]*OperConfig + Logging []LoggingConfig + Limits struct { AwayLen uint `yaml:"awaylen"` ChanListModes uint `yaml:"chan-list-modes"` @@ -343,6 +355,43 @@ func LoadConfig(filename string) (config *Config, err error) { if config.Limits.LineLen.Tags < 512 || config.Limits.LineLen.Rest < 512 { return nil, errors.New("Line lengths must be 512 or greater (check the linelen section under server->limits)") } + for _, logConfig := range config.Logging { + // methods + for _, method := range strings.Split(logConfig.Method, " ") { + if len(method) > 0 { + logConfig.Methods[strings.ToLower(method)] = true + } + } + if logConfig.Methods["file"] && logConfig.Filename == "" { + return nil, errors.New("Logging configuration specifies 'file' method but 'filename' is empty") + } + + // levels + level, exists := logLevelNames[strings.ToLower(logConfig.LevelString)] + if !exists { + return nil, fmt.Errorf("Could not translate log leve [%s]", logConfig.LevelString) + } + logConfig.Level = level + + // types + for _, typeStr := range strings.Split(logConfig.TypeString, " ") { + if len(typeStr) == 0 { + continue + } + if typeStr == "-" { + return nil, errors.New("Encountered logging type '-' with no type to exclude") + } + if typeStr[0] == '-' { + typeStr = typeStr[1:] + logConfig.ExcludedTypes[typeStr] = true + } else { + logConfig.Types[typeStr] = true + } + } + if len(logConfig.Types) < 1 { + return nil, errors.New("Logger has no types to log") + } + } return config, nil } diff --git a/irc/logger.go b/irc/logger.go index 90348521..b049e8ac 100644 --- a/irc/logger.go +++ b/irc/logger.go @@ -14,8 +14,10 @@ import ( type LogLevel int const ( + // LogErr is an error value. + LogErr LogLevel = iota // LogDebug represents debug messages. - LogDebug LogLevel = iota + LogDebug // LogInfo represents informational messages. LogInfo // LogWarn represents warnings. @@ -24,6 +26,18 @@ const ( LogError ) +var ( + logLevelNames = map[string]LogLevel{ + "debug": LogDebug, + "info": LogInfo, + "warn": LogWarn, + "warning": LogWarn, + "warnings": LogWarn, + "error": LogError, + "errors": LogError, + } +) + // ClientLogger is a logger dedicated to a single client. This is a convenience class that // automagically adds the client nick to logged messages. type ClientLogger struct { @@ -50,7 +64,7 @@ type Logger struct { } // NewLogger returns a new Logger. -func NewLogger(config LogConfig) (*Logger, error) { +func NewLogger(config LoggingConfig) (*Logger, error) { return nil, fmt.Errorf("Not implemented") }