2019独角兽企业重金招聘Python工程师标准>>>

在Python中,上面以实现的和已经实现的,均可以使用logging模块迅速搞定,且仅仅只需要一个配置文件,两行代码,实现过程如下(仅以输出的磁盘文件为例,命令输出只需要修改配置文件即可,具体可查API手册):

1. 定义配置文件logging.conf:

[loggers]
keys=root,applog
[handlers]
keys=rotateFileHandler
[formatters]
keys=applog_format[formatter_applog_format]
format=[%(asctime)s - %(name)s]%(levelname)s:  %(message)s - %(filename)s:%(lineno)d[logger_root]
level=NOTSET
handlers=rotateFileHandler[logger_applog]
level=NOTSET
handlers=rotateFileHandler
qualname=simple_example[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=NOTSET
formatter=applog_format
args=('log_1.log', 'a', 10000, 9)

注意前三个[ ]中的keys,这个在后面各[ ]中定义定义,section的取名格式如looger_自定义名称, handler_自定义名称,我偷懒直接使用了标准名称,其他一样,最后一个要注意的就是format,即日志文件中内容的格式,具体见后面附一。level参数是日志级别,可扩展,如果使用python自己的,有以下四个级别:

Level Numeric value 
CRITICAL       50 
ERROR          40 
WARNING        30 
INFO           20 
DEBUG          10 
NOTSET          0

例如配置文件中level定义为WARN,则INFO, DEBUG,NOTSET三个级别的日志点则不会输出,很方便的做到了日志级别控制。args定义了日志方件名,写方式,最大大小,保存最多个数等属性。

#!/usr/bin/env python
# -*- coding: utf-8 -*-import logging
import logging.config#日志初始化
LOG_FILENAME = 'logging.conf'
logging.config.fileConfig(LOG_FILENAME)
logger = logging.getLogger("simple_log_example")#测试代码
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")

运行后,查看日志文件,内容如下:
[2012-02-11 14:47:05,483 - simple_log_example]DEBUG:  debug message - test_log.py:48
[2012-02-11 14:47:05,483 - simple_log_example]INFO:  info message - test_log.py:49
[2012-02-11 14:47:05,483 - simple_log_example]WARNING:  warn message - test_log.py:50
[2012-02-11 14:47:05,483 - simple_log_example]ERROR:  error message - test_log.py:51
[2012-02-11 14:47:05,483 - simple_log_example]CRITICAL:  critical message - test_log.py:52

如将日志级别设置为WARN,再次运行,查看日志:
[2012-02-11 14:54:20,046 - simple_log_example]WARNING:  warn message - test_log.py:50
[2012-02-11 14:54:20,092 - simple_log_example]ERROR:  error message - test_log.py:51
[2012-02-11 14:54:20,092 - simple_log_example]CRITICAL:  critical message - test_log.py:52

附一:format参数格式说明:

Format Description
%(name)s Name of the logger (logging channel).
%(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
%(levelname)s Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
%(pathname)s Full pathname of the source file where the logging call was issued (if available).
%(filename)s Filename portion of pathname.
%(module)s Module (name portion of filename).
%(funcName)s Name of function containing the logging call.
%(lineno)d Source line number where the logging call was issued (if available).
%(created)f Time when the LogRecord was created (as returned by time.time()).
%(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(asctime)s Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).
%(msecs)d Millisecond portion of the time when the LogRecord was created.
%(thread)d Thread ID (if available).
%(threadName)s Thread name (if available).
%(process)d Process ID (if available).
%(message)s The logged message, computed as msg % args.

转载于:https://my.oschina.net/gshlsh17/blog/130975

Python:日志模块logging的应用相关推荐

  1. Python日志模块logging高级用法

    推荐图书: <Python程序设计(第3版)>,(ISBN:978-7-302-55083-9),董付国,清华大学出版社,2020年6月第1次印刷,2021年12月第11次印刷,山东省一流 ...

  2. python logging模块的作用_【python】【logging】python日志模块logging常用功能

    logging模块:应用程序的灵活事件日志系统,可以打印并自定义日志内容 logging.getLogger 创建一个log对象 >>> log1=logging.getLogger ...

  3. python日志模块----logging

    使用Python的logging模块能很好的帮我们完成程序的日志功能,其实它跟Java中的log4j有不少相似的地方.下面记录下今天学习到的logging的知识(因为有一些还没真正使用过,不知道是否说 ...

  4. Python日志模块logging,这一篇就够了

    ​ 橙好科技logging模块教程 文章目录 1-logging介绍 2-日志作用 3-日志配置basicConfig 3-日志级别level 4-日志格式format 4-输出日志到控制台 5-输出 ...

  5. python 日志模块 logging

    如何使用python自带的 logging 模块实现日志功能 1.初始化一个logger对象 1)引入模块 import os import logging import sys 2)初始化变量,声明 ...

  6. python logging模块的作用_Python 日志模块logging分析及使用-2

    本文作为Python日志模块的补充,主要介绍日志回滚RotatingFileHandler和TimedRotatingFileHandler的使用,以及其所带来的问题.Logger对象的日志等级是如何 ...

  7. Python之配置日志模块logging

    一.定义日志打印方式 如果我们运行自己的程序,有时候需要记录程序运行过程中出现的问题或者信息.可以运用日志模块logging来记录,该模块日志格式可以根据用户需求来自己定义. 常见打印日志信息形式如下 ...

  8. Python的日志模块logging的使用

    Python的日志模块logging的使用 1 logging模块介绍 2 logging 简单示例 3 logging.basicConfig() 4 logging组件 4.1 Logger类 4 ...

  9. 【Python】—日志模块logging使用详解1

    文章目录 1.日志级别 2.logging流程 3.几个重要的类 3.1 Logger类 1) 最常用的配置方法 2) 创建`Logger`对象`logging.getLogger([name])` ...

最新文章

  1. cocos2d-x3.0rc打包apk遇到的一些问题记录
  2. JDK源码解析 Runable是一个典型命令模式,Runnable担当命令的角色,Thread充当的是调用者,start方法就是其执行方法
  3. dell r220服务器配置oracle linux 阵列卡,如何在Dell服务器PERC5/6阵列卡配置RAID
  4. Comet:基于 HTTP 长连接的“服务器推”技术解析
  5. 如何提高SSD的使用寿命
  6. storm的并行度的解释--- ( 看完就能理解 )
  7. java compare equla_Java中的equals,==,compareTo和compare的比较
  8. Visual Studio Code(VSCODE)语言设置
  9. 用户具有FullControl的权限,但是还是创建不了页面?
  10. 7 vsphere 分配许可_vCenter server 5.5中添加ESXi5.5主机并分配许可密钥
  11. Linux上的arping命令介绍
  12. java velocity js_JavaScript 模板引擎 Velocity.js_js
  13. 如何优雅地删除Redis大键
  14. 学之思开源代码学习(1)
  15. c语言考试考点,C语言考点精选
  16. Houdini vop
  17. 【转】校园招聘 建议
  18. Java入门基础学习总结
  19. 安卓 P 动态显示和隐藏虚拟按键
  20. Conway’s Game of Life中看C++SSE2并行化计算

热门文章

  1. mac使用allure_制作测试报告,allure,生成
  2. adams怎么做往复运动_关于HiFi | 在家里听音乐看电影,喇叭应该怎么选?
  3. esxi vsphere的端口_硬干货!一张图弄清楚在ESXi下如何进行网络抓包
  4. 2021新乡市铁一中高考成绩查询,2021铁一中中考录取分数线预测
  5. php用户同步,ucenter同步用户登录【转】
  6. python pywin32模块 修改cad_安装pywin32(Python调用win api必看)
  7. 单例模式访问mysql设计类图_利用单例模式设计数据库连接Model类
  8. 甜甜用计算机1050除以一个数,上册四年级数学期末试卷带答案
  9. js文件中可以写html吗,js代码写在HTML正常,分离成js文件再在HTML中引用不起作用...
  10. oracle24801错误,[数据库]oracle错误(ORA