为什么我们要使用logging模块来代替print函数记录日志呢?因为使用模块可以更好的管理我们的日志,并且可以将标准输入输出保存到日志文件,而且利用logging模块可以部分代替debug的功能,给程序排错。
默认情况分为6个级别:
NOTSET : 0
DEBUG : 10
INFO:20
WARNING : 30
ERROR : 40
CRITICAL :50
默认级别:WARNING
打开python console
import logging
logging.NOTSET
0
logging.DEBUG
10
logging.INFO
20
logging.WARNING
30
logging.ERROR
40
logging.CRITICAL
50
logging.debug('debug messgae')
logging.info('info messgae')
logging.warning('warning messgae')
WARNING:root:warning messgae
logging.error('error messgae')
ERROR:root:error messgae
logging.critical('critical message')
CRITICAL:root:critical message
由运行结果也可得知logging默认等级为warning
filename:用指定文件名创建filedHandler
filemode: 文件打开方式 默认:’a’ 可为:’w’
format:指定handler使用的日志显示格式
datefmt:指定日期时间格式
level:设置rootlogger的日志级别
stream:用指定的stream创建streamHandler。可以输出到sys.stderr,sys.stdout或者文件 。默认为sys.stderr,若参数filename stream同时存在,则stream会被忽略
编写demo.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Created by fengyuwusong at 2018/1/14
__author__ = 'fengyuwusong'
import logging
class TestLogging(object):
def __init__(self):
logFormat = '%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s'
logFileName = './testLog.txt'
logging.basicConfig(level=logging.INFO,
format=logFormat,
filename=logFileName,
filemode='w')
logging.debug('debug messgae')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
if __name__ == '__main__':
TestLogging()
得到结果文本testLog.txt:
2018-01-14 17:04:06,242 INFO root info message
2018-01-14 17:04:06,243 WARNING root warning message
2018-01-14 17:04:06,243 ERROR root error message
2018-01-14 17:04:06,243 CRITICAL root critical message
myLog.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Created by fengyuwusong at 2018/1/14
__author__ = 'fengyuwusong'
import logging
import getpass
import sys
# 定义MyLog类
class MyLog(object):
'''这个类用于创建一个自用的log'''
def __init__(self):
user = getpass.getuser()
self.logger = logging.getLogger(user)
self.logger.setLevel(logging.DEBUG)
logFile = './' + sys.argv[0][0:-3] + '.log'
formatter = logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s')
'''日志显示到屏幕上并输出到日志文件内'''
logHand = logging.FileHandler(logFile)
logHand.setFormatter(formatter)
logHand.setLevel(logging.ERROR) # 只有错误才会被记录到logfile中
logHandSt = logging.StreamHandler()
logHandSt.setFormatter(formatter)
self.logger.addHandler(logHand)
self.logger.addHandler(logHandSt)
'''日志5个等级对应5个函数'''
def debug(self, msg):
self.logger.debug(msg)
def info(self, msg):
self.logger.info(msg)
def error(self, msg):
self.logger.error(msg)
def warn(self, msg):
self.logger.warn(msg)
def critical(self, msg):
self.logger.critical(msg)
if __name__ == '__main__':
mylog = MyLog()
mylog.debug("I'm debug")
mylog.info("I'm info")
mylog.warn("I'm warn")
mylog.error("I'm error")
mylog.critical("I'm critical")
终端运行python myLog.py
得到结果
2018-01-14 17:17:29,850 DEBUG fengyuwusong I’m debug
2018-01-14 17:17:29,851 INFO fengyuwusong I’m info
2018-01-14 17:17:29,851 WARNING fengyuwusong I’m warn
2018-01-14 17:17:29,852 ERROR fengyuwusong I’m error
2018-01-14 17:17:29,852 CRITICAL fengyuwusong I’m critical
和日志文件mylog.log
2018-01-14 17:17:29,852 ERROR fengyuwusong I’m error
2018-01-14 17:17:29,852 CRITICAL fengyuwusong I’m critical
testMyLog.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Created by fengyuwusong at 2018/1/14
__author__ = 'fengyuwusong'
from myLog import MyLog
if __name__ == '__main__':
mylog = MyLog('testMylog')
mylog.debug("I'm debug")
mylog.info("I'm info")
mylog.warn("I'm warn")
mylog.error("I'm error")
mylog.critical("I'm critical")
运行结果基本同上.