logging

用于便捷既然日志切线程安全的模块

vim log_test.py

import logging

logging.basicConfig(filename='log.log',
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
level=logging.DEBUG) logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')
logging.log(10,'log')

运行生成日志文件log.log

模拟一个生成错误日志的脚本

import logging

logging.basicConfig(filename='log.log',
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
level=logging.DEBUG) while True:
option = raw_input("input a digit:")
if option.isdigit():
print "hehe",option
logging.info('option correct')
else:
logging.error('Must input a digit!')

执行如果输入的是数字,写入info日志如果不是则写成error日志

PS:level=logging.DEBUG 是代表最低记录基本如果改成WARNING则不会记录info,debug信息就算设置了

上面是把日志写到文件里面

把日志显示到屏幕上面有输出到文件

import logging

logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
#on screen
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)
#into file
fh = logging.FileHandler("log2.log")
fh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s") ch.setFormatter(formatter)
fh.setFormatter(formatter) logger.addHandler(ch)
logger.addHandler(fh) logger.debug("debug msg...")
logger.info("info msg...")
logger.warn("warn msg...")
logger.error("error msg...")
logger.critical("critical msg...")

屏幕没有输出debug信息

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(6)-logging 日志模块

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

  6. python的logging日志模块

    1. 简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info mess ...

  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. Hibernate session FlushMode的五种设置

    http://www.2cto.com/kf/201207/141455.html Hibernate session FlushMode有五种属性:1.NEVEL:已经废弃了,被MANUAL取代了2 ...

  2. springboot+elasticsearch配置实现

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. git同时提交到两个仓库

    有时候一个项目,希望既提交到oschina又提交到公司内网的gitlab,或者是github什么的. 使用git remote -v 查看当前git的远程仓库. 添加一个远程仓库 git remote ...

  4. jquery组件WebUploader文件上传用法详解

    这篇文章主要为大家详细介绍了jquery组件WebUploader文件上传用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 WebUploader是由Baidu WebFE(FEX)团队开发的一 ...

  5. JQuery------各种版本下载

    转载: http://www.jq22.com/jquery-info122

  6. NuGet的几个小技巧(转)

    NuGet的几个小技巧   因为可视化库程序包管理器的局限性,有很多需要的功能在界面中无法完成. 以下技巧均需要在“程序包管理器控制台”中使用命令来完成. 一.改变项目目标框架后,更新程序包 当改变项 ...

  7. lua 对表的简单序列化与反序列化

    参考文档:http://blog.csdn.net/xiaodan007/article/details/7096718 function sz_T2S(_t) local szRet = " ...

  8. python卸载或者安装时提示There is a problem with this Windows Installer package.A program required for this install to complete could not be run. Contact your support personnel or package vendor

    1.卸载时报这个错,先进行下修复,再执行卸载: 2.安装时报这个错,安装的过程中,没有取得管理员的权限. Msi格式的文件,点右键后,也没有“以管理员身份运行”的菜单项,那怎么办呢?你可以点“开始”菜 ...

  9. Java - Calendar类的使用

    今天在写代码时需要用到时间相关的类,一开始,数据库中存的数据类型是timestamp的,所以在Java中就使用了 Timestamp类型,但当调用Timestamp类型的方法时发现,它的很多方法都是d ...

  10. Ubuntu12.04编译Android2.3.4

    Ubuntu12.04编译Android2.3.4 1.下载Ubuntuubuntu-12.04-dvd-i386.iso2.使用U盘安装,启动盘制作用unetbootin-windows-568工具 ...