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. MySQL的sql_mode解析与设置,sql文件导入报错解决

    在往MySQL数据库中插入一组数据时,出错了!数据库无情了给我报了个错误:ERROR 1365(22012):Division by 0:意思是说:你不可以往数据库中插入一个 除数为0的运算的结果.于 ...

  2. eclipse debug Liunx服务器上的svn项目

    1.本地项目提交到svn上,以保证本地代码与服务器代码相同 2.开启服务器debug端口 3.使用root账号重新部署服务器项目并监听catalina.out sh /home/p/deploy/gt ...

  3. [AX]AX2012 Interaction class

    Ax2012 Client的form如果属性FormTemplate设置为DetailsPage或者ListPage,则必须同时设置属性InteractionClass为相应的Interaction类 ...

  4. UITextField in a UITableViewCell

    http://stackoverflow.com/questions/409259/having-a-uitextfield-in-a-uitableviewcell http://stackover ...

  5. An internal error occurred during: "Launching xxx on WebLogic10.x".

    An internal error occurred during: "Launching xxx on WebLogic10.x". java.lang.NullPointerE ...

  6. PostgreSQL流复制

    原理机制 参考--https://yq.aliyun.com/articles/51009 主备总体结构 PG主备流复制的核心部分由walsender,walreceiver和startup三个进程组 ...

  7. 【安全开发】python安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...

  8. Splash runjs() 方法

    runjs() 方法可以执行 JavaScript 代码,它与 evaljs() 功能类似,但是更偏向于执行某些动作或声明某些方法 function main(splash, args) splash ...

  9. Spring transaction事务 roll back各种回滚

    Spring的AOP事务管理默认是针对unchecked exception回滚. 也就是默认对RuntimeException()异常极其子类进行事务回滚. Exception作为基类,下面还分ch ...

  10. 从Eclipse转移到IntelliJ IDEA的一点心得

    IntelliJ使用指南—— 深入理解IntelliJ的Web部署逻辑 Intellij IDEA 部署Web项目,解决 404 错误 Intellij IDEA快捷键的使用 本文转载地址 本人使用I ...