1、配置文件
logging.conf
[loggers]
keys=root,MysqlDao
[handlers]
keys=consoleHandler,fileHandler,rotatingFileHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_MysqlDao]
level=DEBUG
handlers=fileHandler
qualname=MysqlDao
propagate=0
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=("../zentao/log/zentao.log", "a")
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=("../zentao/log/zentao.log", "a")
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
2、新建py
mylog.py
#-*- coding:utf-8 -*-
import logging
import logging.config
class MyLog(object):
def __init__(self,tag):
logging.config.fileConfig('logging.conf')
self.logger = logging.getLogger(tag)
def debug(self, msg):
self.logger.debug(msg)
def info(self, msg):
self.logger.info(msg)
def war(self, msg):
self.logger.warning(msg)
def error(self, msg):
self.logger.error(msg)
def cli(self, msg):
self.logger.critical(msg)
if __name__ == '__main__':
logobj = MyLog()
logobj.info('123')
3、使用
from mylog import MyLog
class MysqlDao(object):
def __init__(self):
self.logobj = MyLog('MysqlDao')
def initDB(self):
self.logobj.info("init DB")
m_models.Base.metadata.create_all(bind=db.engine)
log信息
2016-11-18 16:50:07,157 - MysqlDao - INFO - init DB