Skip to content

Commit

Permalink
Colorize levels in text logs, rm duplicate WARNINGs
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcohen6 committed Nov 17, 2021
1 parent fca304c commit 6398157
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/dbt/deps/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _fetch_metadata(self, project, renderer) -> ProjectPackageMetadata:
'The git package "{}" \n\tis {}.\n\tThis can introduce '
'breaking changes into your project without warning!\n\nSee {}'
.format(self.git, self.unpinned_msg(), PIN_PACKAGE_URL),
log_fmt=ui.yellow('WARNING: {}')
log_fmt=ui.yellow(warning_tag('{}'))
)
loaded = Project.from_project_root(path, renderer)
return ProjectPackageMetadata.from_project(loaded)
Expand Down
16 changes: 15 additions & 1 deletion core/dbt/events/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dbt.events.functions as this # don't worry I hate it too.
from dbt.events.base_types import Cli, Event, File, ShowException
import dbt.flags as flags
import dbt.ui as ui
# TODO this will need to move eventually
from dbt.logger import SECRET_ENV_PREFIX, make_log_dir_if_missing, GLOBAL_LOGGER
import io
Expand Down Expand Up @@ -110,14 +111,27 @@ def scrub_secrets(msg: str, secrets: List[str]) -> str:
T_Event = TypeVar('T_Event', bound=Event)


def format_level(level):
fixed_width = level.ljust(5)
# TODO turn off all color everwhere for file + JSON
if not this.format_color:
return fixed_width
elif level == 'warn':
return ui.yellow(fixed_width)
elif level == 'error':
return ui.red(fixed_width)
else:
return fixed_width


# translates an Event to a completely formatted text-based log line
# you have to specify which message you want. (i.e. - e.message, e.cli_msg(), e.file_msg())
# type hinting everything as strings so we don't get any unintentional string conversions via str()
def create_text_log_line(e: T_Event, msg_fn: Callable[[T_Event], str]) -> str:
color_tag: str = '' if this.format_color else Style.RESET_ALL
ts: str = e.ts.strftime("%H:%M:%S")
scrubbed_msg: str = scrub_secrets(msg_fn(e), env_secrets())
level: str = e.level_tag()
level: str = format_level(e.level_tag())
log_line: str = f"{color_tag}{ts} | [ {level} ] | {scrubbed_msg}"
return log_line

Expand Down
2 changes: 1 addition & 1 deletion core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def parse_patch(
macro = self.manifest.macros.get(unique_id)
if not macro:
warn_or_error(
f'WARNING: Found patch for macro "{patch.name}" '
f'Found patch for macro "{patch.name}" '
f'which was not found'
)
return
Expand Down
4 changes: 3 additions & 1 deletion core/dbt/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ def line_wrap_message(


def warning_tag(msg: str) -> str:
return f'[{yellow("WARNING")}]: {msg}'
# duplicative now that new structured logs include levels
# return f'[{yellow("WARNING")}]: {msg}'
return msg

0 comments on commit 6398157

Please sign in to comment.