Last active
August 29, 2015 14:14
-
-
Save jorge-lavin/e0284f7887db38a4ffbd to your computer and use it in GitHub Desktop.
A simple debug stdout logger for python2 and 3
This file contains hidden or 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
import logging | |
def setup_logging(name=__name__, level=logging.DEBUG, formatter=logging.Formatter('%(asctime)s %(levelname)s : %(message)s - (%(module)s, line %(lineno)s)')): | |
""" | |
Setups a logger with a `StreamHandler` that prints to `stdout` with `debug` logging | |
level and the following formatter | |
%(asctime)s %(levelname)s : %(message)s - (%(module)s, line %(lineno)s') | |
@return logger: The previously formatted logger | |
@rtype: logging.Logger | |
""" | |
# First the logger | |
logger = logging.getLogger(name) | |
logger.setLevel(level) | |
# Then the handler | |
stdout_handler = logging.StreamHandler() | |
stdout_handler.setLevel(level) | |
stdout_handler.setFormatter(formatter) | |
# We add the handler to the logger | |
logger.addHandler(stdout_handler) | |
return logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment