Created
February 17, 2015 13:48
-
-
Save nakatanakatana/e8b47f6f07d136679209 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
from logging import getLogger, StreamHandler, FileHandler | |
from logging import INFO, DEBUG, WARNING, ERROR, CRITICAL | |
from logging import Formatter | |
level = {'INFO': INFO, | |
'DEBUG': DEBUG, | |
'WARNING': WARNING, | |
'ERROR': ERROR, | |
'CRITICAL': CRITICAL, | |
} | |
handler = {'STREAMHANDLER': lambda x: (StreamHandler(), ), | |
'FILEHANDLER': lambda x: (FileHandler(x), ), | |
'FILEANDSTREAM': lambda x: (StreamHandler(), FileHandler(x)), | |
} | |
def logger(logname, | |
handlername='STREAMHANDLER', | |
logfilename=None, | |
loglevel='DEBUG', | |
format='%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'): | |
log = getLogger(logname) | |
l = level.get(loglevel.upper()) | |
log.setLevel(l) | |
logfilename = logfilename if logfilename else '%s.log' % logname | |
handlers = handler.get(handlername.upper())(logfilename) | |
for h in handlers: | |
f = Formatter(format) | |
h.setFormatter(f) | |
h.setLevel(l) | |
log.addHandler(h) | |
return log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment