python的logging日志模块功能很强大,就能实现日志的切换、命名、分级等功能,下面是简单的样例,只需在代码的前面加入,就可以在余下的代码中用log.debug(),log.info()等进行日志记录
#!/usr/bin/python -O
import sys
import logging
import os
import time
log = logging.getLogger("socket")
MYNAME = os.path.splitext(os.path.basename(sys.argv[0]))[0]
MYDATE = time.strftime('%Y%m%d-%H')
LOGFILE = '/tmp/' + MYNAME + '.log.' + MYDATE
#LOGFILE = os.path.join('/tmp',MYNAME + '.log.' + MYDATE)
fh = logging.FileHandler(LOGFILE)
LOGFILE_FORMAT = "%(asctime)s %(process)d %(levelname)-8s [%(name)s] (%(module)s:%(lineno)d) %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(LOGFILE_FORMAT, DATE_FORMAT)
fh.setFormatter(formatter)
log.addHandler(fh)
log.setLevel(logging.DEBUG)
阅读(1353) | 评论(0) | 转发(0) |