Skip to content

Commit ff7b598

Browse files
Enable otel logs to be redirected to logfile if configured (#179)
1 parent ee8aef9 commit ff7b598

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

cmd/host-agent/main.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ func main() {
249249

250250
CallerKey: "caller",
251251
EncodeCaller: zapcore.ShortCallerEncoder,
252+
253+
StacktraceKey: "stacktrace",
254+
LineEnding: zapcore.DefaultLineEnding,
252255
}
253256
zapCfg := zap.NewProductionConfig()
254257
zapCfg.EncoderConfig = zapEncoderCfg
@@ -282,6 +285,7 @@ func main() {
282285
go profiler.StartProfiling("mw-host-agent", cfg.Target, cfg.HostTags)
283286
}
284287

288+
var zapFileCore zapcore.Core
285289
if cfg.Logfile != "" {
286290
logger.Info("redirecting logs to logfile", zap.String("logfile", cfg.Logfile))
287291
// logfile specified. Update logger to write logs to the
@@ -292,13 +296,13 @@ func main() {
292296
MaxBackups: 1,
293297
MaxAge: 7, // days
294298
})
295-
core := zapcore.NewCore(
299+
zapFileCore = zapcore.NewCore(
296300
zapcore.NewJSONEncoder(zapEncoderCfg),
297301
w,
298302
zap.InfoLevel,
299303
)
300304

301-
logger = zap.New(core)
305+
logger = zap.New(zapFileCore)
302306
}
303307

304308
infraPlatform := agent.InfraPlatformInstance
@@ -408,10 +412,20 @@ func main() {
408412

409413
settings := otelcol.CollectorSettings{
410414
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+
}(),
415429

416430
BuildInfo: component.BuildInfo{
417431
Command: "mw-otelcontribcol",

0 commit comments

Comments
 (0)