@@ -21,75 +21,113 @@ func convertMap(input interface{}) map[string]string {
21
21
return result
22
22
}
23
23
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
+
24
62
// parseFormatter parses parser.FormatterConfiguration configuration and returns
25
63
// formatter.Interface.
26
- func parseFormatter (configuration parser.FormatterConfiguration ) formatter.Interface {
64
+ func ( parser * Parser ) parseFormatter (configuration parser.FormatterConfiguration ) formatter.Interface {
27
65
switch configuration .Type {
28
66
case "json" :
29
- return formatter .NewJSON (convertMap ( configuration .Template ) , configuration .PrettyPrint )
67
+ return formatter .NewJSON (configuration .Template . MapValue , configuration .PrettyPrint )
30
68
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 )
32
70
default :
33
71
panic ("unknown formatter type." )
34
72
}
35
73
}
36
74
37
75
// parseHandler parses parser.HandlerConfiguration configuration and returns
38
76
// handler.Interface.
39
- func parseHandler (configuration parser.HandlerConfiguration ) handler.Interface {
77
+ func ( parser * Parser ) parseHandler (configuration parser.HandlerConfiguration ) handler.Interface {
40
78
fromLevel := level .ParseLevel (strings .ToLower (configuration .FromLevel ))
41
79
toLevel := level .ParseLevel (strings .ToLower (configuration .ToLevel ))
42
80
switch configuration .Type {
43
81
case "stdout" :
44
- return handler .NewConsoleHandler (fromLevel , toLevel , parseFormatter (configuration .Formatter ))
82
+ return handler .NewConsoleHandler (fromLevel , toLevel , parser . parseFormatter (configuration .Formatter ))
45
83
case "stderr" :
46
- return handler .NewConsoleErrorHandler (fromLevel , toLevel , parseFormatter (configuration .Formatter ))
84
+ return handler .NewConsoleErrorHandler (fromLevel , toLevel , parser . parseFormatter (configuration .Formatter ))
47
85
case "file" :
48
86
if configuration .File == "" {
49
87
panic ("file handler requires file option." )
50
88
}
51
- return handler .NewFileHandler (fromLevel , toLevel , parseFormatter (configuration .Formatter ), configuration .File )
89
+ return handler .NewFileHandler (fromLevel , toLevel , parser . parseFormatter (configuration .Formatter ), configuration .File )
52
90
default :
53
91
return nil
54
92
}
55
93
}
56
94
57
95
// parseLogger parses parser.LoggerConfiguration configuration and returns
58
96
// structuredlogger.Logger.
59
- func parseLogger (configuration parser.LoggerConfiguration ) * structuredlogger.Logger {
97
+ func ( parser * Parser ) parseLogger (configuration parser.LoggerConfiguration ) * structuredlogger.Logger {
60
98
newLogger := structuredlogger .New (configuration .Name , configuration .TimeFormat )
61
99
for _ , handlerConfiguration := range configuration .Handlers {
62
- newLogger .AddHandler (parseHandler (handlerConfiguration ))
100
+ newLogger .AddHandler (parser . parseHandler (handlerConfiguration ))
63
101
}
64
102
return newLogger
65
103
}
66
104
67
105
// parseAsyncLogger parses parser.LoggerConfiguration configuration and returns
68
106
// structuredlogger.AsyncLogger.
69
- func parseAsyncLogger (configuration parser.LoggerConfiguration ) * structuredlogger.AsyncLogger {
107
+ func ( parser * Parser ) parseAsyncLogger (configuration parser.LoggerConfiguration ) * structuredlogger.AsyncLogger {
70
108
newLogger := structuredlogger .NewAsyncLogger (configuration .Name , configuration .TimeFormat , configuration .MessageQueueSize )
71
109
for _ , handlerConfiguration := range configuration .Handlers {
72
- newLogger .AddHandler (parseHandler (handlerConfiguration ))
110
+ newLogger .AddHandler (parser . parseHandler (handlerConfiguration ))
73
111
}
74
112
return newLogger
75
113
}
76
114
77
115
// 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 {
80
118
if loggerConfiguration .Name == name {
81
- return parseLogger (loggerConfiguration )
119
+ return parser . parseLogger (loggerConfiguration )
82
120
}
83
121
}
84
122
return nil
85
123
}
86
124
87
125
// GetAsyncLogger returns structuredlogger.AsyncLogger by name from the
88
126
// 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 {
91
129
if loggerConfiguration .Name == name {
92
- return parseAsyncLogger (loggerConfiguration )
130
+ return parser . parseAsyncLogger (loggerConfiguration )
93
131
}
94
132
}
95
133
return nil
0 commit comments