日志级别: critical > error > warning > info > debug
四个主要类:
logger提供了应用程序可以直接使用的接口;
handler将(logger创建的)日志记录发送到合适的目的输出;
filter提供了细度设备来决定输出哪条日志记录;
formatter决定日志格式。
# _*_coding:utf-8_*_
from proconfig.vxconfig import VxConfig
import logging
def getMyLogger(file_name=None):
if file_name is None:
file_name = __name__
logger = logging.getLogger(file_name)
logger.setLevel(level=logging.DEBUG)
# ---日志输出到文件
# 文件路径
handler = logging.FileHandler(VxConfig.get_cur_pro_path() + "/log/log.txt")
# 文件格式
formatter = logging.Formatter('%(asctime)s-%(levelname)s - %(pathname)s-%(lineno)d: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# ---日志输出到控制台
console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(logging.DEBUG)
logger.addHandler(console)
return logger
if __name__ == '__main__':
log = getMyLogger(__name__)
log.info("Start print log")
log.debug("Do something")
log.warning("Something maybe fail.")
log.info("Finish")
让日志按照大小截取 或让日志按时间截取:
参考:
按大小截取 logging.handlers.RotatingFileHandler
按时间截取:logging.handlers.TimedRotatingFileHandler
重点讲logging.handlers.TimedRotatingFileHandler 因为我用这个
它的构造函数是:
TimedRotatingFileHandler( filename [,when [,interval [,backupCount]]])
filename :文件路径
when :时间 不区分大小写 S 秒 M 分 H 小时 D 天 W 每星期
midnight 每天凌晨
interval : 时间间隔 等待多少个when之后创建新的文件记录日志
backupCount: 日志保留个数
# _*_coding:utf-8_*_
from logging import handlers
from proconfig.vxconfig import VxConfig
import logging
def getMyLogger(file_name=None):
if file_name is None:
file_name = __name__
logger = logging.getLogger(file_name)
logger.setLevel(level=logging.DEBUG)
# 日志输出到文件
log_file = VxConfig.get_cur_pro_path() + "/log/vx.log"
# filename 文件路径 when="D" 按天分割 interval:等待when那么长时间后 新建log文件记录 backupCount=7 保留30个文件
time_hander = handlers.TimedRotatingFileHandler(filename=log_file, when="D", interval=1, backupCount=7)
formatter = logging.Formatter('%(asctime)s-%(levelname)s - %(pathname)s-%(lineno)d: %(message)s')
time_hander.setFormatter(formatter)
logger.addHandler(time_hander)
# 日志输出到控制台
console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(logging.DEBUG)
logger.addHandler(console)
return logger
if __name__ == '__main__':
log = getMyLogger(__name__)
log.info("Start print log")
log.debug("Do something")
log.warning("Something maybe fail.")
log.info("Finish")
logging配置
参考
配置文件:
[loggers]
keys=root,main
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=fmt
[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler
[logger_main]
level=DEBUG
qualname=main
handlers=fileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=fmt
args=(sys.stdout,)
[handler_fileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=fmt
args=('vx.log','s',5,10)
[formatter_fmt]
format=%(asctime)s-%(levelname)s - %(pathname)s-%(lineno)d: %(message)s
datefmt=
代码:
# coding:utf-8
import logging.config
def getMyLogger(key='root'):
file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(file_path)
config_path = 'logging.conf'
logging.config.fileConfig(config_path)
logger = logging.getLogger(key)
return logger
if __name__ == '__main__':
mylog = getMyLogger()
mylog.debug("我就是试试")
并不怎么好使,还不如自己写xml 然后直接写在代码中明了