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配置日志的更多相关文章

  1. python项目通过配置文件方式配置日志-logging

    背景:项目中引入日志是必须的,这里介绍通过配置文件config.ini的方式配置日志 1.新建config.ini 2.添加配置 [loggers]keys=root,ProxyIP [handler ...

  2. 解决多个py模块调用同一个python的logging模块,打印日志冲突问题

    前期对python中的logging模块进行了封装,这样自动化测试框架中的多个测试脚本(py)就可以使用同一个封装后的日志系统,这样各脚本中只需要引用一下即可,方面快捷.那么当我使用unittest框 ...

  3. python的logging日志模块(二)

    晚上比较懒,直接搬砖了. 1.简单的将日志打印到屏幕   import logging logging.debug('This is debug message') logging.info('Thi ...

  4. Python之配置日志的几种方式(logging模块)

    原文:https://blog.csdn.net/WZ18810463869/article/details/81147167 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Py ...

  5. logging.config模块---使用配置文件管理logger

    logging配置文件 一.使用到的模块: logging.config 官方文档: https://docs.python.org/3/library/logging.config.html 非官方 ...

  6. python模块 ---logging模块

    摘要by crazyhacking: 与log4cxx一样,分为三个部分,logger, handler,formatter. 详细内容参考:1官网http://docs.python.org/2/h ...

  7. python 多进程 logging:ConcurrentLogHandler

    python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...

  8. python之配置日志的三种方式

    以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...

  9. 【转】python之配置日志的几种方式

    [转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...

随机推荐

  1. CodeChef Mahesh and his lost array

    Mahesh and his lost array   Problem code: ANUMLA   Submit All Submissions   All submissions for this ...

  2. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  3. Windows程序设计--(五)绘图基础

    5.1 GDI的结构 图形设备接口(GDI:Graphics Device Interface)是Windows的子系统,它负责在视讯显示器和打印机上显示图形. 5.2 设备环境 5.2.1 获取设备 ...

  4. elasticsearch 基础 —— Jion父子关系

    前言 由于ES6.X版本以后,每个索引下面只支持单一的类型(type),因此不再支持以下形式的父子关系: PUT /company { "mappings": { "br ...

  5. Spring Boot 2 Webflux的全局异常处理

    https://www.jianshu.com/p/6f631f3e00b9 本文首先将会回顾Spring 5之前的SpringMVC异常处理机制,然后主要讲解Spring Boot 2 Webflu ...

  6. nginx日志切割脚本shell

    nginx-log-rotate.sh: #!/bin/bash#---------------------------------------------# Comment:Used for rot ...

  7. squid代理与缓存(上)

    squid代理与缓存(上) 1. Squid介绍 1.1 缓存服务器介绍 缓存服务器(英文意思cache server),即用来存储(介质为内存及硬盘)用户访问的网页,图片,文件等等信息的专用服务器. ...

  8. centos(6-7)安装openldap

    前言 参考资料: http://yhz61010.iteye.com/blog/2352672 https://www.cnblogs.com/lemon-le/p/6266921.html 实验环境 ...

  9. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  10. Linux 下源码安装ngnix

    版本说明: NGINX 版本1.12.0 pcre-8.40 zlib-1.2.11 openssl-1.1.0i   安装过程 # ./configure  --prefix=/usr/ngnix  ...