Skip to content

Commit

Permalink
Merge branch 'release/v0.3.31'
Browse files Browse the repository at this point in the history
  • Loading branch information
ALERTua committed Jan 16, 2024
2 parents 932129a + d73b565 commit 723bec0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
History
=======

0.3.31 (2023-11-05)
-------------------

* Bugfix: Proper add handler to all handlers


0.3.30 (2022-18-06)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion global_logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

__author__ = """Alexey Rubasheff"""
__email__ = 'alexey.rubasheff@gmail.com'
__version__ = '0.3.30'
__version__ = '0.3.31'

from .global_logger import Log # noqa # pylint: disable=unused-import
15 changes: 7 additions & 8 deletions global_logger/global_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def get_prev_function_name():


def clear_message(msg):
# noinspection RegExpRedundantEscape
return re.sub(r'\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))', '', msg)


Expand All @@ -60,6 +61,7 @@ class Log(object):
# pylint: disable=protected-access
_LOGGER_LEVELS_DICT = logging._nameToLevel
# pylint: disable=unnecessary-comprehension
# noinspection PyTypeChecker
Levels = IntEnum('Levels', [(k, v) for k, v in _LOGGER_LEVELS_DICT.items()])
GLOBAL_LOG_LEVEL = Levels.INFO
LOGGER_FILE_MESSAGE_FORMAT = '%(asctime)s.%(msecs)03d %(lineno)3s:%(name)-22s %(levelname)-6s %(message)s'
Expand Down Expand Up @@ -129,15 +131,10 @@ def __init__(self, name, level=None, global_level=True, logs_dir=None, log_sessi
if Log.logs_dir:
Log.logs_dir = Path(Log.logs_dir)
if not (Log.logs_dir.exists() and Log.logs_dir.is_dir()):
Log.logs_dir.mkdir()
Log.logs_dir.mkdir(parents=True)

if Log.log_session_filename is None:
# pylint: disable=import-outside-toplevel
from pendulum.tz.zoneinfo.exceptions import InvalidZoneinfoFile
try:
now = pendulum.now()
except InvalidZoneinfoFile:
now = pendulum.now(pendulum.UTC) # travis-ci precaution
now = pendulum.now(tz=pendulum.local_timezone())
Log.log_session_filename = "%s.log" % now.strftime('%Y-%m-%d_%H-%M-%S')
self._clean_logs_folder()

Expand Down Expand Up @@ -264,7 +261,9 @@ def add_handler_to_all_loggers(handler):
if logger_name in Log.individual_loggers.keys():
continue

if handler not in logger.logger.handlers and handler.name not in [h.name for h in logger.logger.handlers]:
handler_name = handler.name or handler.__class__.__name__
handlers_names = [h.name or h.__class__.__name__ for h in logger.logger.handlers]
if handler not in logger.logger.handlers and handler_name not in handlers_names:
logger.logger.addHandler(handler)

@property
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.3.30
current_version = 0.3.31
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/alertua/global_logger',
version='0.3.30',
version='0.3.31',
zip_safe=False,
)
27 changes: 24 additions & 3 deletions tests/test_global_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
@pytest.fixture
def _logger():
def __logger(*args, **kwargs):
return Log.get_logger(*args, **kwargs)
___logger = Log.get_logger(*args, **kwargs)
return ___logger

yield __logger

Expand Down Expand Up @@ -120,8 +121,8 @@ def test_levels(logger, logger_screen):
# noinspection PyUnusedLocal
def test_handlers_quantity(_logger, logger_file):
logger1 = _logger(name='logger1') # noqa: F841
logger2 = logger_file # noqa: F841
logger3 = _logger(name='logger3') # noqa: F841
logger2 = _logger(name='logger2') # noqa: F841
logger3 = logger_file # noqa: F841
for logger in Log.loggers.values():
filehandlers = [handler for handler in logger.logger.handlers if isinstance(handler, logging.FileHandler)]
assert len(filehandlers) == 1
Expand Down Expand Up @@ -202,6 +203,26 @@ def check_logger_file_contents(logger, text):
assert check_logger_file_contents(logger_file, unique_string)


# noinspection PyUnusedLocal
def test_add_handler(_logger, logger_file):
class CustomHandler(logging.Handler):
pass

logger1 = _logger(name='logger1') # noqa: F841
logger2 = logger_file # noqa: F841
logger3 = _logger(name='logger3') # noqa: F841
handler = CustomHandler()
logger1.logger.addHandler(handler)
assert handler not in logger2.logger.handlers, "Handler shouldn't be in a logger where it wasn't added"

Log.add_handler_to_all_loggers(handler)
for logger in Log.loggers.values():
if logger in logger.individual_loggers.values():
continue

assert handler in logger.logger.handlers, "%s must be in %s %s" % (handler, logger, logger.logger.handlers)


if __name__ == '__main__':
__logger_file = logger('logger', logs_dir=Path(__file__).parent.parent / 'Logs')
test_basic(__logger_file)
Expand Down
3 changes: 0 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ doctests = True

[pytest]
addopts = -ra -q
testpaths =
{toxinidir}/tests

[testenv:pre-commit]
description = format the code
Expand Down Expand Up @@ -85,4 +83,3 @@ disable = logging-not-lazy,
missing-function-docstring,
missing-class-docstring,
too-few-public-methods,

0 comments on commit 723bec0

Please sign in to comment.