Skip to content

Commit

Permalink
Merge pull request #1791 from fishtown-analytics/feature/log-format-p…
Browse files Browse the repository at this point in the history
…arameter

add log-format flag (#1237)
  • Loading branch information
beckjake authored Oct 2, 2019
2 parents 1fbd82d + 626e642 commit 3765435
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/dbt/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def __init__(
logbook.Handler.__init__(self, level, filter, bubble)
if log_dir is not None:
self.set_path(log_dir)
self._text_format_string = None

def reset(self):
if self.initialized:
Expand Down
9 changes: 8 additions & 1 deletion core/dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def run_from_args(parsed):
log_cache_events(getattr(parsed, 'log_cache_events', False))
flags.set_from_args(parsed)

parsed.cls.pre_init_hook()
parsed.cls.pre_init_hook(parsed)
# we can now use the logger for stdout

logger.info("Running with dbt{}".format(dbt.version.installed))
Expand Down Expand Up @@ -740,6 +740,13 @@ def parse_args(args):
'''
)

p.add_argument(
'--log-format',
choices=['text', 'json', 'default'],
default='default',
help='''Specify the log format, overriding the command's default.'''
)

p.add_argument(
'--no-write-json',
action='store_false',
Expand Down
8 changes: 6 additions & 2 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dbt.config import RuntimeConfig, Project
from dbt.config.profile import read_profile, PROFILES_DIR
from dbt import tracking
from dbt.logger import GLOBAL_LOGGER as logger
from dbt.logger import GLOBAL_LOGGER as logger, log_manager
import dbt.exceptions


Expand Down Expand Up @@ -45,8 +45,12 @@ def __init__(self, args, config):
self.config = config

@classmethod
def pre_init_hook(cls):
def pre_init_hook(cls, args):
"""A hook called before the task is initialized."""
if args.log_format == 'json':
log_manager.format_json()
else:
log_manager.format_text()

@classmethod
def from_args(cls, args):
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ def __init__(self, args, config):
)

@classmethod
def pre_init_hook(cls):
def pre_init_hook(cls, args):
"""A hook called before the task is initialized."""
log_manager.stderr_console()
super().pre_init_hook(args)

def _iterate_selected_nodes(self):
nodes = sorted(self.select_nodes())
Expand Down
18 changes: 15 additions & 3 deletions core/dbt/task/rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def signhup_replace():


class RPCServerTask(ConfiguredTask):
DEFAULT_LOG_FORMAT = 'json'

def __init__(self, args, config, tasks=None):
if os.name == 'nt':
raise RuntimeException(
Expand All @@ -107,6 +109,14 @@ def __init__(self, args, config, tasks=None):
self._reload_task_manager()
signal.signal(signal.SIGHUP, self._sighup_handler)

@classmethod
def pre_init_hook(cls, args):
"""A hook called before the task is initialized."""
if args.log_format == 'text':
log_manager.format_text()
else:
log_manager.format_json()

def _reload_task_manager(self):
"""This function can only be running once at a time, as it runs in the
signal handler we replace
Expand Down Expand Up @@ -145,8 +155,7 @@ def _default_tasks():
def single_threaded(self):
return SINGLE_THREADED_WEBSERVER or self.args.single_threaded

def run(self):
log_manager.format_json()
def run_forever(self):
host = self.args.host
port = self.args.port
addr = (host, port)
Expand All @@ -155,7 +164,6 @@ def run(self):
if host == '0.0.0.0':
display_host = 'localhost'

ServerContext().push_application()
logger.info(
'Serving RPC server at {}:{}, pid={}'.format(
*addr, os.getpid()
Expand Down Expand Up @@ -183,6 +191,10 @@ def run(self):
# fast.
run_simple(host, port, app, threaded=not self.single_threaded)

def run(self):
with ServerContext().applicationbound():
self.run_forever()

@Request.application
def handle_jsonrpc_request(self, request):
with HTTPRequest(request):
Expand Down

0 comments on commit 3765435

Please sign in to comment.