1. 简单的将日志打印到屏幕

  1. import logging
  2.  
  3. logging.debug('This is debug message')
  4. logging.info('This is info message')
  5. logging.warning('This is warning message')
  6.  
  7. 屏幕上打印:
  8. WARNING:root:This is warning message

默认情况下,logging将日志打印到屏幕,日志级别为WARNING;
日志级别大小关系为:CRITICAL > ERROR
> WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。

2. 通过logging.basicConfig函数对日志的输出格式及方式做相关配置

  1. import logging
  2.  
  3. logging.basicConfig(level=logging.DEBUG,
  4. format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
  5. datefmt='%a, %d %b %Y %H:%M:%S',
  6. filename='myapp.log',
  7. filemode='w')
  8.  
  9. logging.debug('This is debug message')
  10. logging.info('This is info message')
  11. logging.warning('This is warning message')
  12.  
  13. ./myapp.log文件中内容为:
  14. Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message
  15. Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message
  16. Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message

logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
 %(levelno)s: 打印日志级别的数值
 %(levelname)s: 打印日志级别名称
 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
 %(filename)s: 打印当前执行程序名
 %(funcName)s: 打印日志的当前函数
 %(lineno)d: 打印日志的当前行号
 %(asctime)s: 打印日志的时间
 %(thread)d: 打印线程ID
 %(threadName)s: 打印线程名称
 %(process)d: 打印进程ID
 %(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

3. 将日志同时输出到文件和屏幕

  1. import logging
  2.  
  3. logging.basicConfig(level=logging.DEBUG,
  4. format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
  5. datefmt='%a, %d %b %Y %H:%M:%S',
  6. filename='myapp.log',
  7. filemode='w')
  8.  
  9. #################################################################################################
  10. #定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#
  11. console = logging.StreamHandler()
  12. console.setLevel(logging.INFO)
  13. formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
  14. console.setFormatter(formatter)
  15. logging.getLogger('').addHandler(console)
  16. #################################################################################################
  17.  
  18. logging.debug('This is debug message')
  19. logging.info('This is info message')
  20. logging.warning('This is warning message')
  21.  
  22. 屏幕上打印:
  23. root : INFO This is info message
  24. root : WARNING This is warning message
  25. ./myapp.log文件中内容为:
  26. Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message
  27. Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message
  28. Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message

参考资料:http://blog.csdn.net/langb2014/article/details/53397307

python的logging日志模块的更多相关文章

  1. Python 中 logging 日志模块在多进程环境下的使用

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...

  2. python的logging日志模块(一)

    最近修改了项目里的logging相关功能,用到了Python标准库里的logging模块,在此做一些记录.主要是从官方文档和stackoverflow上查询到的一些内容. 官方文档 技术博客 基本用法 ...

  3. 【python】logging日志模块写入中文编码错误解决办法

    一.问题: 使用python的logging模块记录日志,有时会遇到中文编码问题错误. 二.解决办法: 在logging.FileHandler(path) 中添加指定编码方式 encoding='u ...

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

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

  5. Python之logging日志模块

    logging 用于便捷既然日志切线程安全的模块 vim log_test.py import logging logging.basicConfig(filename='log.log', form ...

  6. python(6)-logging 日志模块

    logging的日志分为5个级别分别为debug(), info(), warning(), error(), critical() 先来看一下简单的代码: logging.basicConfig(f ...

  7. Python中logging日志模块的使用

    参考https://www.cnblogs.com/CJOKER/p/8295272.html

  8. logging日志模块

    为什么要做日志: 审计跟踪:但错误发生时,你需要清除知道该如何处理,通过对日志跟踪,你可以获取该错误发生的具体环境,你需要确切知道什么是什么引起该错误,什么对该错误不会造成影响. 跟踪应用的警告和错误 ...

  9. python 自动化之路 logging日志模块

    logging 日志模块 http://python.usyiyi.cn/python_278/library/logging.html 中文官方http://blog.csdn.net/zyz511 ...

随机推荐

  1. 在nodeJS中操作文件系统(二)

    在nodeJS中操作文件系统(二)   1. 移动文件或目录 在fs模块中,可以使用rename方法移动文件或目录,使用方法如下:     fs.rename(oldPath,newPath,call ...

  2. centos 7 配置iptables(转) + iptabes规则理解

    一.防火墙配置 1.检测并关闭firewall 1 2 3 4 5 systemctl status firewalld.service #检测是否开启了firewall   systemctl st ...

  3. CS100.1x Introduction to Big Data with Apache Spark

    CS100.1x简介 这门课主要讲数据科学,也就是data science以及怎么用Apache Spark去分析大数据. Course Software Setup 这门课主要介绍如何编写和调试Py ...

  4. .NET Core 开发之旅 (1. .NET Core R2安装教程及Hello示例)

    前言 前几天.NET Core发布了.NET Core 1.0.1 R2 预览版,之前想着有时间尝试下.NET Core.由于各种原因,就没有初试.刚好,前几天看到.NET Core发布新版本了,决定 ...

  5. Jmeter(二十二)_脚本上传Gitlab

    Docker部署接口自动化持续集成环境第四步,代码上传到远程仓库! 接上文:Ubuntu部署jmeter与ant Gitlab在容器中部署好了之后,本地直接打开.我们可以在里面创建项目,上传脚本. 新 ...

  6. Monkey基本使用

    什么是 Monkey Monkey 是一个 Android 自动化测试小工具.主要用于Android 的压力测试, 主要目的就是为了测试app 是否会Crash. Monkey 特点 顾名思义,Mon ...

  7. GitLab篇之Linux下环境搭建

    之前公司一直在使用微软的VSS和SVN做为源代码管理工具,考虑到VSS和SVN的局限性,个人一直建议我们应该采用Git来管理我们的源代码.Git的好处不多说相信大家也都知道的.Git不仅仅是一个源代码 ...

  8. 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard

    目录 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard 正文 对我的启发 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard Sanyam Bhutani 是 M ...

  9. HyperLedger/Fabric JAVA-SDK with 1.1

    HyperLedger/Fabric JAVA-SDK with 1.1 该项目可直接在github上访问. 该项目介绍如何使用fabric-sdk-java框架,基于fabric-sdk-java ...

  10. [文章存档]Azure上部署的java app在向第三方服务传送中文时出现乱码

    https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-java ...