From 6bbd4a1bbd73eb000e89ac188f715c66f48b0e30 Mon Sep 17 00:00:00 2001 From: Arne Bahlo Date: Mon, 9 Sep 2024 12:00:59 +0200 Subject: [PATCH 1/2] docs: Add logger example --- README.md | 21 ++++++++++++++++++++- examples.py => examples/client.py | 0 examples/logger.py | 19 +++++++++++++++++++ src/axiom_py/logging.py | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) rename examples.py => examples/client.py (100%) create mode 100644 examples/logger.py diff --git a/README.md b/README.md index 8d6dfa7..91ae29c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,26 @@ client.ingest_events( client.query(r"['my-dataset'] | where foo == 'bar' | limit 100") ``` -for more examples, check out `examples.py`. +For more examples, see [`examples/client.py`](examples/client.py). + +## Logger + +You can use `axiom_py.logging.AxiomHandler` to send logs from the `logging` +module to Axiom: + +```python +import axiom_py +from axiom_py.logging import AxiomHandler +import logging + + +def setup_logger(): + client = axiom_py.Client() + handler = AxiomHandler(client, "my-dataset") + logging.getLogger().addHandler(handler) +``` + +For more examples, see [`examples/logger.py`](examples/logger.py). ## Contributing diff --git a/examples.py b/examples/client.py similarity index 100% rename from examples.py rename to examples/client.py diff --git a/examples/logger.py b/examples/logger.py new file mode 100644 index 0000000..5bf1edf --- /dev/null +++ b/examples/logger.py @@ -0,0 +1,19 @@ +import axiom_py +from axiom_py.logging import AxiomHandler +import logging + + +def main(): + # Add Axiom handler to root logger + client = axiom_py.Client() + handler = AxiomHandler(client, "my-dataset") + logging.getLogger().addHandler(handler) + + # Get logger and log something + logger = logging.getLogger(__name__) + logger.setLevel(logging.INFO) + logger.info("Hello world") + + +if __name__ == "__main__": + main() diff --git a/src/axiom_py/logging.py b/src/axiom_py/logging.py index ea76b7f..0ff62cc 100644 --- a/src/axiom_py/logging.py +++ b/src/axiom_py/logging.py @@ -17,7 +17,7 @@ class AxiomHandler(Handler): last_run: float def __init__(self, client: Client, dataset: str, level=NOTSET, interval=1): - Handler.__init__(self, level) + super().__init__() # set urllib3 logging level to warning, check: # https://github.com/axiomhq/axiom-py/issues/23 # This is a temp solution that would stop requests From 30d500a3a5d86136e94f7a4169f8c5ad8e14a554 Mon Sep 17 00:00:00 2001 From: Arne Bahlo Date: Mon, 9 Sep 2024 12:07:11 +0200 Subject: [PATCH 2/2] docs: Improve wording on logger section --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 91ae29c..01f0f11 100644 --- a/README.md +++ b/README.md @@ -79,8 +79,8 @@ For more examples, see [`examples/client.py`](examples/client.py). ## Logger -You can use `axiom_py.logging.AxiomHandler` to send logs from the `logging` -module to Axiom: +You can use the `AxiomHandler` to send logs from the `logging` module to Axiom +like this: ```python import axiom_py @@ -94,7 +94,7 @@ def setup_logger(): logging.getLogger().addHandler(handler) ``` -For more examples, see [`examples/logger.py`](examples/logger.py). +For a full example, see [`examples/logger.py`](examples/logger.py). ## Contributing