Skip to content

Commit

Permalink
colorized dbt output (#441)
Browse files Browse the repository at this point in the history
colorize dbt output
  • Loading branch information
drewbanin authored May 23, 2017
1 parent 34d0e33 commit dfb24fd
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 218 deletions.
12 changes: 5 additions & 7 deletions dbt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ def read_profile(profiles_dir):

def read_config(profiles_dir):
profile = read_profile(profiles_dir)
return profile.get('config')
return profile.get('config', {})


def send_anonymous_usage_stats(profiles_dir):
config = read_config(profiles_dir)
def send_anonymous_usage_stats(config):
return config.get('send_anonymous_usage_stats', True)

if config is not None \
and not config.get("send_anonymous_usage_stats", True):
return False

return True
def colorize_output(config):
return config.get('use_colors', True)
36 changes: 35 additions & 1 deletion dbt/logger.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import dbt.clients.system
import dbt.compat
import logging
import os
import sys

import colorama

# disable logs from other modules, excepting CRITICAL logs
logging.getLogger('botocore').setLevel(logging.CRITICAL)
logging.getLogger('contracts').setLevel(logging.CRITICAL)
logging.getLogger('requests').setLevel(logging.CRITICAL)
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
logging.getLogger('snowflake.connector').setLevel(logging.CRITICAL)


# Colorama needs some help on windows because we're using logger.info
# intead of print(). If the Windows env doesn't have a TERM var set,
# then we should override the logging stream to use the colorama
# converter. If the TERM var is set (as with Git Bash), then it's safe
# to send escape characters and no log handler injection is needed.
colorama_stdout = sys.stdout
colorama_wrap = True

if sys.platform == 'win32' and not os.environ.get('TERM'):
colorama_wrap = False
colorama_stdout = colorama.AnsiToWin32(sys.stdout).stream

elif sys.platform == 'win32':
colorama_wrap = False

colorama.init(wrap=colorama_wrap)

# create a global console logger for dbt
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler = logging.StreamHandler(colorama_stdout)
stdout_handler.setFormatter(logging.Formatter('%(message)s'))
stdout_handler.setLevel(logging.INFO)

Expand All @@ -26,6 +47,16 @@ def make_log_dir_if_missing(log_dir):
dbt.clients.system.make_directory(log_dir)


class ColorFilter(logging.Filter):
def filter(self, record):
subbed = dbt.compat.to_string(record.msg)
for escape_sequence in dbt.ui.colors.COLORS.values():
subbed = subbed.replace(escape_sequence, '')
record.msg = subbed

return True


def initialize_logger(debug_mode=False, path=None):
global initialized, logger, stdout_handler

Expand All @@ -49,6 +80,9 @@ def initialize_logger(debug_mode=False, path=None):
backupCount=7,
)

color_filter = ColorFilter()
logdir_handler.addFilter(color_filter)

logdir_handler.setFormatter(
logging.Formatter('%(asctime)-18s: %(message)s'))
logdir_handler.setLevel(logging.DEBUG)
Expand Down
7 changes: 6 additions & 1 deletion dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dbt.tracking
import dbt.config as config
import dbt.adapters.cache as adapter_cache
import dbt.ui.printer


def main(args=None):
Expand All @@ -39,11 +40,15 @@ def handle(args):

# this needs to happen after args are parsed so we can determine the
# correct profiles.yml file
if not config.send_anonymous_usage_stats(parsed.profiles_dir):
profile_config = config.read_config(parsed.profiles_dir)
if not config.send_anonymous_usage_stats(profile_config):
dbt.tracking.do_not_track()
else:
dbt.tracking.initialize_tracking()

if dbt.config.colorize_output(profile_config):
dbt.ui.printer.use_colors()

res = run_from_args(parsed)
dbt.tracking.flush()

Expand Down
Loading

0 comments on commit dfb24fd

Please sign in to comment.