Skip to content
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

logger creation removed from __init__.py #27

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions handout/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import sys
import logging

from .handout import Handout

from .logger import configure_logger

# 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)
configure_logger()
3 changes: 2 additions & 1 deletion handout/handout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil

from handout import blocks
from handout import logger


class Handout(object):
Expand All @@ -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 = logger.get_logger()
for info in inspect.stack():
if info.filename == __file__:
continue
Expand Down
17 changes: 17 additions & 0 deletions handout/logger.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions handout/tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -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.logger.get_logger()
assert len(logger.handlers) == 1