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性能和高并发支持,附压力测试数据
很多项目都配置了日志记录的功能,但是,却只有很少的项目组会经常去看日志.原因就是日志文件生成规则设置不合理,将严重的错误日志跟普通的错误日志混在一起,分析起来很麻烦. 其实,我们想要的一个日志系统核心 ...
随机推荐
- 【JS/CSS3】实现带预览图幻灯片效果~
一.前期准备 1.1 案例分析 适用场景:单例布局1.2 方法论 V视图 HTML+CSS+调试C js实现控制流程D数据 优化扩展 二.代码 结构 <div class="slide ...
- LAMP环境配置 linux+apache+mysql+php
虚拟机安装Linux系统: 新建虚拟机过程中选择Linux,下面选择centos或者是Ubuntu Linux切换图像命令:注意只有装了图像界面才可以切换 查看安装环境的版本: rpm -qa 查看安 ...
- 前端之jquery
前端之jquery 本节内容 jquery简介 选择器和筛选器 操作元素 示例 1. jquery简介 1 jquery是什么 jQuery由美国人John Resig创建,至今已吸引了来自世界各地的 ...
- 关于js解析的一点小问题
先来看一下下面的一段代码有什么问题? <html><head><script src="./jquery.min.js"></script ...
- Less的使用
Less为css预处理器 首先先去下载一个koala编译器软件,然后运行打开 新建一个项目目录,在css文件夹中新建一个后缀为less的文件,然后拉入koala软件中,然后运行 ,你的css文件夹中就 ...
- react-redux原理分析
写在前面 之前写了一篇分析Redux中Store实现的文章(详见:Redux原理(一):Store实现分析),突然意识到,其实React与Redux并没有什么直接的联系.Redux作为一个通用模块,主 ...
- neo4j初次使用学习简单操作-cypher语言使用
Neo4j 使用cypher语言进行操作 Cypher语言是在学习Neo4j时用到数据库操作语言(DML),涵盖对图数据的增删改查 neo4j数据库简单除暴理解的概念: Neo4j中不存在表的概念, ...
- Fragment完全解析
Android Fragment 的使用,一些你不可不知的注意事项 Fragment全解析系列(一):那些年踩过的坑 Fragment全解析系列(二):正确的使用姿势 Fragment之我的解决方案: ...
- 下拉框-ComboBox
<ComboBox Name="cbBox" SelectionChanged="cbBox_SelectionChanged"></Comb ...
- Python 随机数用法
1. random.seed(int) 给随机数对象一个种子值,用于产生随机序列. 对于同一个种子值的输入,之后产生的随机数序列也一样. 通常是把时间秒数等变化值作为种子值,达到每次运行产生的随机系列 ...