Skip to content

Commit

Permalink
Fix accidental propagation of log messages to root logger (#7882)
Browse files Browse the repository at this point in the history
* Fix accidental propagation of log messages to root logger.

* Add changelog entry

* Fixed an issue which blocked debug logging to stdout with --log-level debug, unless --debug was also used.
  • Loading branch information
peterallenwebb authored Jul 11, 2023
1 parent 44572e7 commit a8e3afe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230615-102639.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix accidental propagation of log messages to root logger.
time: 2023-06-15T10:26:39.139421-04:00
custom:
Author: peterallenwebb
Issue: "7872"
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20230615-110538.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Fixed an issue which blocked debug logging to stdout with --log-level debug,
unless --debug was also used.
time: 2023-06-15T11:05:38.053387-04:00
custom:
Author: peterallenwebb
Issue: "7872"
1 change: 1 addition & 0 deletions core/dbt/events/eventmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def _get_python_log_for_handler(self, handler: logging.Handler):
log.setLevel(_log_level_map[self.level])
handler.setFormatter(logging.Formatter(fmt="%(message)s"))
log.handlers.clear()
log.propagate = False
log.addHandler(handler)
return log

Expand Down
29 changes: 11 additions & 18 deletions core/dbt/events/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ def setup_event_logger(flags, callbacks: List[Callable[[EventMsg], None]] = [])
else:
if flags.LOG_LEVEL != "none":
line_format = _line_format_from_str(flags.LOG_FORMAT, LineFormat.PlainText)
log_level = EventLevel.DEBUG if flags.DEBUG else EventLevel(flags.LOG_LEVEL)
log_level = (
EventLevel.ERROR
if flags.QUIET
else EventLevel.DEBUG
if flags.DEBUG
else EventLevel(flags.LOG_LEVEL)
)
console_config = _get_stdout_config(
line_format,
flags.DEBUG,
flags.USE_COLORS,
log_level,
flags.LOG_CACHE_EVENTS,
flags.QUIET,
)
EVENT_MANAGER.add_logger(console_config)

Expand Down Expand Up @@ -81,11 +85,9 @@ def _line_format_from_str(format_str: str, default: LineFormat) -> LineFormat:

def _get_stdout_config(
line_format: LineFormat,
debug: bool,
use_colors: bool,
level: EventLevel,
log_cache_events: bool,
quiet: bool,
) -> LoggerConfig:

return LoggerConfig(
Expand All @@ -97,8 +99,6 @@ def _get_stdout_config(
filter=partial(
_stdout_filter,
log_cache_events,
debug,
quiet,
line_format,
),
output_stream=sys.stdout,
Expand All @@ -107,16 +107,11 @@ def _get_stdout_config(

def _stdout_filter(
log_cache_events: bool,
debug_mode: bool,
quiet_mode: bool,
line_format: LineFormat,
msg: EventMsg,
) -> bool:
return (
(msg.info.name not in ["CacheAction", "CacheDumpGraph"] or log_cache_events)
and (EventLevel(msg.info.level) != EventLevel.DEBUG or debug_mode)
and (EventLevel(msg.info.level) == EventLevel.ERROR or not quiet_mode)
and not (line_format == LineFormat.Json and type(msg.data) == Formatting)
return (msg.info.name not in ["CacheAction", "CacheDumpGraph"] or log_cache_events) and not (
line_format == LineFormat.Json and type(msg.data) == Formatting
)


Expand Down Expand Up @@ -147,11 +142,9 @@ def _get_logbook_log_config(
) -> LoggerConfig:
config = _get_stdout_config(
LineFormat.PlainText,
debug,
use_colors,
EventLevel.DEBUG if debug else EventLevel.INFO,
EventLevel.ERROR if quiet else EventLevel.DEBUG if debug else EventLevel.INFO,
log_cache_events,
quiet,
)
config.name = "logbook_log"
config.filter = (
Expand Down Expand Up @@ -183,7 +176,7 @@ def cleanup_event_logger():
EVENT_MANAGER.add_logger(
_get_logbook_log_config(False, True, False, False) # type: ignore
if ENABLE_LEGACY_LOGGER
else _get_stdout_config(LineFormat.PlainText, False, True, EventLevel.INFO, False, False)
else _get_stdout_config(LineFormat.PlainText, True, EventLevel.INFO, False)
)

# This global, and the following two functions for capturing stdout logs are
Expand Down

0 comments on commit a8e3afe

Please sign in to comment.