diff --git a/.changes/unreleased/Features-20241020-004903.yaml b/.changes/unreleased/Features-20241020-004903.yaml new file mode 100644 index 0000000..4f0ed81 --- /dev/null +++ b/.changes/unreleased/Features-20241020-004903.yaml @@ -0,0 +1,6 @@ +kind: Features +body: enable json log for pretty-formatted +time: 2024-10-20T00:49:03.699651+09:00 +custom: + Author: jx2lee + Issue: "209" diff --git a/dbt_common/events/__init__.py b/dbt_common/events/__init__.py index 6ba789b..e1433dd 100644 --- a/dbt_common/events/__init__.py +++ b/dbt_common/events/__init__.py @@ -5,5 +5,5 @@ # make sure event manager starts with a logger get_event_manager().add_logger( - get_stdout_config(LineFormat.PlainText, True, EventLevel.INFO, False) + get_stdout_config(LineFormat.PlainText, True, EventLevel.INFO, False, False,) ) diff --git a/dbt_common/events/functions.py b/dbt_common/events/functions.py index 86d6823..be93dcc 100644 --- a/dbt_common/events/functions.py +++ b/dbt_common/events/functions.py @@ -41,6 +41,7 @@ def get_stdout_config( use_colors: bool, level: EventLevel, log_cache_events: bool, + pretty_print_json: bool = False, ) -> LoggerConfig: return LoggerConfig( name="stdout_log", @@ -55,6 +56,7 @@ def get_stdout_config( ), invocation_id=get_invocation_id(), output_stream=sys.stdout, + pretty_print_json=pretty_print_json, ) diff --git a/dbt_common/events/logger.py b/dbt_common/events/logger.py index 460a01d..bfa59c2 100644 --- a/dbt_common/events/logger.py +++ b/dbt_common/events/logger.py @@ -88,6 +88,7 @@ class LoggerConfig: output_file_name: Optional[str] = None output_file_max_bytes: Optional[int] = 10 * 1024 * 1024 # 10 mb logger: Optional[Any] = None + pretty_print_json: bool = False class _Logger: @@ -188,10 +189,17 @@ def _get_thread_name(self) -> str: class _JsonLogger(_Logger): + def __init__(self, config: LoggerConfig) -> None: + super().__init__(config) + self.pretty_print_json = config.pretty_print_json + def create_line(self, msg: EventMsg) -> str: from dbt_common.events.functions import msg_to_dict msg_dict = msg_to_dict(msg) - raw_log_line = json.dumps(msg_dict, sort_keys=True, cls=ForgivingJSONEncoder) + indent = 2 if self.pretty_print_json else None + raw_log_line = json.dumps( + msg_dict, sort_keys=True, cls=ForgivingJSONEncoder, indent=indent + ) line = self.scrubber(raw_log_line) # type: ignore return line