somepeople

python模块之logging

输出到console(默认打印warning级别以上(warnging、error、critical)的日志):

import logging
logging.debug('hello world') logging.warning('hello world') logging.error('hello world') #WARNING:root:hello world #ERROR:root:hello world

输出到文件:

import logging
logging.basicConfig(filename='log_test.log',level=logging.INFO)
logging.debug('helloworld')
logging.info('helloworld')
logging.warning('helloworld')
#INFO:root:helloworld
#WARNING:root:helloworld

输出到文件,设定格式:

import logging
logging.basicConfig(filename='log_test.log',level=logging.INFO,format='%(asctime)s %(message)s',datefmt='%m-%d-%Y %I:%M:%S %p')
logging.debug('helloworld')
logging.info('helloworld')
logging.warning('helloworld')

#08-02-2020 05:56:53 AM helloworld
#08-02-2020 05:56:53 AM helloworld

输出到文件,所有格式:

import logging
logging.basicConfig(filename='log_test.log',level=logging.INFO,format='%(asctime)s %(message)s %(levelno)s %(levelname)s %(pathname)s %(filename)s %(module)s %(funcName)s %(lineno)d %(created)f %(relativeCreated)d %(asctime)s '
                                                                      '%(thread)d %(threadName)s %(process)d )',datefmt='%m-%d-%Y %I:%M:%S %p')

logging.debug('helloworld')
logging.info('helloworld')
logging.warning('helloworld')
def test_log():
    logging.warning('func test')
test_log()
#08-02-2020 06:20:59 AM helloworld 20 INFO E:/pythondir/Day01/testsdsd.py testsdsd.py testsdsd <module> 36 1596320459.622015 2 08-02-2020 06:20:59 AM 18572 MainThread 19432)
#08-02-2020 06:20:59 AM helloworld 30 WARNING E:/pythondir/Day01/testsdsd.py testsdsd.py testsdsd <module> 37 1596320459.622015 2 08-02-2020 06:20:59 AM 18572 MainThread 19432)
#08-02-2020 06:20:59 AM func test 30 WARNING E:/pythondir/Day01/testsdsd.py testsdsd.py testsdsd test_log 39 1596320459.622015 2 08-02-2020 06:20:59 AM 18572 MainThread 19432)

格式说明:

 

 同时输出日志到console以及文本:

import logging
#1.生成logger对象
logger = logging.getLogger('Mysql')
#全局日志级别,(总过滤)
logger.setLevel(logging.DEBUG)

#2.生成handler对象
ch = logging.StreamHandler()
#局部日志级别,(次级过滤)
ch.setLevel(logging.INFO)
fh = logging.FileHandler(filename='log_test_logging')
#局部日志级别,(次级过滤)
fh.setLevel(logging.WARNING)

#2.1把handler对象绑定到logger
logger.addHandler(ch)
logger.addHandler(fh)

#3.生成format对象,并设定日志格式
file_formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s ')
console_formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s ')

#3.1把formatter对象绑定handler对象
ch.setFormatter(console_formatter)
fh.setFormatter(file_formatter)
#日志输出
logger.warning('warning test')
logger.debug('debug test')

 对日志进行过滤以及切割:

import logging
from logging import handlers
#创建过滤对象类
class IgnoreBackupLogFilter(logging.Filter):
    def filter(self, record):
        return "you" not in record.getMessage()

# 1.先生成logger
logger = logging.getLogger('beifen')
# logger.setLevel(logging.WARNING)
logger.addFilter(IgnoreBackupLogFilter())
# 2.生成handler对象
ch = logging.StreamHandler()
#普通文件对象
# fh = logging.FileHandler(filename='beifen_test')
#切割文件对象
fh = handlers.RotatingFileHandler(filename='beifen_test',maxBytes=10,backupCount=3 )
# 2.1把handler对象绑定到logger
logger.addHandler(ch)
logger.addHandler(fh)

# 3.生成formatter
ch_formatter = logging.Formatter('%(asctime)s %(message)s')
fh_formatter = logging.Formatter('%(asctime)s %(message)s')
# 3.1 把formatter绑定到handler
ch.setFormatter(ch_formatter)
fh.setFormatter(fh_formatter)

# 输出
logger.warning('hello you')
logger.critical('hha')

 

posted on 2020-08-02 10:15  Hello_Thanos  阅读(96)  评论(0编辑  收藏  举报

导航