官方教程: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 (DEBUGINFOWARNINGERRORCRITICAL).
%(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的更多相关文章

  1. python logging详解及自动添加上下文信息

    之前写过一篇文章日志的艺术(The art of logging),提到了输出日志的时候记录上下文信息的重要性,我认为上下文信息包括: when:log事件发生的时间 where:log事件发生在哪个 ...

  2. 管理 python logging 日志使用

    1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上INFO:确认一切按预期运行WA ...

  3. python logging 日志使用

    https://docs.python.org/3/library/logging.html1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRI ...

  4. python logging模块可能会令人困惑的地方

    python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...

  5. python logging 配置

    python logging 配置 在python中,logging由logger,handler,filter,formater四个部分组成,logger是提供我们记录日志的方法:handler是让 ...

  6. Python LOGGING使用方法

    Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...

  7. python logging 日志轮转文件不删除问题

    前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...

  8. python logging模块使用

    近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...

  9. python Logging的使用

    日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分. 根据事件的轻重可分为以下几个级别: DEBUG: 详细信息, ...

随机推荐

  1. P1349 广义斐波那契数列

    题目描述 广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列.今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an除以m的余数. 输入输出格 ...

  2. [洛谷P1369]矩形

    题目大意:有$n(n\leqslant300)$个点,每个点坐标范围在$[1\sim100]$,求一个矩阵,使得边界上的点最多. 题解:做一遍二维前缀和,直接暴力枚举两个顶点 卡点:无 C++ Cod ...

  3. Android View 绘制刷新流程分析

    Android中对View的更新有很多种方式,使用时要区分不同的应用场合.1.不使用多线程和双缓冲      这种情况最简单,一般只是希望在View发生改变时对UI进行重绘.你只需显式地调用View对 ...

  4. 【BZOJ5288】[HNOI2018]游戏(乱搞?)

    [BZOJ5288][HNOI2018]游戏(乱搞?) 题面 BZOJ 洛谷 题面自己到洛谷上看把 题解 考场上乱搞拿到了\(90\)分,简直不敢相信. 回家把代码再交了一份直接就\(AC\)了??? ...

  5. BZOJ1037 [ZJOI2008]生日聚会Party 【DP】

    1037: [ZJOI2008]生日聚会Party Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2800  Solved: 1654 [Submi ...

  6. 创建JavaScript的哈希表Hashtable

    Hashtable是最常用的数据结构之一,但在JavaScript里没有各种数据结构对象.但是我们可以利用动态语言的一些特性来实现一些常用的数据结构和操作,这样可以使一些复杂的代码逻辑更清晰,也更符合 ...

  7. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. 题解【bzoj2427 [HAOI2010]软件安装】

    Description 现在我们的手头有\(N\)个软件,对于一个软件\(i\),它要占用\(W_i\)的磁盘空间,它的价值为\(V_i\).我们希望从中选择一些软件安装到一台磁盘容量为\(M\)计算 ...

  9. C#泛型实例详解

    本文以实例形式讲述了C#泛型的用法,有助于读者深入理解C#泛型的原理,具体分析如下: 首先需要明白什么时候使用泛型: 当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型. 一.针 ...

  10. LightOJ 1326 – Race 第二类Stirling数/

    简单的模板题. 题意:问n匹马出现的不同排名数. 题解:可以使用DP,本质上还是第二类Stirling数(隔板法) #include <stdio.h> #include <iost ...