Python的logging,记录log的包
import logging
import logging.handlers
LOG_FILE=r"C:\Users\min.sun\Desktop\自动化测试\log.txt"
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5)
fmt = "%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s"
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
logger = logging.getLogger('tst')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.info('first info message')
logger.debug('first debug message')
try:
1/0
except Exception as e:
logger.debug(e)
print(e)
[loggers]
keys=root,example01,example02,performtest [logger_root]
level=DEBUG
handlers=hand01,hand02 [logger_example01]
handlers=hand03
qualname=example01
propagate=0 [logger_example02]
handlers=hand01,hand03
qualname=example02
propagate=0 [logger_performtest]
handlers=hand03
qualname=performtest
propagate=0
###############################################
[formatters]
keys=form01,form02 [formatter_form01]
format=%(asctime)s - [%(filename)s:%(lineno)s] - %(levelname)s - %(message)s
datefmt= [formatter_form02]
format=%(name)-12s: %(levelname)-8s %(message)s
datefmt=%a, %d %b %Y %H:%M:%S ############################################### [handlers]
keys=hand01,hand02,hand03 [handler_hand01]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stderr,) [handler_hand02]
class=FileHandler
level=DEBUG
formatter=form01
#要存储log的文件地址
args=(r"C:\Users\min.sun\Desktop\testlog.txt", 'a') [handler_hand03]
class=handlers.RotatingFileHandler
level=INFO
formatter=form01
args=(r"C:\Users\min.sun\Desktop\testlog.txt", 'a',10*1024*1024, 5)
(说明:重点注意一下handler中的class,logging提供了多种Handler,不同的代表了不同的日志存储方式,具体内容可以见我下图给出的参考)
模块中调用的方法:
import logging
import logging.config
logging.config.fileConfig(r"C:\Users\min.sun\Desktop\autotest\com\log.conf")
logger = logging.getLogger("example01") logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message"
说明:在用logging的过程中遇到了编码的问题,一个是配置文件中的存储log的文件路径中包含中文,但是执行时提示GBK的不能读取;一个是存储的log包含中文,结果存储到日志文件中后乱码。
解决方法:将log文件更改编码为UTF8无BOM格式编码。配置文件也更改为这个格式或者GBK格式。 参考文章:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html
Python的logging,记录log的包的更多相关文章
- 【python】logging记录traceback
import logging try: do-something-block except: logging.exception("Exception Logged")
- Python的日志记录-logging模块的使用
一.日志 1.1什么是日志 日志是跟踪软件运行时所发生的事件的一种方法,软件开发者在代码中调用日志函数,表明发生了特定的事件,事件由描述性消息描述,同时还包含事件的重要性,重要性也称为级别或严重性. ...
- [ Python入门教程 ] Python中日志记录模块logging使用实例
python中的logging模块用于记录日志.用户可以根据程序实现需要自定义日志输出位置.日志级别以及日志格式. 将日志内容输出到屏幕 一个最简单的logging模块使用样例,直接打印显示日志内容到 ...
- python 中 logging 模块的 log 函数以及坑
记录下吧,一个日志的函数,但有个坑是在调用函数时需要先将函数实例化为一个变量,否则进入某个循环时会多次刷新日志: """ 日志模块 """ ...
- day27 python学习 logging
logging模块 函数式简单配置 import logging logging.debug('debug message') logging.info('info message') logging ...
- Python之logging模块
一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...
- python模块 ---logging模块
摘要by crazyhacking: 与log4cxx一样,分为三个部分,logger, handler,formatter. 详细内容参考:1官网http://docs.python.org/2/h ...
- python的logging模块之读取yaml配置文件。
python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...
- django/python日志logging 的配置以及处理
日志在程序开发中是少不了的,通过日志我们可以分析到错误在什么地方,有什么异常.在生产环境下有很大的用处.在java 开发中通常用 log4j,logback 等三方组件.那么在 django中是怎么处 ...
随机推荐
- 【Spring实战-1】网站登录系统
系统概述: 2015年5月8日 14:05:49 本系统为一个常规的用户登录系统,主要完成了用户登录界面设计.用户登录后信息校验.更新用户登录信息以及记录用户登录信息等. 整个系统以Spring框架 ...
- bzoj1864 三色二叉树
Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. 记录每个节点染 ...
- 一些css杂项笔记
div[class*="col-"]{ background-color: gold; border: 1px solid #ccc; } //给class开头等于col-的div ...
- 使用Golang进行性能分析(Profiling)
转自:http://www.cppblog.com/sunicdavy/archive/2015/04/11/210308.html 本文介绍游戏服务器的性能分析, web服务器性能分析不在本文分析范 ...
- WCF服务部署
一.将WCF服务部署到IIS上 1.首先检测电脑上是否安装了IIS,一般来说Win7以上系统自带IIS 2.下面进行IIS服务的开启设置 控制面板=>打开或关闭Windos功能 3.勾选该窗口中 ...
- iOS 基础
layoutSubviews: layoutSubviews是对sbuviews的重新布局,比如,我们想更新子视图的位置,可以通过调用layoutSubviews方法(不能直接调用) layoutSu ...
- 【基础知识五】神经网络NN
常用模型:BP神经网络,RBF神经网络 一.神经元模型 | 连接权,阈值,激活函数 1. 输入信号通过带权重的连接(connection)进行传递,神经元接收到的总输入值将与神经元的阈值进行比较, ...
- 管理11gRAC基本命令 (转载)
在 Oracle Clusterware 11g 第 2 版 (11.2) 中,有许多子程序和命令已不再使用: crs_stat crs_register crs_unregiste ...
- IntelliJ IDEA神器使用技巧笔记
1. Alt + 数字 打开idea 快捷键打开相应的窗口: 高效定位代码: 无处不在的跳转 1.项目间的跳转: Windows -> ctrl+alt+[ / ] 2.文件之间的跳转 ...
- python学习笔记一和PHP的一些对比
python和php一样是 解释性语言 php和php一样 定义变量不需要指定类型,C语言中的print函数 在python中也是适用的 python编码 适用缩进 4个空格,通常存储为UTF-8模 ...