From 608e08a5080e39180b737a036023867eb51d5065 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Tue, 22 Oct 2024 14:45:34 +0100 Subject: [PATCH] fix: Reset logger context when streaming logs from plugins (#421) #### Summary Fixes https://github.com/cloudquery/cloudquery-issues/issues/2624 (internal issue) We were using the logger created here https://github.com/cloudquery/cloudquery/blob/69143a73499660db2ab714781a5d207884fb9303/cli/cmd/logging.go#L51 to output logs messages from plugins. Plugins already set their log context in https://github.com/cloudquery/plugin-sdk/blob/8b9e067af19604d90d2e77d6e1aff1d93adad752/plugin/plugin.go#L213 and https://github.com/cloudquery/plugin-sdk/blob/8b9e067af19604d90d2e77d6e1aff1d93adad752/plugin/plugin.go#L241, so when we stream them to the `cloudquery.log` file we should use a new logger with an empty context. --- --- managedplugin/logging.go | 3 +-- managedplugin/plugin.go | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/managedplugin/logging.go b/managedplugin/logging.go index 131a31d..fee7de0 100644 --- a/managedplugin/logging.go +++ b/managedplugin/logging.go @@ -4,9 +4,8 @@ import "github.com/rs/zerolog" func (c *Client) jsonToLog(l zerolog.Logger, msg map[string]any) { level := msg["level"] - // Delete fields already added by the CLI so that they don't appear twice in the logs when we stream it from plugins + // The log level is part of the log message received from the plugin, so we need to remove it before logging delete(msg, "level") - delete(msg, "invocation_id") switch level { case "trace": l.Trace().Fields(msg).Msg("") diff --git a/managedplugin/plugin.go b/managedplugin/plugin.go index a9d7338..981c032 100644 --- a/managedplugin/plugin.go +++ b/managedplugin/plugin.go @@ -548,6 +548,8 @@ func (c *Client) getPluginArgs() []string { func (c *Client) readLogLines(reader io.ReadCloser) { defer c.wg.Done() lr := NewLogReader(reader) + // reset the context to avoid duplicate fields in the logs when streaming logs from plugins + pluginsLogger := c.logger.With().Reset().Timestamp().Logger() for { line, err := lr.NextLine() if errors.Is(err, io.EOF) { @@ -565,7 +567,7 @@ func (c *Client) readLogLines(reader io.ReadCloser) { if err := json.Unmarshal(line, &structuredLogLine); err != nil { c.logger.Info().Str("level", "unknown").Msg(string(line)) } else { - c.jsonToLog(c.logger, structuredLogLine) + c.jsonToLog(pluginsLogger, structuredLogLine) } } }