-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/logging system #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yammesicka
merged 21 commits into
PythonFreeCourse:develop
from
Liad-n:feature/logging-system
Jan 27, 2021
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b9d44a9
first logging commit
87ff7bc
took main from develop logging commit
0299b10
Added docstring and annotations to logger, added logger tests
1408056
Added docstring and annotations to logger, added logger tests
3cd2e37
Removed merge conflicts
c644bc0
Fixed an issue with variable name
ea9c0f4
Fixed requirements.txt duplicate rows.
c1e0221
Fixed requirements.txt duplicate rows, again.
5f14fc6
Fixed merge suggestions in logger_customizer.py
41febf5
Fixed merge suggestions in logger_customizer.py
c07f18d
Fixed linting in test_logging, conftest and logger_customizer, still …
973b4fc
Took config from global config file,
8c07e19
Fixed tests, created new clientless logger fixture. Another fixture l…
551936d
Updated conftest and separated logger fixtures to new file, fix mergi…
310bd58
Fix requirements for merging from develop
e71328d
Finished logger_fixture, added logging config file to tests, added lo…
88713de
Added logger_fixture and config which werent added for some reason
91322d9
Changed logging config to be received by separate parameters for simp…
bca9c7c
removed logging_config file which is no longer needed
26f0a40
Fixing merge conflicts
1a7fd84
Fixing merge conflicts - missing whitespace on config.py.example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -138,7 +138,6 @@ dmypy.json | |
# Pyre type checker | ||
.pyre/ | ||
|
||
|
||
# VScode | ||
.vscode/ | ||
app/.vscode/ | ||
|
This file contains hidden or 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 hidden or 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,94 @@ | ||
import sys | ||
|
||
from pathlib import Path | ||
from loguru import logger, _Logger as Logger | ||
|
||
|
||
class LoggerConfigError(Exception): | ||
pass | ||
|
||
|
||
class LoggerCustomizer: | ||
|
||
Liad-n marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@classmethod | ||
def make_logger(cls, log_path: Path, | ||
log_filename: str, | ||
log_level: str, | ||
log_rotation_interval: str, | ||
log_retention_interval: str, | ||
log_format: str) -> Logger: | ||
"""Creates a logger from given configurations | ||
|
||
Args: | ||
log_path (Path): Path where the log file is located | ||
log_filename (str): | ||
|
||
log_level (str): The level we want to start logging from | ||
log_rotation_interval (str): Every how long the logs | ||
would be rotated | ||
log_retention_interval (str): Amount of time in words defining | ||
how long the log will be kept | ||
log_format (str): The logging format | ||
|
||
Raises: | ||
LoggerConfigError: Error raised when the configuration is invalid | ||
|
||
Returns: | ||
Logger: Loguru logger instance | ||
""" | ||
try: | ||
logger = cls.customize_logging( | ||
file_path=Path(log_path) / Path(log_filename), | ||
level=log_level, | ||
retention=log_retention_interval, | ||
rotation=log_rotation_interval, | ||
format=log_format | ||
) | ||
except (TypeError, ValueError) as err: | ||
raise LoggerConfigError( | ||
f"You have an issue with the logger configuration: {err!r}, " | ||
"fix it please") | ||
|
||
return logger | ||
|
||
@classmethod | ||
def customize_logging(cls, | ||
file_path: Path, | ||
level: str, | ||
rotation: str, | ||
retention: str, | ||
format: str | ||
) -> Logger: | ||
"""Used to customize the logger instance | ||
|
||
Args: | ||
file_path (Path): Path where the log file is located | ||
level (str): The level wanted to start logging from | ||
rotation (str): Every how long the logs would be | ||
rotated(creation of new file) | ||
retention (str): Amount of time in words defining how | ||
long a log is kept | ||
format (str): The logging format | ||
|
||
Returns: | ||
Logger: Instance of a logger mechanism | ||
""" | ||
logger.remove() | ||
logger.add( | ||
sys.stdout, | ||
enqueue=True, | ||
backtrace=True, | ||
level=level.upper(), | ||
format=format | ||
) | ||
logger.add( | ||
str(file_path), | ||
rotation=rotation, | ||
retention=retention, | ||
enqueue=True, | ||
backtrace=True, | ||
level=level.upper(), | ||
format=format | ||
) | ||
|
||
return logger |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
import logging | ||
|
||
import pytest | ||
from _pytest.logging import caplog as _caplog # noqa: F401 | ||
from loguru import logger | ||
|
||
from app import config | ||
from app.internal.logger_customizer import LoggerCustomizer | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def logger_instance(): | ||
_logger = LoggerCustomizer.make_logger(config.LOG_PATH, | ||
config.LOG_FILENAME, | ||
config.LOG_LEVEL, | ||
config.LOG_ROTATION_INTERVAL, | ||
config.LOG_RETENTION_INTERVAL, | ||
config.LOG_FORMAT) | ||
|
||
return _logger | ||
|
||
|
||
@pytest.fixture | ||
def caplog(_caplog): # noqa: F811 | ||
class PropagateHandler(logging.Handler): | ||
def emit(self, record): | ||
logging.getLogger(record.name).handle(record) | ||
|
||
handler_id = logger.add(PropagateHandler(), format="{message} {extra}") | ||
yield _caplog | ||
logger.remove(handler_id) |
This file contains hidden or 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,42 @@ | ||
import logging | ||
Liad-n marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import pytest | ||
|
||
from app.internal.logger_customizer import LoggerCustomizer, LoggerConfigError | ||
from app import config | ||
|
||
|
||
class TestLogger: | ||
@staticmethod | ||
def test_log_debug(caplog, logger_instance): | ||
with caplog.at_level(logging.DEBUG): | ||
logger_instance.debug('Is it debugging now?') | ||
assert 'Is it debugging now?' in caplog.text | ||
|
||
@staticmethod | ||
def test_log_info(caplog, logger_instance): | ||
with caplog.at_level(logging.INFO): | ||
logger_instance.info('App started') | ||
assert 'App started' in caplog.text | ||
|
||
@staticmethod | ||
def test_log_error(caplog, logger_instance): | ||
with caplog.at_level(logging.ERROR): | ||
logger_instance.error('Something bad happened!') | ||
assert 'Something bad happened!' in caplog.text | ||
|
||
@staticmethod | ||
def test_log_critical(caplog, logger_instance): | ||
with caplog.at_level(logging.CRITICAL): | ||
logger_instance.critical("WE'RE DOOMED!") | ||
assert "WE'RE DOOMED!" in caplog.text | ||
|
||
@staticmethod | ||
def test_bad_configuration(): | ||
with pytest.raises(LoggerConfigError): | ||
LoggerCustomizer.make_logger(config.LOG_PATH, | ||
config.LOG_FILENAME, | ||
'eror', | ||
config.LOG_ROTATION_INTERVAL, | ||
config.LOG_RETENTION_INTERVAL, | ||
config.LOG_FORMAT) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.