Python模块-logging模块(一)
logging模块用来写日志文件
有5个级别,debug(),info(),warning(),error()和critical(),级别最高的为critical()
debug()为调试模式,info()为正常情况下的信息,warning()为警告,error()为错误,critical()为严重问题
普通的打印
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.debug('The debug') logging.info('The info') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
5个级别中只打印了警告级别及之后的语句,因为Logger的默认级别为warning
写入日志文件
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(filename='log_test.log',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
level=loggin.INFO,把日志级别设置为INFO,只有INFO或比INFO级别更高的日志才会被写到日志文件里
写入日志文件的内容
只从info级别开始写入内容,debug级别比info低,没有写入
日志格式化
%(name)s logger的名称
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(name)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(levelno)s 数字形式的日志级别,%(levelname)s 文本形式的日志级别
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(levelno)s,%(levelname)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(lineno)d 调用日志输出函数的语句所在的代码行
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(lineno)d',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(pathname)s 调用日志输出函数的模块的完整路径名
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(pathname)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(filename)s 调用日志输出函数的模块的文件名
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(filename)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(module)s 调用日志输出函数的模块名
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(module)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
就是文件名,没有后缀
%(funcName)s 调用日志输出函数的函数名
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(funcName)s',level=logging.INFO) def log_test(): logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down') log_test()
运行结果
%(created)f 当前时间,用UNIX标准的表示时间的浮点数表示
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(created)f',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(relativeCreated)d 输出日志信息自创建以来的毫秒数
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging import time logging.basicConfig(format='%(relativeCreated)d',level=logging.INFO) logging.debug('This python is running') time.sleep(2) logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
结果是不定的
%(asctime)s 字符串形式的当前时间,默认格式为“2018-02-15 17:59:31,546”,逗号后面的是毫秒
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(asctime)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(thread)d 当前线程号,%(threadName)s 当前线程名
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(thread)d,%(threadName)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(process)d 进程号
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(process)d',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
%(message)s 用户输出的消息
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(format='%(message)s',level=logging.INFO) logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果
格式化的日志写入
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import logging logging.basicConfig(filename='log_test.log',level=logging.INFO,format='%(message)s',datefmt='%m') logging.debug('This python is running') logging.info('The infomation of python') logging.warning('Request outtime') logging.error('The python run error') logging.critical('The server down')
运行结果