Python logging封装是指将Python中logging模块的功能封装成简单易用的API,以便在实际应用中更方便地使用。
Python logging封装是指将Python中logging模块的功能封装成简单易用的API,以便在实际应用中更方便地使用。
是一个Python logging封装的示例代码:
# -*- coding:utf-8 -*-
import logging
class Logger(object):
def __init__(self, filename):
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
self.fh = logging.FileHandler(filename)
self.fh.setLevel(logging.INFO)
self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.fh.setFormatter(self.formatter)
self.logger.addHandler(self.fh)
def debug(self, message):
self.logger.debug(message)
def info(self, message):
self.logger.info(message)
def warning(self, message):
self.logger.warning(message)
def error(self, message):
self.logger.error(message)
def critical(self, message):
self.logger.critical(message)
if __name__ == '__main__':
logger = Logger('test.log')
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(4条)