Skip to content

Commit 1221509

Browse files
michrzanarbivharitamar
authored
feat: quiet-logs flag (#2072)
* feat: quiet-logs flag * fix: include quiet_logs in config. make it a flag * fix: initiate self.quiet_logs in the Config constructor --------- Co-authored-by: Yosef Arbiv <yosef@elementary-data.com> Co-authored-by: Itamar Hartstein <haritamar@gmail.com>
1 parent 885bc7c commit 1221509

5 files changed

Lines changed: 53 additions & 12 deletions

File tree

elementary/cli/cli.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def get_log_path(ctx):
2929
return os.path.join(target_path, "edr.log")
3030

3131

32+
def get_quiet_logs(ctx):
33+
try:
34+
return "--quiet-logs" in ctx.args
35+
except (ValueError, AttributeError):
36+
return False
37+
38+
3239
class ElementaryCLI(click.MultiCommand):
3340
_CMD_MAP = {
3441
"monitor": monitor,
@@ -57,12 +64,14 @@ def format_help(self, ctx, formatter):
5764

5865
def invoke(self, ctx: click.Context) -> Any:
5966
files_target_path = get_log_path(ctx)
60-
set_root_logger_handlers("elementary", files_target_path)
61-
click.echo(
62-
"Any feedback and suggestions are welcomed! join our community here - "
63-
"https://bit.ly/slack-elementary\n"
64-
)
65-
logger.info(f"Running with edr={package.get_package_version()}")
67+
quiet_logs = get_quiet_logs(ctx)
68+
set_root_logger_handlers("elementary", files_target_path, quiet_logs=quiet_logs)
69+
if not quiet_logs:
70+
click.echo(
71+
"Any feedback and suggestions are welcomed! join our community here - "
72+
"https://bit.ly/slack-elementary\n"
73+
)
74+
logger.info(f"Running with edr={package.get_package_version()}")
6675
return super().invoke(ctx)
6776

6877

elementary/config/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
env: str = DEFAULT_ENV,
7676
run_dbt_deps_if_needed: Optional[bool] = None,
7777
project_name: Optional[str] = None,
78+
quiet_logs: Optional[bool] = None,
7879
):
7980
self.config_dir = config_dir
8081
self.profiles_dir = profiles_dir
@@ -217,6 +218,10 @@ def __init__(
217218
"disable_elementary_logo_print", False
218219
)
219220

221+
self.quiet_logs = self._first_not_none(
222+
quiet_logs, config.get("quiet_logs"), False
223+
)
224+
220225
def _load_configuration(self) -> dict:
221226
if not os.path.exists(self.config_dir):
222227
os.makedirs(self.config_dir)

elementary/monitor/cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ def decorator(func):
149149
else "DEPRECATED! Please use --filters instead! - Filter the alerts by tags:<TAGS> / owners:<OWNERS> / models:<MODELS> / "
150150
"statuses:<warn/fail/error/skipped> / resource_types:<model/test>.",
151151
)(func)
152+
func = click.option(
153+
"--quiet-logs",
154+
is_flag=True,
155+
default=False,
156+
help="Minimize INFO level logs. Only WARNING and above will be shown.",
157+
)(func)
152158
return func
153159

154160
return decorator
@@ -324,12 +330,14 @@ def monitor(
324330
excludes,
325331
teams_webhook,
326332
maximum_columns_in_alert_samples,
333+
quiet_logs,
327334
):
328335
"""
329336
Get alerts on failures in dbt jobs.
330337
"""
331338
if ctx.invoked_subcommand is not None:
332339
return
340+
333341
if deprecated_slack_webhook is not None:
334342
click.secho(
335343
'\n"-s" is deprecated and won\'t be supported in the near future.\n'
@@ -356,6 +364,7 @@ def monitor(
356364
report_url=report_url,
357365
teams_webhook=teams_webhook,
358366
maximum_columns_in_alert_samples=maximum_columns_in_alert_samples,
367+
quiet_logs=quiet_logs,
359368
)
360369
anonymous_tracking = AnonymousCommandLineTracking(config)
361370
anonymous_tracking.set_env("use_select", bool(select))
@@ -450,6 +459,7 @@ def report(
450459
env,
451460
select,
452461
target_path,
462+
quiet_logs,
453463
):
454464
"""
455465
Generate a local observability report of your warehouse.
@@ -463,6 +473,7 @@ def report(
463473
target_path,
464474
dbt_quoting=dbt_quoting,
465475
env=env,
476+
quiet_logs=quiet_logs,
466477
)
467478
anonymous_tracking = AnonymousCommandLineTracking(config)
468479
anonymous_tracking.set_env("use_select", bool(select))
@@ -680,6 +691,7 @@ def send_report(
680691
disable,
681692
include,
682693
target_path,
694+
quiet_logs,
683695
):
684696
"""
685697
Generate and send the report to an external platform.
@@ -722,6 +734,7 @@ def send_report(
722734
report_url=report_url,
723735
env=env,
724736
project_name=project_name,
737+
quiet_logs=quiet_logs,
725738
)
726739
anonymous_tracking = AnonymousCommandLineTracking(config)
727740
anonymous_tracking.set_env("use_select", bool(select))

elementary/monitor/fetchers/alerts/alerts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
from typing import Dict, List, Optional
33

4+
import click
5+
46
from elementary.clients.dbt.base_dbt_runner import BaseDbtRunner
57
from elementary.clients.fetcher.fetcher import FetcherClient
68
from elementary.config.config import Config
@@ -29,7 +31,7 @@ def skip_alerts(
2931
):
3032
alert_ids = [alert.id for alert in alerts_to_skip]
3133
alert_ids_chunks = self._split_list_to_chunks(alert_ids)
32-
logger.info("Update skipped alerts")
34+
click.echo(f"Update skipped alerts ({len(alerts_to_skip)})")
3335
for alert_ids_chunk in alert_ids_chunks:
3436
self.dbt_runner.run(
3537
select="elementary_cli.update_alerts.update_skipped_alerts",
@@ -60,7 +62,7 @@ def query_last_alert_times(
6062

6163
def update_sent_alerts(self, alert_ids: List[str]) -> None:
6264
alert_ids_chunks = self._split_list_to_chunks(alert_ids)
63-
logger.info("Update sent alerts")
65+
click.echo(f"Update sent alerts ({len(alert_ids)})")
6466
for alert_ids_chunk in alert_ids_chunks:
6567
self.dbt_runner.run(
6668
select="elementary_cli.update_alerts.update_sent_alerts",

elementary/utils/log.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ def format(self, record):
3030
ROTATION_BACKUP_COUNT = 4
3131

3232

33-
def get_console_handler():
33+
def get_console_handler(quiet_logs: bool = False):
3434
console_handler = logging.StreamHandler(sys.stdout)
3535
console_handler.setFormatter(FORMATTER)
36-
console_handler.setLevel(logging.DEBUG if is_debug() else logging.INFO)
36+
if is_debug():
37+
console_handler.setLevel(logging.DEBUG)
38+
elif quiet_logs:
39+
console_handler.setLevel(logging.WARNING)
40+
else:
41+
console_handler.setLevel(logging.INFO)
3742
return console_handler
3843

3944

@@ -55,7 +60,14 @@ def get_logger(logger_name):
5560
return logger
5661

5762

58-
def set_root_logger_handlers(logger_name, files_target_path):
63+
def set_root_logger_handlers(logger_name, files_target_path, quiet_logs: bool = False):
5964
logger = logging.getLogger(logger_name)
60-
logger.addHandler(get_console_handler())
65+
66+
# Disable propagation to root logger to avoid duplicate logs
67+
logger.propagate = False
68+
69+
logger.addHandler(get_console_handler(quiet_logs=quiet_logs))
6170
logger.addHandler(get_file_handler(files_target_path))
71+
72+
# Set logger level to DEBUG so it doesn't filter messages (handler will filter)
73+
logger.setLevel(logging.DEBUG)

0 commit comments

Comments
 (0)