-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: add logger #14
Merged
Merged
feat: add logger #14
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -26,4 +26,5 @@ jobs: | |
|
||
- name: Run tests | ||
run: | | ||
export PYTHONPATH=`pwd` | ||
pytest |
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,28 @@ | ||
from unittest import TestCase | ||
from hm_pyhelper.utils.logger import get_logger, _log_format | ||
import re | ||
import logging | ||
|
||
|
||
class TestExample(TestCase): | ||
def test_logging(self): | ||
logger = get_logger(__name__) | ||
|
||
with self.assertLogs() as captured: | ||
logger.debug("Hello world.") | ||
|
||
# check that there is only one log message | ||
self.assertEqual(len(captured.records), 1) | ||
record = captured.records[0] | ||
formatter = logging.Formatter(_log_format) | ||
formatted_output = formatter.format(record) | ||
|
||
# Do not check timestamp and filepath because those change | ||
# based on the environment and run time | ||
expected_partial_output_regex = re.escape( | ||
" - [DEBUG] - test_logger - (test_logger.py).test_logging -- ") | ||
expected_output_regex = ".*" + \ | ||
expected_partial_output_regex + ".*" + \ | ||
" - Hello world." | ||
are_logs_correct = re.search(expected_output_regex, formatted_output) | ||
self.assertTrue(are_logs_correct) |
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,23 @@ | ||
import os | ||
import logging | ||
|
||
LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG") | ||
_log_format = f"%(asctime)s - [%(levelname)s] - %(name)s - (%(filename)s).%(funcName)s -- %(pathname)s:(%(lineno)d) - %(message)s" # noqa: F541 E501 | ||
|
||
|
||
def get_stream_handler(): | ||
stream_handler = logging.StreamHandler() | ||
stream_handler.setLevel(LOGLEVEL) | ||
stream_handler.setFormatter(logging.Formatter(_log_format)) | ||
return stream_handler | ||
|
||
|
||
def get_logger(name): | ||
logger = logging.getLogger(name) | ||
logger.setLevel(LOGLEVEL) | ||
logger.addHandler(get_stream_handler()) | ||
return logger | ||
|
||
|
||
def log(class_name): | ||
return get_logger(class_name) |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
setup( | ||
name='hm_pyhelper', | ||
version='0.6.1', | ||
version='0.6.3', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This had to be bumped twice because of this: #20 |
||
author="Nebra Ltd", | ||
author_email="[email protected]", | ||
description="Helium Python Helper", | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think the explicit
PYTHONPATH
is required here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure if this is a best practice but with the code as-is now, you'll get an error if just trying to
pytest
. This type of thing is a problem whenever we have the test code in a separate directory/module. In other repos, we get around it by modifying the path from the test code, which I think is worse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pwd
should be in PYTHONPATH, but this is not a big deal.