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

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

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

级别
CRITICAL50
ERROR40
WARNING30
INFO20
DEBUG10
NOTSET0

每个级别都有对应的方法,用小写字母,比如 logging.debug() , logging.info(),分别用来记录 DEBUG 级别和 INFO 级别的消息。

logging.basicConfig 中配置的级别可以用来过滤消息,比配置级别低的消息将被忽略,不会写入文件。

比如,如果一开始配置的是 level=logging.INFO ,那么调用 logging.debug() 处理的消息将被忽略,不会记录到文件。只有用 info() 或者 warning() 以及更高级别才会被记录。

3. 日志格式

格式化字符串支持如下参数:

参数解释
%(asctime)sHuman-readable time when the LogRecord was created. By default this is of the form '2003-07-08 16:49:45,896'.
%(created)fTime when the LogRecord was created (as returned by time.time()).
%(filename)sFilename portion of pathname.
%(funcName)sName of function containing the logging call.
%(levelname)sText logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
%(lineno)dSource line number where the logging call was issued (if available).
%(module)sModule (name portion of filename).
%(msecs)dMillisecond portion of the time when the LogRecord was created.
%(message)sThe logged message.
%(name)sName of the logger used to log the call.
%(pathname)sFull pathname of the source file where the logging call was issued (if available).
%(process)dProcess ID (if available).
%(processName)sProcess name (if available).
%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(thread)dThread ID (if available).
%(threadName)sThread name (if available).

3. 时间格式

时间格式化字符串与time.strftime()使用相同的参数

参数解释
%aLocale's abbreviated weekday name.
%ALocale's full weekday name.
%bLocale's abbreviated month name.
%BLocale's full month name.
%cLocale's appropriate date and time representation.
%dDay of the month as a decimal number [01,31].
%HHour (24-hour clock) as a decimal number [00,23].
%IHour (12-hour clock) as a decimal number [01,12].
%jDay of the year as a decimal number [001,366].
%mMonth as a decimal number [01,12].
%MMinute as a decimal number [00,59].
%pLocale's equivalent of either AM or PM.
%SSecond as a decimal number [00,61].
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53].
%wWeekday as a decimal number [0(Sunday),6].
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53].
%xLocale's appropriate date representation.
%XLocale's appropriate time representation.
%yYear without century as a decimal number [00,99].
%YYear with century as a decimal number.
%ZTime 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'第二条记录')

转载于:https://www.cnblogs.com/iszero/p/3858204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值