@@ -249,6 +249,9 @@ func main() {
249
249
250
250
CallerKey : "caller" ,
251
251
EncodeCaller : zapcore .ShortCallerEncoder ,
252
+
253
+ StacktraceKey : "stacktrace" ,
254
+ LineEnding : zapcore .DefaultLineEnding ,
252
255
}
253
256
zapCfg := zap .NewProductionConfig ()
254
257
zapCfg .EncoderConfig = zapEncoderCfg
@@ -282,6 +285,7 @@ func main() {
282
285
go profiler .StartProfiling ("mw-host-agent" , cfg .Target , cfg .HostTags )
283
286
}
284
287
288
+ var zapFileCore zapcore.Core
285
289
if cfg .Logfile != "" {
286
290
logger .Info ("redirecting logs to logfile" , zap .String ("logfile" , cfg .Logfile ))
287
291
// logfile specified. Update logger to write logs to the
@@ -292,13 +296,13 @@ func main() {
292
296
MaxBackups : 1 ,
293
297
MaxAge : 7 , // days
294
298
})
295
- core : = zapcore .NewCore (
299
+ zapFileCore = zapcore .NewCore (
296
300
zapcore .NewJSONEncoder (zapEncoderCfg ),
297
301
w ,
298
302
zap .InfoLevel ,
299
303
)
300
304
301
- logger = zap .New (core )
305
+ logger = zap .New (zapFileCore )
302
306
}
303
307
304
308
infraPlatform := agent .InfraPlatformInstance
@@ -408,10 +412,20 @@ func main() {
408
412
409
413
settings := otelcol.CollectorSettings {
410
414
DisableGracefulShutdown : true ,
411
- LoggingOptions : []zap.Option {
412
- // zap.Development(),
413
- // zap.IncreaseLevel(zap.DebugLevel),
414
- },
415
+ LoggingOptions : func () []zap.Option {
416
+ // if logfile is specified, then write logs to the file using zapFileCore
417
+ if cfg .Logfile != "" {
418
+ return []zap.Option {
419
+ zap .WrapCore (func (core zapcore.Core ) zapcore.Core {
420
+ return zapFileCore
421
+ }),
422
+ }
423
+ }
424
+ return []zap.Option {
425
+ // zap.Development(),
426
+ // zap.IncreaseLevel(zap.DebugLevel),
427
+ }
428
+ }(),
415
429
416
430
BuildInfo : component.BuildInfo {
417
431
Command : "mw-otelcontribcol" ,
0 commit comments