config: Parse logger config

This commit is contained in:
Daniel Oaks 2017-03-06 13:31:10 +10:00
parent a982d965f1
commit 1ddeec9225
2 changed files with 66 additions and 3 deletions

View File

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

View File

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