Skip to content

Instantly share code, notes, and snippets.

@yamakk
Created October 4, 2011 11:05
Show Gist options
  • Save yamakk/1261358 to your computer and use it in GitHub Desktop.
Save yamakk/1261358 to your computer and use it in GitHub Desktop.
よく使うRotateLoggerの設定とtracebackの組み合わせ
#coding:utf-8
import logging
import logging.handlers
import traceback
"""よく使うRotateLoggerの設定とtracebackの設定"""
def getRotateLogger(path, name='', level=logging.DEBUG, **kws):
maxBytes = kws.get('maxBytes', 1024*1024*50)
backupCount=kws.get('backupCount', 5)
default_format = '%(asctime)s,%(msecs)03d %(levelname)-5.5s [%(threadName)s] [%(name)s line:%(lineno)d] [%(funcName)s] %(message)s'
format = kws.get('format', default_format)
datefmt = kws.get('datefmt', '%Y/%m/%d(%a) %H:%M:%S %Z')
log = logging.getLogger(name)
log.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
path, maxBytes=maxBytes, backupCount=backupCount)
handler.setFormatter(logging.Formatter(format, datefmt))
log.addHandler(handler)
return log
def get_traceback(exception, args):
message = []
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
for tb in traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback):
message.append(tb)
return '\n'.join(message)
if __name__ == '__main__':
log = getRotateLogger(__file__.replace('.py','.log'))
try:
pass
except Exception, args:
log.error(get_traceback(Exception, args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment