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

add log-format flag (#1237) #1791

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
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
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', None],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it would be clearer to use default instead of None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters much, but we can!

default=None,
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 == 'text' or args.log_format is None:
log_manager.format_text()
else:
log_manager.format_json()

@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 @@ -94,6 +94,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 @@ -106,6 +108,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 @@ -144,8 +154,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 @@ -154,7 +163,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 @@ -182,6 +190,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