Created
July 18, 2014 11:30
-
-
Save reox/60cde7f2cc6429c8e9f9 to your computer and use it in GitHub Desktop.
Sample Logger - return a pretty straight forward and configured logger
This file contains 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 | |
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