本篇讲py-logging.
日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET.
设置级别为INFO,开始测时输出时logging.info(‘hell0’),测完改为ogging.debug(‘hell0’)
0x1.logging.basicConfig(注意 # )
0x1.1 输出到console
import logging
logging.basicConfig(
level = logging.DEBUG, #不需要输出时改为INFO
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
#,filename='myapp.log',
#filemode='a+'
)
0x1.2 输出到File
logging.basicConfig(
level = logging.DEBUG, #不需要输出时改为INFO
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
,filename='myapp.log',
filemode='a+'
)
0x1.3 同时输出到console 和file.
*:因为有一个root,所以不能同时用下面代码配置方式.
logging.basicConfig(
level = logging.DEBUG, #不需要输出时改为INFO
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
#,filename='myapp.log',
#filemode='a+'
)
#指定log输出格式. logging有一个日志处理的主对象,其它处理方式都是通过addHandler添加进去的。
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
#输出到文件
file_handler= logging.FileHandler('myapp.log','a+') #FileHandler(filename, mode=’a’, encoding=None, delay=False)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)
logging.getLogger('').addHandler(file_handler)
##输出到console
#console_handler = logging.StreamHandler(sys.stdout)
#console_handler.setLevel(logging.DEBUG)
#console_handler.formatter = formatter
#logging.getLogger('').addHandler(console_handler)
0x2. logconfig.
0x2.1调用
import logging
import logging.config----需要导入
#--------------------文件配置----------
logfilePath = os.path.join(os.path.dirname(__file__), 'log\\logging.conf')
logging.config.fileConfig('log\\logging.conf')
logging.getLogger()
0x2.2 logging.conf(路径自己设置)
# logging.conf
[loggers]
keys=root
[logger_root]
level=DEBUG
handlers=consoleHandler,FileHandler ,errorTimedRotateFileHandler
#,timedRotateFileHandler
#################################################
[handlers]
keys=consoleHandler,timedRotateFileHandler,errorTimedRotateFileHandler,FileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[handler_timedRotateFileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('log\\debug.log', 'H')
[handler_errorTimedRotateFileHandler]
class=handlers.TimedRotatingFileHandler
level=WARN
formatter= multiLineFormatter
args=('log\\error.log', 'H')
[handler_FileHandler]
class=FileHandler
level=DEBUG
formatter = selfFormatter
args=('log\\myapp.log','a+')
#################################################
[formatters]
keys=simpleFormatter, multiLineFormatter,selfFormatter
[formatter_simpleFormatter]
format= %(levelname)s %(threadName)s %(asctime)s: %(message)s
datefmt=%H:%M:%S
[formatter_multiLineFormatter]
format= ------------------------- %(levelname)s -------------------------
Time: %(asctime)s
Thread: %(threadName)s
File: %(filename)s(line %(lineno)d)
Message:
%(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_selfFormatter]
format = %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
0x3 记录异常-带栈信息
# 记录异常信息
try:
1 / 0
except:
# 等同于error级别,但是会额外记录当前抛出的异常堆栈信息
logger.exception('this is an exception message')
------------------------- ERROR -------------------------
Time: 2017-09-17 11:04:02
Thread: MainThread
File: ObfuscateEng.py(line 152)
Message:
Eng has a exception info.
Traceback (most recent call last):
File "F:\AlreadyDoneForRecord\test\ObfuscateEng\oldVer\v0.94\ObfuscateEng.py", line 147, in Eng
1/0
ZeroDivisionError: division by zero