黄山网站seo,网络营销推广外包平台,一个网站备案两个域名,html网页制作视频教学Python(37):使用logging的配置文件配置日志
输出日志到控制台和日志文件方法#xff1a;
创建一个日志配置文件#xff0c;然后使用fileConfig()函数来读取该文件的内容。
方法1#xff1a;输出日志到文件#xff0c;文件是固定的
方法2#xff1a;输出日志到文件
创建一个日志配置文件然后使用fileConfig()函数来读取该文件的内容。
方法1输出日志到文件文件是固定的
方法2输出日志到文件定时生成新日志文件推荐使用这个
一、fileConfig()函数说明 该函数实际上是对configparser模块的封装关于configparser模块的介绍请参考。 函数定义 该函数定义在loging.config模块下 logging.config.fileConfig(fname, defaultsNone, disable_existing_loggersTrue) 参数 fname表示配置文件的文件名或文件对象defaults指定传给ConfigParser的默认值disable_existing_loggers这是一个布尔型值默认值为True为了向后兼容表示禁用已经存在的logger除非它们或者它们的祖先明确的出现在日志配置中如果值为False则对已存在的loggers保持启动状态。 二、方法1、logging日志配置StreamHandler,FileHandler 一、StreamHandler 流handler——包含在logging模块中的三个handler之一。 能够将日志信息输出到sys.stdout, sys.stderr 或者类文件对象更确切点就是能够支持write()和flush()方法的对象。 只有一个参数 class logging.StreamHandler(streamNone) 日志信息会输出到指定的stream中如果stream为空则默认输出到sys.stderr。 二、FileHandler logging模块自带的三个handler之一。继承自StreamHandler。将日志信息输出到磁盘文件上。 构造参数 class logging.FileHandler(filename, modea, encodingNone, delayFalse) 模式默认为appenddelay为true时文件直到emit方法被执行才会打开。默认情况下日志文件可以无限增大。 1、配置文件logging.conf
[loggers]
#Configure logger information. Must include a logger named root,other:logging.getLogger(fileAndConsole)
keysroot,file,fileAndConsole[handlers]
#Define declaration handlers information.
keysfileHandler,consoleHandler[formatters]
#Set log format
keyssimpleFormatter[logger_root]
levelDEBUG
handlersconsoleHandler#qualname:name in the logger hierarchy,logging.getLogger(fileAndConsole)
[logger_file]
levelDEBUG
handlersfileHandler
qualnamefile
propagate1[logger_fileAndConsole] #Log output to console and file
levelDEBUG
handlersfileHandler,consoleHandler
qualnamefileAndConsole
propagate0[handler_consoleHandler] #Log output to console
classStreamHandler
args(sys.stdout,)
levelDEBUG
formattersimpleFormatter[handler_fileHandler] #Log output to file
classFileHandler
args(./log/test.log, a)
levelDEBUG
formattersimpleFormatter[formatter_simpleFormatter]
format%(asctime)s - %(module)s - %(thread)d - %(levelname)s : %(message)s
datefmt%Y-%m-%d %H:%M:%S
2、调用logtest.py
import logging.config# 读取日志配置文件
logging.config.fileConfig(./conf/logging.conf)# 创建一个日志器logger
logger logging.getLogger(fileAndConsole)
logger.debug(debug)
logger.info(info)
logger.warning(warn)
logger.error(error)
logger.critical(critical)3、结果展示
控制台和test.log都产生日志
控制台 test.log 三、方法2、logging日志配置StreamHandler,TimedRotatingFileHandler 定时循环日志handler位于logging.handlers支持定时生成新日志文件。 class logging.handlers.TimedRotatingFileHandler(filename, whenh, interval1, backupCount0, encodingNone, delayFalse, utcFalse) 参数when决定了时间间隔的类型参数interval决定了多少的时间间隔。如when‘D’interval1就是指一天的时间间隔backupCount决定了能留几个日志文件。超过数量就会丢弃掉老的日志文件。 类型单位‘S’秒‘M’分‘H’时‘D’天‘W0’-‘W6’周一至周日‘midnight’每天的凌晨 1、配置文件logging.conf #以下是和方法一不同的地方使用TimedRotatingFileHandler
[handler_fileHandler] #Log output to file
classlogging.handlers.TimedRotatingFileHandler
args(./log/test.log,midnight)
levelDEBUG
formattersimpleFormatter [loggers]
#Configure logger information. Must include a logger named root,other:logging.getLogger(fileAndConsole)
keysroot,file,fileAndConsole[handlers]
#Define declaration handlers information.
keysfileHandler,consoleHandler[formatters]
#Set log format
keyssimpleFormatter[logger_root]
levelDEBUG
handlersconsoleHandler#qualname:name in the logger hierarchy,logging.getLogger(fileAndConsole)
[logger_file]
levelDEBUG
handlersfileHandler
qualnamefile
propagate1[logger_fileAndConsole] #Log output to console and file
levelDEBUG
handlersfileHandler,consoleHandler
qualnamefileAndConsole
propagate0[handler_consoleHandler] #Log output to console
classStreamHandler
args(sys.stdout,)
levelDEBUG
formattersimpleFormatter[handler_fileHandler] #Log output to file
classlogging.handlers.TimedRotatingFileHandler
args(./log/test.log,midnight)
levelDEBUG
formattersimpleFormatter[formatter_simpleFormatter]
format%(asctime)s - %(module)s - %(thread)d - %(levelname)s : %(message)s
datefmt%Y-%m-%d %H:%M:%S
2、调用logtest.py import logging.config# 读取日志配置文件
logging.config.fileConfig(./conf/logging.conf)# 创建一个日志器logger
logger logging.getLogger(fileAndConsole)
logger.debug(debug)
logger.info(info)
logger.warning(warn)
logger.error(error)
logger.critical(critical)3、结果展示
控制台和test.log都产生日志
控制台 test.log 问题解决 问题UnicodeDecodeError: gbk codec cant decode byte 0xab in position 44: illegal multibyte sequence 问题原因l
ogging.conf配置文件中注释有中文并且编码格式为utf-8。
解决办法1
Windows下的解决办法把日志配置文件logging.conf改成ANSI编码
Linux下的解决办法把日志配置文件logging.conf改成UTF-8编码 解决办法2
logging.conf的注释全部修改为英文(编码格式为utf-8也没关系)这种方式更好些。 其他用代码配置的可参考
python的logging模块中的配置文件
python之logging的文件配置 - 简书 (jianshu.com)
python之配置日志的几种方式
python自动按日期保存日志文件
https://blog.csdn.net/yypsober/article/details/51800120
https://blog.csdn.net/Liu_Jilong/article/details/131896723