官方教程: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. 通过logger命令记录日志

    通过logger命令记录日志 logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息. ------------------- ...

  2. US Open 2016 Contest

    比较弱,只写了金组和银组,铂金组的第一题. [262144] http://www.usaco.org/index.php?page=viewproblem2&cpid=648 给一个序列,相 ...

  3. [SCOI2013]摩托车交易 kruskal重构树(最大生成树) 倍增

    ---题面--- 题解: 这题想法简单,,,写起来真的是失智,找了几个小时的错误结果是inf没开到LL范围.... 首先我们需要找到任意两点之间能够携带黄金的上限值,因为是在经过的道路权值中取min, ...

  4. CodeForces 185A. Plant (矩阵快速幂)

    CodeForces 185A. Plant (矩阵快速幂) 题意分析 求解N年后,向上的三角形和向下的三角形的个数分别是多少.如图所示: N=0时只有一个向上的三角形,N=1时有3个向上的三角形,1 ...

  5. 1.61 三角形O(nlogn)做法

     书里给出比较无脑的做法,三个for循环复杂度是n的立方.如果先把数列排序,依次判断连续三个数是否能形成三角形,可以把时间复杂度控制在nlogn. #include<stdio.h> ...

  6. NOIP2017 列队——平衡树

    平衡树蒟蒻,敲了半天. 其实思路很简单,就是把许多个人合并成一个区间.必要的时候再拆开.(是不是和这个题的动态开点线段树有异曲同工之妙?) 每次操作最多多出来6个点. 理论上时间复杂度是nlogn,空 ...

  7. [python]爬站点

    #!/usr/bin/python 2 import urllib 3 import urllib2 4 import re 5 import os 6 7 dirs = ['js','img','p ...

  8. Codeforces Round #398 (Div. 2) B,C

    B. The Queue time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. HDU3524 数论

    Perfect Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. CSS图片宽度设置百分比 , 高度同宽度相同

    在图片长宽不相等的情况下,想将长宽设置为相等并且自适应屏幕,可以通过 js 的方式进行设置并通过监听 resize 来实时更新,但是这种方式很麻烦. 这里通过 css 来达到我们想要的效果: < ...