-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise the logger instanciation/initial handlers (#135)
* Update logger to use NullHandler by default * Add test class to cover Logging behavior * ❇️ Having a dedicated final handler attached to the explain toggle * 🔧 Having the logger initialized in top-level init * ✔️ Add tests / enforce hdl checks Co-authored-by: Ahmed TAHRI <[email protected]>
- Loading branch information
Showing
6 changed files
with
119 additions
and
30 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
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,51 @@ | ||
import pytest | ||
import logging | ||
|
||
from charset_normalizer.utils import set_logging_handler | ||
from charset_normalizer.api import from_bytes, explain_handler | ||
|
||
|
||
class TestLogBehaviorClass: | ||
def setup(self): | ||
self.logger = logging.getLogger("charset_normalizer") | ||
self.logger.handlers.clear() | ||
self.logger.addHandler(logging.NullHandler()) | ||
self.logger.level = None | ||
|
||
def test_explain_true_behavior(self, caplog): | ||
test_sequence = b'This is a test sequence of bytes that should be sufficient' | ||
from_bytes(test_sequence, steps=1, chunk_size=50, explain=True) | ||
assert explain_handler not in self.logger.handlers | ||
for record in caplog.records: | ||
assert record.levelname == "INFO" | ||
|
||
def test_explain_false_handler_set_behavior(self, caplog): | ||
test_sequence = b'This is a test sequence of bytes that should be sufficient' | ||
set_logging_handler(level=logging.INFO, format_string="%(message)s") | ||
from_bytes(test_sequence, steps=1, chunk_size=50, explain=False) | ||
assert any(isinstance(hdl, logging.StreamHandler) for hdl in self.logger.handlers) | ||
for record in caplog.records: | ||
assert record.levelname == "INFO" | ||
assert "ascii is most likely the one. Stopping the process." in caplog.text | ||
|
||
def test_set_stream_handler(self, caplog): | ||
set_logging_handler( | ||
"charset_normalizer", level=logging.DEBUG | ||
) | ||
self.logger.debug("log content should log with default format") | ||
for record in caplog.records: | ||
assert record.levelname == "DEBUG" | ||
assert "log content should log with default format" in caplog.text | ||
|
||
def test_set_stream_handler_format(self, caplog): | ||
set_logging_handler( | ||
"charset_normalizer", format_string="%(message)s" | ||
) | ||
self.logger.info("log content should only be this message") | ||
assert caplog.record_tuples == [ | ||
( | ||
"charset_normalizer", | ||
logging.INFO, | ||
"log content should only be this message", | ||
) | ||
] |
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