Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Last active August 29, 2015 14:14
Show Gist options
  • Save jorge-lavin/e0284f7887db38a4ffbd to your computer and use it in GitHub Desktop.
Save jorge-lavin/e0284f7887db38a4ffbd to your computer and use it in GitHub Desktop.
A simple debug stdout logger for python2 and 3
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