Skip to content

Commit

Permalink
feat: Add logging feature for custom validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Rybacki committed Jul 11, 2024
1 parent 9a0ff51 commit d320779
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
29 changes: 29 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Few words about logging

There is a total of 4 logger included in the Phaistos package:

<!-- 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)') -->

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
8 changes: 6 additions & 2 deletions docs/main-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions phaistos/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

Expand Down

0 comments on commit d320779

Please sign in to comment.