Skip to content

Instantly share code, notes, and snippets.

@reox
Created July 18, 2014 11:30
Show Gist options
  • Save reox/60cde7f2cc6429c8e9f9 to your computer and use it in GitHub Desktop.
Save reox/60cde7f2cc6429c8e9f9 to your computer and use it in GitHub Desktop.
Sample Logger - return a pretty straight forward and configured logger
import logging
import logging.handlers
def get_logger(name, logfile=""):
"""
Get a logger with a given name.
The logger will be configured to match other loggers.
Keyword arguments:
name -- name of the logger
logfile -- path of the logfile or "" if only stdout
"""
l = logging.getLogger(name)
l.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(name)s: %(message)s")
ch.setFormatter(formatter)
l.addHandler(ch)
if logfile != "":
handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10 * 1024 * 1024, backupCount=5)
l.addHandler(handler)
return l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment