-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paw/ct 1844 log params take 2 #6994
Changes from 11 commits
b287a78
f6f733c
6ddde18
84a8502
ab952c9
bf7bee0
734c634
68845a6
1171796
a704416
9a15490
7a7b476
9cb5fa0
cc52342
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Implemented new log cli parameters for finer-grained control. | ||
time: 2023-02-16T14:45:34.038453-05:00 | ||
custom: | ||
Author: peterallenwebb | ||
Issue: "6639" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import betterproto | ||
from dbt.constants import METADATA_ENV_PREFIX | ||
from dbt.events.base_types import BaseEvent, Cache, EventLevel, NoFile, NoStdOut, EventMsg | ||
from dbt.events.eventmgr import EventManager, LoggerConfig, LineFormat | ||
from dbt.events.eventmgr import EventManager, LoggerConfig, LineFormat, NoFilter | ||
from dbt.events.helpers import env_secrets, scrub_secrets | ||
from dbt.events.types import Formatting | ||
from dbt.flags import get_flags, ENABLE_LEGACY_LOGGER | ||
|
@@ -18,103 +18,146 @@ | |
metadata_vars: Optional[Dict[str, str]] = None | ||
|
||
|
||
def setup_event_logger( | ||
log_path: str, | ||
log_format: str, | ||
use_colors: bool, | ||
debug: bool, | ||
log_cache_events: bool, | ||
quiet: bool, | ||
) -> None: | ||
def setup_event_logger(flags) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we able to annotate the type of |
||
cleanup_event_logger() | ||
make_log_dir_if_missing(log_path) | ||
make_log_dir_if_missing(flags.LOG_PATH) | ||
|
||
if ENABLE_LEGACY_LOGGER: | ||
EVENT_MANAGER.add_logger(_get_logbook_log_config(debug)) | ||
else: | ||
EVENT_MANAGER.add_logger( | ||
_get_stdout_config(log_format, debug, use_colors, log_cache_events, quiet) | ||
_get_logbook_log_config( | ||
flags.DEBUG, flags.USE_COLORS, flags.LOG_CACHE_EVENTS, flags.QUIET | ||
) | ||
) | ||
else: | ||
if flags.LOG_LEVEL != "none": | ||
log_format = _line_format_from_str(flags.LOG_FORMAT, LineFormat.PlainText) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, the variable being assigned here should be line_format. There is no difference between the two things, and the difference in naming is unfortunate. |
||
log_level = EventLevel.DEBUG if flags.DEBUG else EventLevel(flags.LOG_LEVEL) | ||
EVENT_MANAGER.add_logger( | ||
_get_stdout_config( | ||
log_format, | ||
flags.DEBUG, | ||
flags.USE_COLORS, | ||
log_level, | ||
flags.LOG_CACHE_EVENTS, | ||
flags.QUIET, | ||
) | ||
) | ||
|
||
if _CAPTURE_STREAM: | ||
# Create second stdout logger to support test which want to know what's | ||
# being sent to stdout. | ||
# debug here is true because we need to capture debug events, and we pass in false in main | ||
capture_config = _get_stdout_config( | ||
log_format, True, use_colors, log_cache_events, quiet | ||
if _CAPTURE_STREAM: | ||
# Create second stdout logger to support test which want to know what's | ||
# being sent to stdout. | ||
# debug here is true because we need to capture debug events, and we pass in false in main | ||
capture_config = _get_stdout_config( | ||
log_format, | ||
flags.DEBUG, | ||
flags.USE_COLORS, | ||
log_level, | ||
flags.LOG_CACHE_EVENTS, | ||
flags.QUIET, | ||
) | ||
capture_config.output_stream = _CAPTURE_STREAM | ||
EVENT_MANAGER.add_logger(capture_config) | ||
|
||
if flags.LOG_LEVEL_FILE != "none": | ||
# create and add the file logger to the event manager | ||
log_file = os.path.join(flags.LOG_PATH, "dbt.log") | ||
log_file_format = _line_format_from_str(flags.LOG_FORMAT_FILE, LineFormat.DebugText) | ||
log_level_file = EventLevel.DEBUG if flags.DEBUG else EventLevel(flags.LOG_LEVEL_FILE) | ||
EVENT_MANAGER.add_logger( | ||
_get_logfile_config( | ||
log_file, flags.USE_COLORS_FILE, log_file_format, log_level_file | ||
) | ||
) | ||
capture_config.output_stream = _CAPTURE_STREAM | ||
EVENT_MANAGER.add_logger(capture_config) | ||
|
||
# create and add the file logger to the event manager | ||
EVENT_MANAGER.add_logger( | ||
_get_logfile_config(os.path.join(log_path, "dbt.log"), use_colors, log_format) | ||
) | ||
|
||
def _line_format_from_str(format_str: str, default: LineFormat) -> LineFormat: | ||
if format_str == "text": | ||
return LineFormat.PlainText | ||
elif format_str == "debug": | ||
return LineFormat.DebugText | ||
elif format_str == "json": | ||
return LineFormat.Json | ||
|
||
return default | ||
|
||
|
||
def _get_stdout_config( | ||
log_format: str, debug: bool, use_colors: bool, log_cache_events: bool, quiet: bool | ||
line_format: LineFormat, | ||
debug: bool, | ||
use_colors: bool, | ||
level: EventLevel, | ||
log_cache_events: bool, | ||
quiet: bool, | ||
) -> LoggerConfig: | ||
fmt = LineFormat.PlainText | ||
if log_format == "json": | ||
fmt = LineFormat.Json | ||
elif debug: | ||
fmt = LineFormat.DebugText | ||
level = EventLevel.DEBUG if debug else EventLevel.INFO | ||
|
||
return LoggerConfig( | ||
name="stdout_log", | ||
level=level, | ||
use_colors=use_colors, | ||
line_format=fmt, | ||
line_format=line_format, | ||
scrubber=env_scrubber, | ||
filter=partial( | ||
_stdout_filter, | ||
log_cache_events, | ||
debug, | ||
quiet, | ||
log_format, | ||
line_format, | ||
), | ||
output_stream=sys.stdout, | ||
) | ||
|
||
|
||
def _stdout_filter( | ||
log_cache_events: bool, debug_mode: bool, quiet_mode: bool, log_format: str, msg: EventMsg | ||
log_cache_events: bool, | ||
debug_mode: bool, | ||
quiet_mode: bool, | ||
line_format: LineFormat, | ||
msg: EventMsg, | ||
) -> bool: | ||
return ( | ||
not isinstance(msg.data, NoStdOut) | ||
and (not isinstance(msg.data, Cache) 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 (log_format == "json" and type(msg.data) == Formatting) | ||
and not (line_format == LineFormat.Json and type(msg.data) == Formatting) | ||
) | ||
|
||
|
||
def _get_logfile_config(log_path: str, use_colors: bool, log_format: str) -> LoggerConfig: | ||
def _get_logfile_config( | ||
log_path: str, use_colors: bool, line_format: LineFormat, level: EventLevel | ||
) -> LoggerConfig: | ||
return LoggerConfig( | ||
name="file_log", | ||
line_format=LineFormat.Json if log_format == "json" else LineFormat.DebugText, | ||
line_format=line_format, | ||
use_colors=use_colors, | ||
level=EventLevel.DEBUG, # File log is *always* debug level | ||
level=level, # File log is *always* debug level | ||
scrubber=env_scrubber, | ||
filter=partial(_logfile_filter, bool(get_flags().LOG_CACHE_EVENTS), log_format), | ||
filter=partial(_logfile_filter, bool(get_flags().LOG_CACHE_EVENTS), line_format), | ||
output_file_name=log_path, | ||
) | ||
|
||
|
||
def _logfile_filter(log_cache_events: bool, log_format: str, msg: EventMsg) -> bool: | ||
def _logfile_filter(log_cache_events: bool, line_format: LineFormat, msg: EventMsg) -> bool: | ||
return ( | ||
not isinstance(msg.data, NoFile) | ||
and not (isinstance(msg.data, Cache) and not log_cache_events) | ||
and not (log_format == "json" and type(msg.data) == Formatting) | ||
and not (line_format == LineFormat.Json and type(msg.data) == Formatting) | ||
) | ||
|
||
|
||
def _get_logbook_log_config(debug: bool) -> LoggerConfig: | ||
# use the default one since this code should be removed when we remove logbook | ||
config = _get_stdout_config("", debug, False, False, False) | ||
def _get_logbook_log_config( | ||
debug: bool, use_colors: bool, log_cache_events: bool, quiet: bool | ||
) -> LoggerConfig: | ||
config = _get_stdout_config( | ||
LineFormat.PlainText, | ||
debug, | ||
use_colors, | ||
EventLevel.DEBUG if debug else EventLevel.INFO, | ||
log_cache_events, | ||
quiet, | ||
) | ||
config.name = "logbook_log" | ||
config.filter = lambda e: not isinstance(e.data, Cache) | ||
config.filter = NoFilter if log_cache_events else lambda e: not isinstance(e.data, Cache) | ||
config.logger = GLOBAL_LOGGER | ||
config.output_stream = None | ||
return config | ||
|
@@ -136,13 +179,10 @@ def cleanup_event_logger(): | |
# currently fire before logs can be configured by setup_event_logger(), we | ||
# create a default configuration with default settings and no file output. | ||
EVENT_MANAGER: EventManager = EventManager() | ||
# Problem: This needs to be set *BEFORE* we've resolved any flags (even CLI params) | ||
EVENT_MANAGER.add_logger( | ||
_get_logbook_log_config(debug=False) # type: ignore | ||
_get_logbook_log_config(False, True, False, False) # type: ignore | ||
if ENABLE_LEGACY_LOGGER | ||
else _get_stdout_config( | ||
log_format="text", debug=False, use_colors=True, log_cache_events=False, quiet=False | ||
) # type: ignore | ||
else _get_stdout_config(LineFormat.PlainText, False, True, EventLevel.INFO, False, False) | ||
) | ||
|
||
# This global, and the following two functions for capturing stdout logs are | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny nit: documenting the method in a docstring below the signature would be more pythonic + consistent with the rest of this file.