-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove `tests` logger in `pyzzy.logs.vars.DEFAULT_CONFIG` - Remove `tests/_test_logger.py`
- Loading branch information
krakozaure
committed
Aug 16, 2018
1 parent
d141b25
commit 1bfab77
Showing
5 changed files
with
69 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = "0.0.4" | ||
__version__ = "0.0.5" | ||
__author__ = "krakozaure" | ||
__license__ = "MIT" |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,51 +1,84 @@ | ||
import contextlib | ||
import io | ||
import logging | ||
import time | ||
|
||
import _pytest.logging | ||
import pytest | ||
|
||
import pyzzy as pz | ||
|
||
|
||
def test_init_logging(): | ||
pz.init_logging( | ||
config=pz.logs.vars.DEFAULT_CONFIG, | ||
capture_warnings=True, | ||
simple_warnings=True, | ||
raise_exceptions=True, | ||
) | ||
@contextlib.contextmanager | ||
def capture_handler_stream(logger, handler_name): | ||
handler = get_handler_by_name(logger.handlers, handler_name) | ||
old_stream = handler.stream | ||
new_stream = io.StringIO() | ||
handler.stream = new_stream | ||
try: | ||
yield new_stream | ||
finally: | ||
new_stream.close() | ||
handler.stream = old_stream | ||
|
||
|
||
def test_handler_console_production(caplog): | ||
caplog.clear() | ||
def get_handler_by_name(handlers, name): | ||
for handler in handlers: | ||
if handler.get_name() == name: | ||
return handler | ||
|
||
logger = logging.getLogger('production') | ||
logger.propagate = True | ||
|
||
handler = get_handler_by_name(logger.handlers, 'console_production') | ||
formatter = handler.formatter | ||
@pytest.fixture() | ||
def logging_config(): | ||
logging.config.dictConfig(pz.logs.DEFAULT_CONFIG) | ||
return pz.logs.DEFAULT_CONFIG | ||
|
||
log_fmt = formatter._fmt | ||
log_msg = "Log message" | ||
log_tags = pz.logs.vars._colored_tags | ||
|
||
all_levels = logging._levelToName.keys() | ||
test_levels = [lvl for lvl in all_levels if lvl >= handler.level] | ||
expected_levels = logging.CRITICAL, logging.ERROR, logging.WARNING | ||
def test_root_handlers_names(logging_config): | ||
logger = logging.getLogger() | ||
root_handlers_names = [ | ||
h.get_name() for h in logger.handlers | ||
if not isinstance(h, _pytest.logging.LogCaptureHandler) | ||
] | ||
assert root_handlers_names == ['console_production', 'tr_file'] | ||
|
||
for level_number in sorted(test_levels): | ||
logger.log(level_number, log_msg) | ||
|
||
result = [ | ||
formatter.format(record) | ||
for record in caplog.records | ||
] | ||
def test_root_console_handler_output(logging_config): | ||
logger = logging.getLogger() | ||
message = "Log message" | ||
|
||
expected = [ | ||
log_fmt % {'message': log_msg, 'levelname': log_tags[level]} | ||
for level in sorted(expected_levels) | ||
] | ||
with capture_handler_stream(logger, "console_production") as stream: | ||
logger.error(message) | ||
logger.debug(message) | ||
captured_output = stream.getvalue() | ||
|
||
assert result == expected | ||
assert message in captured_output | ||
assert pz.logs.vars._colored_tags[logging.ERROR] in captured_output | ||
assert pz.logs.vars._colored_tags[logging.DEBUG] not in captured_output | ||
|
||
|
||
def get_handler_by_name(handlers, name): | ||
for handler in handlers: | ||
if handler.get_name() == name: | ||
return handler | ||
def test_root_trfile_handler_output(logging_config): | ||
logger = logging.getLogger() | ||
message = "Log message" | ||
|
||
with capture_handler_stream(logger, "tr_file") as stream: | ||
logger.error(message) | ||
logger.debug(message) | ||
captured_output = stream.getvalue() | ||
|
||
assert message in captured_output | ||
assert logging._levelToName[logging.ERROR] in captured_output | ||
assert logging._levelToName[logging.DEBUG] in captured_output | ||
assert time.strftime('%Y-%m-%d %H:%M') in captured_output | ||
assert logger.name in captured_output | ||
|
||
|
||
def main(): | ||
logging_config_ = logging_config() | ||
test_root_handlers_names(logging_config_) | ||
test_root_console_handler_output(logging_config_) | ||
test_root_trfile_handler_output(logging_config_) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |