From 243b10bb33ae0f233329e31237d872a173747960 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Sun, 11 Aug 2019 00:07:58 +0300 Subject: [PATCH 1/2] logger creation removed from __init__.py Fixes #11 --- handout/__init__.py | 12 ------------ handout/handout.py | 3 ++- handout/tests/test_tools.py | 7 +++++++ handout/tools.py | 17 +++++++++++++++++ 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 handout/tests/test_tools.py diff --git a/handout/__init__.py b/handout/__init__.py index de017c7..7498bcf 100644 --- a/handout/__init__.py +++ b/handout/__init__.py @@ -1,13 +1 @@ -import sys -import logging - from .handout import Handout - - -# Set up logger. -logger = logging.getLogger('handout') -logger.setLevel(logging.INFO) -logger.propagate = False # Global logger should not print messages again. -handler = logging.StreamHandler(sys.stdout) -handler.setFormatter(logging.Formatter('%(message)s')) -logger.addHandler(handler) diff --git a/handout/handout.py b/handout/handout.py index 4f9b087..8f88c95 100644 --- a/handout/handout.py +++ b/handout/handout.py @@ -6,6 +6,7 @@ import shutil from handout import blocks +from handout import tools class Handout(object): @@ -16,7 +17,7 @@ def __init__(self, directory, title='Handout'): self._title = title self._blocks = collections.defaultdict(list) self._pending = [] - self._logger = logging.getLogger('handout') + self._logger = tools.get_logger() for info in inspect.stack(): if info.filename == __file__: continue diff --git a/handout/tests/test_tools.py b/handout/tests/test_tools.py new file mode 100644 index 0000000..1382ca3 --- /dev/null +++ b/handout/tests/test_tools.py @@ -0,0 +1,7 @@ +import handout + +def test_get_logger_has_single_handler(): + import handout # Simulate multiple imports of a package. + import handout + logger = handout.tools.get_logger() + assert len(logger.handlers) == 1 \ No newline at end of file diff --git a/handout/tools.py b/handout/tools.py index fedc630..7643ef1 100644 --- a/handout/tools.py +++ b/handout/tools.py @@ -1,3 +1,6 @@ +import sys +import logging + def strip_empty_lines(lines): output = [] for line in lines: @@ -12,3 +15,17 @@ def strip_empty_lines(lines): output.append(line) lines = reversed(output) return list(lines) + + +def get_logger(): + logger = logging.getLogger('handout') + logger.setLevel(logging.INFO) # A user can change this level later. + logger.propagate = False # Global logger should not print messages again. + if not logger.handlers: # Prevents creation of multiple handlers. + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(logging.Formatter('%(message)s')) + logger.addHandler(handler) + # The logger can be accessed anywhere in code by its name with + # `logging.getLogger('handout')`. However, we retrun the logger + # explcitly here as function result, for convenience of use. + return logger From df392e56ebf9da27489906033ad47ab64b2c8c82 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Sun, 11 Aug 2019 02:18:20 +0300 Subject: [PATCH 2/2] logger configured at module import Added logger.py --- handout/__init__.py | 4 ++++ handout/handout.py | 4 ++-- handout/logger.py | 17 +++++++++++++++++ handout/tests/{test_tools.py => test_logger.py} | 2 +- handout/tools.py | 17 ----------------- 5 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 handout/logger.py rename handout/tests/{test_tools.py => test_logger.py} (81%) diff --git a/handout/__init__.py b/handout/__init__.py index 7498bcf..8c68b8f 100644 --- a/handout/__init__.py +++ b/handout/__init__.py @@ -1 +1,5 @@ from .handout import Handout + +from .logger import configure_logger + +configure_logger() diff --git a/handout/handout.py b/handout/handout.py index 8f88c95..83dd8c2 100644 --- a/handout/handout.py +++ b/handout/handout.py @@ -6,7 +6,7 @@ import shutil from handout import blocks -from handout import tools +from handout import logger class Handout(object): @@ -17,7 +17,7 @@ def __init__(self, directory, title='Handout'): self._title = title self._blocks = collections.defaultdict(list) self._pending = [] - self._logger = tools.get_logger() + self._logger = logger.get_logger() for info in inspect.stack(): if info.filename == __file__: continue diff --git a/handout/logger.py b/handout/logger.py new file mode 100644 index 0000000..bc32d1d --- /dev/null +++ b/handout/logger.py @@ -0,0 +1,17 @@ +import sys +import logging + +LOGGER_NAME = 'handout' + +def configure_logger(): + logger = logging.getLogger(LOGGER_NAME) + logger.setLevel(logging.INFO) # A user can change this level later. + logger.propagate = False # Global logger should not print messages again. + if not logger.handlers: # Prevents creation of multiple handlers. + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(logging.Formatter('%(message)s')) + logger.addHandler(handler) + + +def get_logger(): + return logging.getLogger(LOGGER_NAME) diff --git a/handout/tests/test_tools.py b/handout/tests/test_logger.py similarity index 81% rename from handout/tests/test_tools.py rename to handout/tests/test_logger.py index 1382ca3..d2ce74e 100644 --- a/handout/tests/test_tools.py +++ b/handout/tests/test_logger.py @@ -3,5 +3,5 @@ def test_get_logger_has_single_handler(): import handout # Simulate multiple imports of a package. import handout - logger = handout.tools.get_logger() + logger = handout.logger.get_logger() assert len(logger.handlers) == 1 \ No newline at end of file diff --git a/handout/tools.py b/handout/tools.py index 7643ef1..fedc630 100644 --- a/handout/tools.py +++ b/handout/tools.py @@ -1,6 +1,3 @@ -import sys -import logging - def strip_empty_lines(lines): output = [] for line in lines: @@ -15,17 +12,3 @@ def strip_empty_lines(lines): output.append(line) lines = reversed(output) return list(lines) - - -def get_logger(): - logger = logging.getLogger('handout') - logger.setLevel(logging.INFO) # A user can change this level later. - logger.propagate = False # Global logger should not print messages again. - if not logger.handlers: # Prevents creation of multiple handlers. - handler = logging.StreamHandler(sys.stdout) - handler.setFormatter(logging.Formatter('%(message)s')) - logger.addHandler(handler) - # The logger can be accessed anywhere in code by its name with - # `logging.getLogger('handout')`. However, we retrun the logger - # explcitly here as function result, for convenience of use. - return logger