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 indent option in JsonLogger #210

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20241020-004903.yaml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion dbt_common/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
)
2 changes: 2 additions & 0 deletions dbt_common/events/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -55,6 +56,7 @@ def get_stdout_config(
),
invocation_id=get_invocation_id(),
output_stream=sys.stdout,
pretty_print_json=pretty_print_json,
)


Expand Down
10 changes: 9 additions & 1 deletion dbt_common/events/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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