logging 文件日志
1. 例子
import logging
logging.basicConfig(filename='log.txt', #文件名
level=logging.DEBUG, #级别
format=u'时间:%(asctime)s\n级别:%(levelname)s\n消息:%(message)s\n', #日志格式
datefmt='%Y-%m-%d %H:%M:%S') # 时间格式
logging.debug(u'第一条记录')
logging.info(u'第二条记录')
2. 级别
日志所记录的消息可以划分为不同的级别,一般用以下几种预定义的级别。
每种级别有对应的值,可以用来比较级别的高低。
级别 | 值 |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
每个级别都有对应的方法,用小写字母,比如 logging.debug() , logging.info(),分别用来记录 DEBUG 级别和 INFO 级别的消息。
logging.basicConfig 中配置的级别可以用来过滤消息,比配置级别低的消息将被忽略,不会写入文件。
比如,如果一开始配置的是 level=logging.INFO ,那么调用 logging.debug() 处理的消息将被忽略,不会记录到文件。只有用 info() 或者 warning() 以及更高级别才会被记录。
3. 日志格式
格式化字符串支持如下参数:
参数 | 解释 |
---|---|
%(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form '2003-07-08 16:49:45,896'. |
%(created)f | Time when the LogRecord was created (as returned by time.time()). |
%(filename)s | Filename portion of pathname. |
%(funcName)s | Name of function containing the logging call. |
%(levelname)s | Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(module)s | Module (name portion of filename). |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. |
%(message)s | The logged message. |
%(name)s | Name of the logger used to log the call. |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(process)d | Process ID (if available). |
%(processName)s | Process name (if available). |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
%(thread)d | Thread ID (if available). |
%(threadName)s | Thread name (if available). |
3. 时间格式
时间格式化字符串与time.strftime()使用相同的参数
参数 | 解释 |
---|---|
%a | Locale's abbreviated weekday name. |
%A | Locale's full weekday name. |
%b | Locale's abbreviated month name. |
%B | Locale's full month name. |
%c | Locale's appropriate date and time representation. |
%d | Day of the month as a decimal number [01,31]. |
%H | Hour (24-hour clock) as a decimal number [00,23]. |
%I | Hour (12-hour clock) as a decimal number [01,12]. |
%j | Day of the year as a decimal number [001,366]. |
%m | Month as a decimal number [01,12]. |
%M | Minute as a decimal number [00,59]. |
%p | Locale's equivalent of either AM or PM. |
%S | Second as a decimal number [00,61]. |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. |
%w | Weekday as a decimal number [0(Sunday),6]. |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. |
%x | Locale's appropriate date representation. |
%X | Locale's appropriate time representation. |
%y | Year without century as a decimal number [00,99]. |
%Y | Year with century as a decimal number. |
%Z | Time zone name (no characters if no time zone exists). |
%% | A literal '%' character. |
4. 另一种写法
麻烦一点,但是可以定制多个logger
import logging
logger = logging.getLogger(u'mylogger')
handler = logging.FileHandler(u'log1.txt')
formatter = logging.Formatter(u'时间:%(asctime)s\n级别:%(levelname)s\n消息:%(message)s\n')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug(u'第一条记录')
logger.info(u'第二条记录')
logger2 = logging.getLogger(u'mylogger2')
handler2 = logging.FileHandler(u'log2.txt')
formatter2 = logging.Formatter(u'时间:%(asctime)s\n级别:%(levelname)s\n消息:%(message)s\n')
handler2.setFormatter(formatter2)
logger2.addHandler(handler2)
logger2.setLevel(logging.DEBUG)
logger2.debug(u'第一条记录')
logger2.info(u'第二条记录')
logging 文件日志的更多相关文章
- .NET跨平台之旅:增加文件日志功能遇到的挫折
在将我们的ASP.NET 5示例站点(about.cnblogs.com)升级至ASP.NET 5 RC1的时候,我们增加了控制台日志功能. 在ASP.NET 5添加日志功能很简单,只需在projec ...
- 解决logging模块日志信息重复问题
解决logging模块日志信息重复问题 问题描述 相信大家都知道python的logging模块记录日志信息的步骤: # coding:utf-8 import logging ### 创建logge ...
- logging模板日志格式
logging模板日志格式 创建loginfo.py模块,然后导入定义的logging配置,即可使用 cat loginfo.py """ logging配置 " ...
- Python学习笔记:logging(日志处理)
在一个软件中,日志是可以说必不可少的一个组成部分,通常会在定位客户问题或者记录软件使用情况等场景中会用到.logging模板块是Python的一个内置标准库,用于实现对日志的控制输出,对于平常的日志输 ...
- 【java】java自带的java.util.logging.Logger日志功能
偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logg ...
- 分享 NET 5.x 自定义文件日志实现 原汁原味
下面直接贴出实现代码 FileLoggerProvider /// <summary> /// 文件记录器提供商 /// </summary> public class Fil ...
- Spring 使用 SLF4J代替 Commons Logging 写日志 异常
项目的日志更换成slf4j和logback后,发现项目无法启动.错误提示 Caused by: java.lang.NoClassDefFoundError: Lorg/apache/commons/ ...
- php 文件日志类
php文件日志类,按年月日组织目录结构. <?php class FileLog { private $_filepath; //文件路径 private $_filename; //日志文件名 ...
- 【分享】我们用了不到200行代码实现的文件日志系统,极佳的IO性能和高并发支持,附压力测试数据
很多项目都配置了日志记录的功能,但是,却只有很少的项目组会经常去看日志.原因就是日志文件生成规则设置不合理,将严重的错误日志跟普通的错误日志混在一起,分析起来很麻烦. 其实,我们想要的一个日志系统核心 ...
随机推荐
- 为什么 C++ 中成员函数指针是 16 字节?
当我们讨论指针时,通常假设它是一种可以用 void * 指针来表示的东西,在 x86_64 平台下是 8 个字节大小.例如,下面是来自 维基百科中关于 x86_64 的文章 的摘录: Pushes a ...
- svn cleanup 执行失败时,可以勾选 break locks,
- 做中学(Learning by Doing)之背单词-扇贝网推荐
做中学(Learning by Doing)之背单词-扇贝网推荐 看完杨贵福老师(博客,知乎专栏,豆瓣)的「继续背单词,8个月过去了」,我就有写这篇文章的冲动了,杨老师说: 有时候我会感觉非常后悔,如 ...
- 动态生成验证码———MVC版
上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- webpack构建vue项目(配置篇)
最近公司要求用vue重构项目,还涉及到模块化开发,于是乎,我专门花了几天的时间研究了一下webpack这个目前来看比较热门的模块加载兼打包工具,发现上手并不是很容易,现将总结的一些有关配置的心得分享出 ...
- 【BZOJ 1016】【JSOI 2008】最小生成树计数
http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...
- odoo种种
[精]Odoo 8.0深入浅出开发教程-模块开发基础 http://blog.csdn.net/sunansheng/article/details/50864527 搭建odoo开发调试环境 htt ...
- Android疑难杂症收集
在渲染前获取 View 的宽高 5种手势工具类 软键盘用法总结 Android中Shape的使用 Android只能动态注册的广播Action Android 悬浮窗权限各机型各系统适配大全 录音权限 ...
- 手机端html5触屏事件(touch事件)
touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...