-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Improve logging #792
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
Improve logging #792
Changes from all commits
1ccdbae
3274157
201f333
087aaf3
2fe1e28
8df7a21
2fd2999
e2576ba
8c1408d
228a4d4
b99c675
bd92ab3
5a0ba1a
8000
15d7c3f
405cd47
9d10b74
c23e92b
a693f27
a42528a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import logging | ||
import sys | ||
|
||
LOG_LEVELS = { | ||
'DEBUG': logging.DEBUG, | ||
'INFO': logging.INFO, | ||
'WARNING': logging.WARNING, | ||
'ERROR': logging.ERROR, | ||
'CRITICAL': logging.CRITICAL, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on using https://docs.python.org/3/library/logging.html#logging.getLevelName instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works both ways:
Probably best to leave your code as is though.
|
||
|
||
LOG_FORMATS = { | ||
'DEBUG': u'%(levelname)s %(name)s: %(message)s', | ||
'INFO': u'%(levelname)s: %(message)s', | ||
} | ||
|
||
|
||
def configure_logger(stream_level='DEBUG', debug_file=None): | ||
# Set up 'cookiecutter' logger | ||
logger = logging.getLogger('cookiecutter') | ||
logger.setLevel(logging.DEBUG) | ||
|
||
# Remove all attached handlers, in case there was | ||
# a logger with using the name 'cookiecutter' | ||
del logger.handlers[:] | ||
|
||
# Create a file handler if a log file is provided | ||
if debug_file is not None: | ||
debug_formatter = logging.Formatter(LOG_FORMATS['DEBUG']) | ||
file_handler = logging.FileHandler(debug_file) | ||
file_handler.setLevel(LOG_LEVELS['DEBUG']) | ||
file_handler.setFormatter(debug_formatter) | ||
logger.addHandler(file_handler) | ||
|
||
# Get settings based on the given stream_level | ||
log_formatter = logging.Formatter(LOG_FORMATS[stream_level]) | ||
log_level = LOG_LEVELS[stream_level] | ||
|
||
# Create a stream handler | ||
stream_handler = logging.StreamHandler(stream=sys.stdout) | ||
stream_handler.setLevel(log_level) | ||
stream_handler.setFormatter(log_formatter) | ||
logger.addHandler(stream_handler) | ||
|
||
return logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move to single quotes here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't
😿