From af9d3eb5027db1046ada484ac04588f9c20804ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 31 Aug 2023 23:09:50 +0000 Subject: [PATCH] control logging to stdout --- .vscode/launch.json | 1 + kiwi_simple_metrics/main.py | 7 +++++-- kiwi_simple_metrics/metrics/_report.py | 8 ++++++++ kiwi_simple_metrics/settings.py | 6 ++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 3f25227..cd3eae0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,7 @@ "module": "kiwi_simple_metrics.main", "env": { "METRIC__INTERVAL": "5", + "METRIC__QUIET": "False", "METRIC__DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]", }, "justMyCode": true diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 82a6bb7..a0fe787 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -10,11 +10,14 @@ async def run_metrics() -> None: while True: interval = asyncio.sleep(SETTINGS.interval) - print(metrics.Report.concat( + report = metrics.Report.concat( metrics.cpu(), metrics.memory(), metrics.disk(), - )) + ) + + if not SETTINGS.quiet: + print(report) await interval diff --git a/kiwi_simple_metrics/metrics/_report.py b/kiwi_simple_metrics/metrics/_report.py index c609f1b..c3067cc 100644 --- a/kiwi_simple_metrics/metrics/_report.py +++ b/kiwi_simple_metrics/metrics/_report.py @@ -34,6 +34,14 @@ class Report: result: str failed: bool = False + def __str__(self) -> str: + state = "OK" if not self.failed else "FAIL" + + return SETTINGS.report_stdout.format( + state=state, + result=self.result, + ) + @classmethod def concat(cls, *_reports: Any) -> Self: reports = [ diff --git a/kiwi_simple_metrics/settings.py b/kiwi_simple_metrics/settings.py index 26106c2..765cd06 100644 --- a/kiwi_simple_metrics/settings.py +++ b/kiwi_simple_metrics/settings.py @@ -91,6 +91,12 @@ class Settings(BaseSettings): # time between gathering reports interval: float = 600 + # if False, prints reports to stdout + quiet: bool = True + + # how to format reports to stdout + report_stdout: str = "[{state}] {result}" + # separates metrics and values in reports separator: str = ", "