python写日志封装类实例

本文实例讲述了python实现写日志封装类。分享给大家供大家参考。具体如下:

# encoding:utf-8
import sys
import logging
import time
def writelog(message):
logger=logging.getlogger()
filename = time.strftime(‘%y-%m-%d’,time.localtime(time.time()))
handler=logging.filehandler(“./log/”+filename+”error”)
logger.addhandler(handler)
logger.setlevel(logging.notset)
logger.info(message)
if __name__ == ‘__main__’:
writelog(“hello”)

将这段代码保存为 tlog,调用的时候先import tlog,然后tlog.writelog(“jb51.net”)即可

希望本文所述对大家的python程序设计有所帮助。