From d320779ef039c2f54250b7d57555081528bf72bb Mon Sep 17 00:00:00 2001 From: Kamil Rybacki Date: Thu, 11 Jul 2024 17:27:53 +0200 Subject: [PATCH] feat: Add logging feature for custom validators --- docs/logging.md | 29 +++++++++++++++++++++++++++++ docs/main-features.md | 8 ++++++-- mkdocs.yml | 2 ++ phaistos/consts.py | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 docs/logging.md diff --git a/docs/logging.md b/docs/logging.md new file mode 100644 index 0000000..097abe3 --- /dev/null +++ b/docs/logging.md @@ -0,0 +1,29 @@ +## Few words about logging + +There is a total of 4 logger included in the Phaistos package: + + + +1. **PHAISTOS (T)**: This logger is used by the `Transpiler` class to log messages related to the transpilation process. +2. **PHAISTOS (M)**: This logger is used by the `Manager` class to log messages related to the schema management. +3. **PHAISTOS (C)**: This logger is used by the `Compiler` class to log messages related to the compilation process. +4. **PHAISTOS (V)**: This logger is used by the `Manager` class to log messages related to the validation process i.e. things that happen **inside** custom validators. + +The logging level for all the loggers is set to `INFO` by default. + +Do You want to create a custom logger for Phaistos? You can do it by using the `phaistos.utils.setup_logger` function: + +```python +from phaistos import utils + +logger = utils.setup_logger('MyLogger', level='DEBUG') +``` + +This will create a logger named `MyLogger` with the logging level set to `DEBUG`. You can use this logger to log messages in your custom code. + +**phaistos.utils.setup_logger** + +::: phaistos.utils.setup_logger diff --git a/docs/main-features.md b/docs/main-features.md index 74a5c8d..bee7166 100644 --- a/docs/main-features.md +++ b/docs/main-features.md @@ -77,9 +77,10 @@ classes in Python (which is a whole different can of worms), we can quickly install Phaistos in the project and start using it to validate data against the defined schemas - which can be leveraged in distributed ETL pipelines (e.g. within Apache Spark workers), APIs, configurations, and more. -#### Minor nitpicks 🤏 +#### Minor nitpicks 🤏 and quality-of-life boons -Below is the section that lists little annoyances that Phaistos tries to solve: +Below is the section that lists little annoyances that Phaistos tries to solve +or things that may be considered as quality-of-life improvements. 1. Normally, Pydantic will **not run** field validators for any attributes that were used in the previously failed model validator. So, this is slightly @@ -91,3 +92,6 @@ abstract names, Phaistos allows you to reference them by their actual names. So, in the custom validator, you can use `name` instead of `value` for a field that is named ... `name`! This makes the code more readable and maintainable. +3. You can use logging via the `logger` variable inside **all** custom +validators, which hooks up into the global validation logger. It is +included by default in the transpiled code. diff --git a/mkdocs.yml b/mkdocs.yml index a4d098e..05ece83 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -24,6 +24,8 @@ nav: - Managing schemas: - Using validation mechanisms: manager.md - Skipping the autodiscovery: noauto.md + - Other features: + - Logging: logging.md markdown_extensions: - pymdownx.superfences: custom_fences: diff --git a/phaistos/consts.py b/phaistos/consts.py index 023a0b9..3da8615 100644 --- a/phaistos/consts.py +++ b/phaistos/consts.py @@ -6,6 +6,7 @@ TRANSPILATION_LOGGER = phaistos.utils.setup_logger('PHAISTOS (T)') MANAGER_LOGGER = phaistos.utils.setup_logger('PHAISTOS (M)') COMPILATION_LOGGER = phaistos.utils.setup_logger('PHAISTOS (C)') +VALIDATION_LOGGER = phaistos.utils.setup_logger('PHAISTOS (V)') ALLOWED_COLLECTION_TYPES = {'list', 'set'}