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中是怎么处 ...
随机推荐
- FPGA将加速今日新型态数据中心的主流应用
在这个强调智能与联网的时代,可编程逻辑栅阵列 (FPGA)已经成为一个重要且不可或缺的元件.以全球500亿个联网设备,一年所产生的数据量将不计其数.从数据中心.5G通讯.虚拟网络功能,到嵌入式系统,F ...
- 【Spring学习笔记-MVC-7】Spring MVC模型对象-模型属性讲解
作者:ssslinppp 来自为知笔记(Wiz) 附件列表 处理模型数据.png
- [转][C#]验证
文件下载 本文仅做备份,参考自:http://www.cnblogs.com/LoveJenny/p/opensource_software_license_tool__easyhelper_easy ...
- [转]Tornado get/post请求异步处理框架分析
转自: http://m.blog.csdn.net/blog/joeyon/41956027 首先说下环境,python2.7,Tornado3.0 然后本文讨论的不是Tornado底层如何用epo ...
- centos 7 mount usb hard disk(ntfs format)
1. yum install -y epel-release* 2. yum install -y ntfs-3g 3. 命令:fdisk -l (查看磁盘分区信息) [root@devserverg ...
- solr入门之搜索建议的几种实现方式和最终选取实现思路
上篇博客中我简单的讲了下solr自身的suggest模块来实现搜索建议.但是今天研究了下在solr自身的suggest中添加进去拼音来智能推荐时不时很方便.在次从网上搜集和整理思考了下该问题的解决. ...
- UML 中关系图的解说
最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有些概念还是经常被混淆的,写在这里是为了加深印象. 关系列表: 继承关系(Generalization): 实现关系 ...
- navicate for mysql mac 含注册机 亲测可用
百度网盘:https://pan.baidu.com/s/1hrXnRes
- catpcha
生成随机验证码: # -*- coding: utf-8 -*- # @Author: huangyong # @Date: 2016-10-29 22:18:38 # @Last Modified ...
- 系统一般信息监控查看shell.磁盘,负载等达阀值告警机制,改进测试中.
1 #!/bin/sh 2 #Create by Qrui 3 while [ "1"="1" ] 4 do 5 clear 6 7 echo &q ...