python logging system
官方教程:https://docs.python.org/2/library/logging.html
1. 用法1
import logging
import logging.handlers LOG_FILE = 'tst.log' handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s' formatter = logging.Formatter(fmt) # 实例化formatter
handler.setFormatter(formatter) # 为handler添加formatter logger = logging.getLogger('tst') # 获取名为tst的logger
logger.addHandler(handler) # 为logger添加handler
logger.setLevel(logging.DEBUG) logger.info('first info message')
logger.debug('first debug message')
formatter
采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:
| Format | Description |
|---|---|
| %(name)s | Name of the logger (logging channel). |
| %(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
| %(levelname)s | Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). |
| %(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
| %(filename)s | Filename portion of pathname. |
| %(module)s | Module (name portion of filename). |
| %(funcName)s | Name of function containing the logging call. |
| %(lineno)d | Source line number where the logging call was issued (if available). |
| %(created)f | Time when the LogRecord was created (as returned by time.time()). |
| %(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
| %(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time). |
| %(msecs)d | Millisecond portion of the time when the LogRecord was created. |
| %(thread)d | Thread ID (if available). |
| %(threadName)s | Thread name (if available). |
| %(process)d | Process ID (if available). |
| %(message)s | The logged message, computed as msg % args. |
level
The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost.
| Level | Numeric value |
|---|---|
| CRITICAL | 50 |
| ERROR | 40 |
| WARNING | 30 |
| INFO | 20 |
| DEBUG | 10 |
| NOTSET | 0 |
如果指定level 为INFO, 则小于它的所有message都不会打印出来。
2. 用法2
采用配置文件的方式去配置logging system, 这种方法简单方便,可以在多模块间调用而不需要传入logger对象。
现在我准备一个配置文件logging.conf如下
[loggers]
keys=root,uploader,msgChanger # [handlers]
keys=consoleHandler,uploaderHandler,msgChangerHandler [formatters]
keys=fmt [logger_root]
level=INFO
handlers=consoleHandler
qualname=root [logger_uploader]
level=INFO
qualname=uploader
handlers=uploaderHandler [logger_msgChanger]
level=INFO
qualname=msgChanger
handlers=msgChangerHandler [handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=fmt
args=(sys.stdout,) [handler_uploaderHandler]
class=logging.handlers.RotatingFileHandler
level=INFO
formatter=fmt
args=('log/uploader.log','w',1024*1024*1024,5,) [handler_msgChangerHandler]
class=logging.handlers.RotatingFileHandler
level=INFO
formatter=fmt
args=('log/msg_changer.log','w',1024*1024*1024,5,) [formatter_fmt]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
called in code:
'''init log module'''
logging.config.fileConfig('config/logging.conf') # parse config file as above
msgChanger_logger = logging.getLogger('msgChanger.main') # get a logger instance, msgChanger must be included
msgChanger_logger.info('test log system in main.')
可以在任何module下写入上述代码调用logger, 只需要传入一个名字就好了,名字必须在config文件里配置了的,否则实例失败。
python logging system的更多相关文章
- python logging详解及自动添加上下文信息
之前写过一篇文章日志的艺术(The art of logging),提到了输出日志的时候记录上下文信息的重要性,我认为上下文信息包括: when:log事件发生的时间 where:log事件发生在哪个 ...
- 管理 python logging 日志使用
1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上INFO:确认一切按预期运行WA ...
- python logging 日志使用
https://docs.python.org/3/library/logging.html1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRI ...
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
- python logging 配置
python logging 配置 在python中,logging由logger,handler,filter,formater四个部分组成,logger是提供我们记录日志的方法:handler是让 ...
- Python LOGGING使用方法
Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...
- python logging 日志轮转文件不删除问题
前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...
- python logging模块使用
近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...
- python Logging的使用
日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分. 根据事件的轻重可分为以下几个级别: DEBUG: 详细信息, ...
随机推荐
- HDFS文件操作命令手册
HDFS文件操作的基本格式是: bin/hadoop dfs -cmd <args> 1. cat $ hadoop dfs -cat URI [URI …] #将参数所指示的文件的内容输 ...
- JS执行上下文(执行环境)详细图解
JS执行上下文(执行环境)详细图解 先随便放张图 我们在JS学习初期或者面试的时候常常会遇到考核变量提升的思考题.比如先来一个简单一点的. console.log(a); // 这里会打印出什么? v ...
- bzoj2165: 大楼 (矩阵快速幂)
//========================== 蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/ 转载要声明! //=============== ...
- iOS图片上传后被旋转的问题
最近用PHP做了一个图片合成程序,前端是通过HTML的file input选取自定图片,POST到php后台调整尺寸后与事先准备好的背景图进行合成. 通过测试发现,上传后的自定图片有的被旋转了,有的是 ...
- Unresolved import *** (models) error on Eclipse
Eclipse version: Oxygen.2 Release (4.7.2) Python version: 3.6 问题:系统提示:from django.db import models 语 ...
- 使用树莓派录音——USB声卡
原文链接:http://jingyan.eeboard.com/article/73723 我们都知道树莓派只有音频输出口,而没有音频输入的接口,怎么办呢?其实只要一个USB声卡就可以了. USB声卡 ...
- BZOJ1898: [Zjoi2005]Swamp 沼泽鳄鱼(矩阵乘法)
1898: [Zjoi2005]Swamp 沼泽鳄鱼 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1898 Description 潘塔 ...
- mybatis中association和collection的column传入多个参数值
在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map
- Android实现自动定位城市并获取天气信息
定位实现代码: <span style="font-size:14px;">import java.io.IOException; import java.util.L ...
- Struts2入门(1)-第一个Struts2程序
目录结构 C:\WorkSpace\java\StrutsTest\src\main C:\WorkSpace\java\StrutsTest\src\test C:\WorkSpace\java\S ...