@@ -38,7 +38,6 @@ import (
38
38
"github.com/spf13/cobra"
39
39
"github.com/spf13/viper"
40
40
"go.uber.org/zap/zapcore"
41
- iniv1 "gopkg.in/ini.v1"
42
41
"gopkg.in/natefinch/lumberjack.v2"
43
42
)
44
43
@@ -53,17 +52,10 @@ type diagnostics struct {
53
52
func RunE (cmd * cobra.Command , v * viper.Viper ) error {
54
53
ctx := context .Background ()
55
54
56
- // force setup logging otherwise log goes to std out
57
- logger , err := SetupLogging (ctx , v )
58
- if err != nil {
59
- // log to std out as logger is not setup yet
60
- stdlog .Fatalf ("failed to setup logging: %s" , err )
61
- }
62
-
63
- // save logger to context
64
- ctx = log .ToContext (ctx , logger )
55
+ // extract logger from context despite it's not fully initialized yet
56
+ logger := log .Extract (ctx )
65
57
66
- err = parseConfigFiles (ctx , v )
58
+ err : = parseConfigFiles (ctx , v )
67
59
if err != nil {
68
60
logger .Errorf ("failed to parse config files: %s" , err )
69
61
@@ -74,12 +66,15 @@ func RunE(cmd *cobra.Command, v *viper.Viper) error {
74
66
}
75
67
}
76
68
77
- // setup logging again to use config file settings if available
78
69
logger , err = SetupLogging (ctx , v )
79
70
if err != nil {
80
- logger .Fatalf ("failed to setup logging: %s" , err )
71
+ // log to std out and exit, as logger instance failed to setup
72
+ stdlog .Fatalf ("failed to setup logging: %s" , err )
81
73
}
82
74
75
+ // save logger to context
76
+ ctx = log .ToContext (ctx , logger )
77
+
83
78
// register all custom lexers
84
79
if err := lexer .RegisterAll (); err != nil {
85
80
logger .Fatalf ("failed to register custom lexers: %s" , err )
@@ -183,30 +178,24 @@ func RunE(cmd *cobra.Command, v *viper.Viper) error {
183
178
}
184
179
185
180
func parseConfigFiles (ctx context.Context , v * viper.Viper ) error {
181
+ logger := log .Extract (ctx )
182
+
186
183
var configFiles = []struct {
187
- fn func (context.Context , * viper.Viper ) (string , error )
188
- vp * viper.Viper
189
- merge bool
184
+ filePathFn func (context.Context , * viper.Viper ) (string , error )
190
185
}{
191
186
{
192
- fn : ini .FilePath ,
193
- vp : v ,
187
+ filePathFn : ini .FilePath ,
194
188
},
195
189
{
196
- fn : ini .ImportFilePath ,
197
- vp : v ,
190
+ filePathFn : ini .ImportFilePath ,
198
191
},
199
192
{
200
- fn : ini .InternalFilePath ,
201
- vp : viper .NewWithOptions (viper .IniLoadOptions (iniv1.LoadOptions {SkipUnrecognizableLines : true })),
202
- merge : true ,
193
+ filePathFn : ini .InternalFilePath ,
203
194
},
204
195
}
205
196
206
- logger := log .Extract (ctx )
207
-
208
197
for _ , c := range configFiles {
209
- configFile , err := c .fn (ctx , v )
198
+ configFile , err := c .filePathFn (ctx , v )
210
199
if err != nil {
211
200
return fmt .Errorf ("error getting config file path: %s" , err )
212
201
}
@@ -221,16 +210,9 @@ func parseConfigFiles(ctx context.Context, v *viper.Viper) error {
221
210
continue
222
211
}
223
212
224
- if err := ini .ReadInConfig (c . vp , configFile ); err != nil {
213
+ if err := ini .ReadInConfig (v , configFile ); err != nil {
225
214
return fmt .Errorf ("failed to load configuration file: %s" , err )
226
215
}
227
-
228
- if c .merge {
229
- err = v .MergeConfigMap (c .vp .AllSettings ())
230
- if err != nil {
231
- logger .Warnf ("failed to merge configuration file: %s" , err )
232
- }
233
- }
234
216
}
235
217
236
218
return nil
@@ -251,10 +233,11 @@ func SetupLogging(ctx context.Context, v *viper.Viper) (*log.Logger, error) {
251
233
if _ , err := os .Stat (dir ); os .IsNotExist (err ) {
252
234
err := os .MkdirAll (dir , 0750 )
253
235
if err != nil {
254
- return nil , fmt .Errorf ("error creating log file directory: %s" , err )
236
+ return nil , fmt .Errorf ("failed to create log file directory %q : %s" , dir , err )
255
237
}
256
238
}
257
239
240
+ // rotate log files
258
241
destOutput = & lumberjack.Logger {
259
242
Filename : params .File ,
260
243
MaxSize : log .MaxLogFileSize ,
@@ -269,8 +252,6 @@ func SetupLogging(ctx context.Context, v *viper.Viper) (*log.Logger, error) {
269
252
log .WithMetrics (params .Metrics ),
270
253
)
271
254
272
- log .SetJww (params .Verbose , destOutput )
273
-
274
255
return logger , nil
275
256
}
276
257
@@ -306,13 +287,13 @@ func runCmd(ctx context.Context, v *viper.Viper, verbose bool, sendDiagsOnErrors
306
287
307
288
// catch panics
308
289
defer func () {
309
- if err := recover (); err != nil {
310
- logger .Errorf ("panicked: %v. Stack: %s" , err , string (debug .Stack ()))
290
+ if r := recover (); r != nil {
291
+ logger .Errorf ("panicked: %v. Stack: %s" , r , string (debug .Stack ()))
311
292
312
293
resetLogs ()
313
294
314
295
diags := diagnostics {
315
- OriginalError : err ,
296
+ OriginalError : r ,
316
297
Panicked : true ,
317
298
Stack : string (debug .Stack ()),
318
299
}
0 commit comments