转 使用Python的logging.config.fileConfig配置日志
Python的logging.config.fileConfig方式配置日志,通过解析conf配置文件实现。文件 logglogging.conf 配置如下:
[loggers]
keys=root,fileLogger,rotatingFileLogger
[handlers]
keys=consoleHandler,fileHandler,rotatingFileHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_fileLogger]
level=DEBUG
# 该logger中配置的handler
handlers=fileHandler
# logger 的名称
qualname=fileLogger
propagate=0
[logger_rotatingFileLogger]
level=DEBUG
# 这样配置,rotatingFileLogger中就同时配置了consoleHandler,rotatingFileHandler
# consoleHandler 负责将日志输出到控制台
# rotatingFileHandler 负责将日志输出保存到文件中
handlers=consoleHandler,rotatingFileHandler
qualname=rotatingFileLogger
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('logs/logging.log', 'a')
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=WARNING
formatter=simpleFormatter
args=("logs/rotating_logging.log", "a", 1*1024*1024, 5)
[formatter_simpleFormatter]
#format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
format=%(asctime)s - %(module)s - %(thread)d - %(levelname)s : %(message)s
datefmt=%Y-%m-%d %H:%M:%S
以上配置文件主要包含以下几部分:
loggers : 配置logger信息。必须包含一个名字叫做root的logger,当使用无参函数logging.getLogger()时,默认返回root这个logger,其他自定义logger可以通过 logging.getLogger("fileLogger") 方式进行调用
handlers:定义声明handlers信息。常用的handlers包括 StreamHandler(仅将日志输出到kong控制台)、FileHandler(将日志信息输出保存到文件)、RotaRotatingFileHandler(将日志输出保存到文件中,并设置单个日志wenj文件的大小和日志文件个数)
formatter : 设置日志格式
logger_xxx : 对loggers中声明的logger进行逐个配置,且要一一对应
handler_xxx : 对handlers中声明的handler进行逐个配置,且要一一对应
formatter_xxx : 对声明的formatterjinx进行配置
代码示例
logging.config.fileConfig(“logging.conf”)
# 输出日志到控制台,获取的是root对应的logger
console_logger = logging.getLogger()
# 输出日志到单个文件
file_logger = logging.getLogger(name="fileLogger")
# rotatingFileLogger中额consoleHandler输出到控制台,rotatingHandler输出日志到文件
rotating_logger = logging.getLogger(name="rotatingFileLogger")
友情提示
进行以上配置后,在项目中需要进行日志输出的地方通过logging.getLogger()方式就可以获取到du应的logger,然后就可以使用logger.info("xxx")jinx进行日志输出了。
使用这种方式配置日志,一定要在项目的入口函数中就调用 logging.config.fileConfig(“logging.conf”)函数,因为 logging.conf 文件中,在handler中配置的是日志文件的相对地址,如果在其他代码文件中进行调用,由于相对地址的原因,将导致日志文件会出现在yixi意想不到的位置。
---------------------
作者:cxx654
来源:CSDN
原文:https://blog.csdn.net/cxx654/article/details/83216337
版权声明:本文为博主原创文章,转载请附上博文链接!
转 使用Python的logging.config.fileConfig配置日志的更多相关文章
- python项目通过配置文件方式配置日志-logging
背景:项目中引入日志是必须的,这里介绍通过配置文件config.ini的方式配置日志 1.新建config.ini 2.添加配置 [loggers]keys=root,ProxyIP [handler ...
- 解决多个py模块调用同一个python的logging模块,打印日志冲突问题
前期对python中的logging模块进行了封装,这样自动化测试框架中的多个测试脚本(py)就可以使用同一个封装后的日志系统,这样各脚本中只需要引用一下即可,方面快捷.那么当我使用unittest框 ...
- python的logging日志模块(二)
晚上比较懒,直接搬砖了. 1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('Thi ...
- Python之配置日志的几种方式(logging模块)
原文:https://blog.csdn.net/WZ18810463869/article/details/81147167 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Py ...
- logging.config模块---使用配置文件管理logger
logging配置文件 一.使用到的模块: logging.config 官方文档: https://docs.python.org/3/library/logging.config.html 非官方 ...
- python模块 ---logging模块
摘要by crazyhacking: 与log4cxx一样,分为三个部分,logger, handler,formatter. 详细内容参考:1官网http://docs.python.org/2/h ...
- python 多进程 logging:ConcurrentLogHandler
python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...
- python之配置日志的三种方式
以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...
- 【转】python之配置日志的几种方式
[转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...
随机推荐
- 2018CCPC吉林赛区(重现赛)
http://acm.hdu.edu.cn/contests/contest_show.php?cid=867 A题,直接分块,不知道正解是什么. #include<bits/stdc++.h& ...
- vue 中公共样式
html, body, h1, h2, h3, h4, h5, h6, p, textarea, input, select, ul, ol, li, fieldset, figure { margi ...
- KindEditor 文本编辑器
官网:http://kindeditor.net/docs/usage.html 目前支持ASP.ASP.NET.PHP.JSP.
- VUE小案例--简易计算器
这个小案例主要时练习v-model的使用,功能并不完善 <!DOCTYPE html> <html lang="zh-CN"> <head> & ...
- Linux Shell 脚本学习第一天: 使用grep 命令,lsusb, ps -ef, 实现树莓派(Debian OS)时检测到依赖的USB设备启动后,启动终端自动执行shell脚本
1.应用背景: 无人监测的设备,常需要设置应用程序开机启动,程序启动前需要保证调用的设备先启动,运行环境先启动. 2.test.sh部分源码 #!/bin/sh #查看桌面是否启动 while tru ...
- 最强大的Android线程池框架
背景 大家都知道在我们的开发中永远都离不开多线程,对于我们为什么要使用多线程,多线程的使用和多线程的一些基础知识这里我们就不讲了,有兴趣的朋友可以去看一下博主之前的几篇文章: 线程你真的了解它吗 这才 ...
- [CentOS]安装软件:/lib/ld-linux.so.2: bad ELF interpreter 解决
错误:/usr/local/bin/rar: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 解决:是因为64位系 ...
- 通过注释查找mysql的表名
通过注释查找mysql的表名 select * from INFORMATION_SCHEMA.columns where COLUMN_NAME Like '%placement%';
- springBoot相关(二)
Spring Boot 三大特性: 组件自动装配:Web MVC .Web Flux .JDBC等 嵌入式Web容器:Tomcat.Jetty以及Undertow 生产准备特性:指标.健康检查.外部化 ...
- python在windows中运行文件
"d:Program Files\python35\python.exe" hello.txt