Skip to content
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

Try to silence dbt logging #5054 #5070

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions plugins/sqlfluff-templater-dbt/sqlfluff_templater_dbt/templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@ def dbt_version_tuple(self):
"""Gets the dbt version."""
return int(self._dbt_version.major), int(self._dbt_version.minor)

def try_silence_dbt_logs(self) -> None:
"""Attempt to silence dbt logs.

During normal operation dbt is likely to log output such as:

.. code-block::

14:13:10 Registered adapter: snowflake=1.6.0

This is emitted by dbt directly to stdout/stderr, and so for us
to silence it (e.g. when outputting to json or yaml) we need to
reach into the internals of dbt and silence it directly.

https:/sqlfluff/sqlfluff/issues/5054

NOTE: We wrap this in a try clause so that if the API changes
within dbt that we don't get a direct fail. This was tested on
dbt-code==1.6.0.
"""
# First check whether we need to silence the logs. If a formatter
# is present then assume that it's not a problem
if not self.formatter:
try:
from dbt.events.functions import cleanup_event_logger

cleanup_event_logger()
except ImportError:
pass

@cached_property
def dbt_config(self):
"""Loads the dbt config."""
Expand All @@ -121,6 +150,10 @@ def dbt_config(self):
from dbt.config.runtime import RuntimeConfig as DbtRuntimeConfig
from dbt.adapters.factory import register_adapter

# Attempt to silence internal logging at this point.
# https:/sqlfluff/sqlfluff/issues/5054
self.try_silence_dbt_logs()

if self.dbt_version_tuple >= (1, 5):
user_config = None
# 1.5.x+ this is a dict.
Expand Down