码迷,mamicode.com
首页 > 其他好文 > 详细

logging 文件日志

时间:2014-07-22 00:20:36      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:blog   使用   strong   文件   io   for   

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. 级别

日志所记录的消息可以划分为不同的级别,一般用以下几种预定义的级别。

每种级别有对应的值,可以用来比较级别的高低。

  • logging.CRITICAL 50
  • logging.ERROR 40
  • logging.WARNING 30
  • logging.INFO 20
  • logging.DEBUG 10
  • logging.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]. All days in a new year preceding the first Sunday are considered to be in week 0.
  • %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]. All days in a new year preceding the first Monday are considered to be in week 0.
  • %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.

logging 文件日志,布布扣,bubuko.com

logging 文件日志

标签:blog   使用   strong   文件   io   for   

原文地址:http://www.cnblogs.com/iszero/p/3858204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!