Skip to content

Commit e33b64a

Browse files
committed
Move parsing logic to Parser
1 parent b4bce2a commit e33b64a

File tree

2 files changed

+317
-114
lines changed

2 files changed

+317
-114
lines changed

pkg/structuredlogger/configuration/parser/parser.go

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,113 @@ func convertMap(input interface{}) map[string]string {
2121
return result
2222
}
2323

24+
// Parser is the configuration parser for the structured logger.
25+
type Parser struct {
26+
// configuration is the parser.Configuration with the structured logger
27+
// configuration.
28+
configuration *parser.Configuration
29+
}
30+
31+
// NewParser returns a new instance of the Parser with the given
32+
// parser.Configuration.
33+
func NewParser(configuration parser.Configuration) *Parser {
34+
return &Parser{configuration: &configuration}
35+
}
36+
37+
// parseFile parses the file with the given parser function and returns the
38+
// Parser.
39+
func parseFile(file string, parserFunction func(string) (*parser.Configuration, error)) (*Parser, error) {
40+
configuration, err := parserFunction(file)
41+
if err != nil {
42+
return nil, err
43+
}
44+
return NewParser(*configuration), nil
45+
}
46+
47+
// ParseJSON parses the JSON file and returns the Parser.
48+
func ParseJSON(file string) (*Parser, error) {
49+
return parseFile(file, parser.ReadFromJSON)
50+
}
51+
52+
// ParseYAML parses the YAML file and returns the Parser.
53+
func ParseYAML(file string) (*Parser, error) {
54+
return parseFile(file, parser.ReadFromYAML)
55+
}
56+
57+
// ParseXML parses the XML file and returns the Parser.
58+
func ParseXML(file string) (*Parser, error) {
59+
return parseFile(file, parser.ReadFromXML)
60+
}
61+
2462
// parseFormatter parses parser.FormatterConfiguration configuration and returns
2563
// formatter.Interface.
26-
func parseFormatter(configuration parser.FormatterConfiguration) formatter.Interface {
64+
func (parser *Parser) parseFormatter(configuration parser.FormatterConfiguration) formatter.Interface {
2765
switch configuration.Type {
2866
case "json":
29-
return formatter.NewJSON(convertMap(configuration.Template), configuration.PrettyPrint)
67+
return formatter.NewJSON(configuration.Template.MapValue, configuration.PrettyPrint)
3068
case "key-value":
31-
return formatter.NewKeyValue(convertMap(configuration.Template), configuration.KeyValueDelimiter, configuration.PairSeparator)
69+
return formatter.NewKeyValue(configuration.Template.MapValue, configuration.KeyValueDelimiter, configuration.PairSeparator)
3270
default:
3371
panic("unknown formatter type.")
3472
}
3573
}
3674

3775
// parseHandler parses parser.HandlerConfiguration configuration and returns
3876
// handler.Interface.
39-
func parseHandler(configuration parser.HandlerConfiguration) handler.Interface {
77+
func (parser *Parser) parseHandler(configuration parser.HandlerConfiguration) handler.Interface {
4078
fromLevel := level.ParseLevel(strings.ToLower(configuration.FromLevel))
4179
toLevel := level.ParseLevel(strings.ToLower(configuration.ToLevel))
4280
switch configuration.Type {
4381
case "stdout":
44-
return handler.NewConsoleHandler(fromLevel, toLevel, parseFormatter(configuration.Formatter))
82+
return handler.NewConsoleHandler(fromLevel, toLevel, parser.parseFormatter(configuration.Formatter))
4583
case "stderr":
46-
return handler.NewConsoleErrorHandler(fromLevel, toLevel, parseFormatter(configuration.Formatter))
84+
return handler.NewConsoleErrorHandler(fromLevel, toLevel, parser.parseFormatter(configuration.Formatter))
4785
case "file":
4886
if configuration.File == "" {
4987
panic("file handler requires file option.")
5088
}
51-
return handler.NewFileHandler(fromLevel, toLevel, parseFormatter(configuration.Formatter), configuration.File)
89+
return handler.NewFileHandler(fromLevel, toLevel, parser.parseFormatter(configuration.Formatter), configuration.File)
5290
default:
5391
return nil
5492
}
5593
}
5694

5795
// parseLogger parses parser.LoggerConfiguration configuration and returns
5896
// structuredlogger.Logger.
59-
func parseLogger(configuration parser.LoggerConfiguration) *structuredlogger.Logger {
97+
func (parser *Parser) parseLogger(configuration parser.LoggerConfiguration) *structuredlogger.Logger {
6098
newLogger := structuredlogger.New(configuration.Name, configuration.TimeFormat)
6199
for _, handlerConfiguration := range configuration.Handlers {
62-
newLogger.AddHandler(parseHandler(handlerConfiguration))
100+
newLogger.AddHandler(parser.parseHandler(handlerConfiguration))
63101
}
64102
return newLogger
65103
}
66104

67105
// parseAsyncLogger parses parser.LoggerConfiguration configuration and returns
68106
// structuredlogger.AsyncLogger.
69-
func parseAsyncLogger(configuration parser.LoggerConfiguration) *structuredlogger.AsyncLogger {
107+
func (parser *Parser) parseAsyncLogger(configuration parser.LoggerConfiguration) *structuredlogger.AsyncLogger {
70108
newLogger := structuredlogger.NewAsyncLogger(configuration.Name, configuration.TimeFormat, configuration.MessageQueueSize)
71109
for _, handlerConfiguration := range configuration.Handlers {
72-
newLogger.AddHandler(parseHandler(handlerConfiguration))
110+
newLogger.AddHandler(parser.parseHandler(handlerConfiguration))
73111
}
74112
return newLogger
75113
}
76114

77115
// GetLogger returns structuredlogger.Logger by name from the configuration.
78-
func GetLogger(name string, configuration parser.Configuration) *structuredlogger.Logger {
79-
for _, loggerConfiguration := range configuration.Loggers {
116+
func (parser *Parser) GetLogger(name string) *structuredlogger.Logger {
117+
for _, loggerConfiguration := range parser.configuration.Loggers {
80118
if loggerConfiguration.Name == name {
81-
return parseLogger(loggerConfiguration)
119+
return parser.parseLogger(loggerConfiguration)
82120
}
83121
}
84122
return nil
85123
}
86124

87125
// GetAsyncLogger returns structuredlogger.AsyncLogger by name from the
88126
// configuration.
89-
func GetAsyncLogger(name string, configuration parser.Configuration) *structuredlogger.AsyncLogger {
90-
for _, loggerConfiguration := range configuration.Loggers {
127+
func (parser *Parser) GetAsyncLogger(name string) *structuredlogger.AsyncLogger {
128+
for _, loggerConfiguration := range parser.configuration.Loggers {
91129
if loggerConfiguration.Name == name {
92-
return parseAsyncLogger(loggerConfiguration)
130+
return parser.parseAsyncLogger(loggerConfiguration)
93131
}
94132
}
95133
return nil

0 commit comments

Comments
 (0)