logging二次封装

import logging
import os
import time
from logging.handlers import  RotatingFileHandler


class Logger():
    logs_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '\\report\\logs'
    def __init__(self, name):
        self.logger = logging.getLogger(name)

		#设置级别,只有等于或高于设置的级别才会写入到文件中
        self.logger.setLevel(logging.INFO)

        #定义日志输出格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        # 创建一个handler,用于写入日志文件,按照日志大小自动切换
        timestamp = time.strftime('%Y%m%d')
        rotating_handler = RotatingFileHandler(
            filename=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '\\report\\logs', "{}.log".format(timestamp)),
            maxBytes=100 *1024 * 1024,
            encoding='utf-8')
        rotating_handler.setFormatter(formatter)

        # 创建一个handler,用于将日志输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        # 定义handler的输出格式
        ch.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(rotating_handler)
        self.logger.addHandler(ch)

    def debug(self, msg):
        self.logger.debug(msg)

    def info(self, msg):
        self.logger.info(msg)

    def warning(self, msg):
        self.logger.warning(msg)

    def error(self, msg):
        self.logger.error(msg)

    def critical(self, msg):
        self.logger.critical(msg)


# 使用Logger类
if __name__ == '__main__':
    logger = Logger('login')
    logger.info('This is  info message')
    logger.debug('This idebug message')
    logger.warning('This i warning message')
    logger.error('This is error message')
    logger.critical('This is critical message')


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值