-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(optional_logger): new optional logger class
- Loading branch information
Showing
5 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from mloggers.logger import Logger | ||
|
||
|
||
class OptionalLogger(Logger): | ||
""" | ||
A wrapper for a logger which can be `None`. | ||
This object can be useful library-side to not force your users to use `mloggers`. | ||
The benefit of this wrapper is that you never have to use `if logger is not None:` again! | ||
Example: | ||
```python | ||
from mloggers import Logger, OptionalLogger | ||
def some_function(logger: Logger | None): | ||
my_logger = OptionalLogger(logger) | ||
# If the logger is None, nothing will happen (not even an error) | ||
my_logger.info("This will only log if the logger is not None.") | ||
``` | ||
""" | ||
|
||
def __init__(self, logger: Logger | None = None): | ||
""" | ||
Initialize the OptionalLogger. | ||
### Parameters | ||
- [optional] `logger`: the logger to wrap. | ||
""" | ||
|
||
# Hopefully this name is unique enough | ||
self.__internal_wrapped_logger__ = logger | ||
|
||
def log(self, _): | ||
pass | ||
|
||
def __getattribute__(self, attr: str): | ||
"""Re-route all attribute access to the logger if it exists.""" | ||
|
||
if attr == "__internal_wrapped_logger__": | ||
return object.__getattribute__(self, attr) | ||
|
||
try: | ||
return getattr(self.__internal_wrapped_logger__, attr) | ||
except AttributeError: | ||
return lambda *_: None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build" | |
|
||
[project] | ||
name = "mloggers" | ||
version = "1.2.0" | ||
version = "1.2.1" | ||
authors = [ | ||
{ name = "Sergio Hernandez Gutierrez", email = "[email protected]" }, | ||
{ name = "Matteo Merler", email = "[email protected]" }, | ||
|