码迷,mamicode.com
首页 > 其他好文 > 详细

logging

时间:2019-07-20 00:14:02      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:back   mha   lse   utf-8   param   art   hid   turn   ret   

 

技术图片
# -*- coding:utf-8 -*-
import os
import logging
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler


class get_log():
    def __init__(self):
        self.loglevel = logging.INFO
        self.logleve2 = logging.WARNING
        self.logleve3 = logging.ERROR
        self.logleve4 = logging.CRITICAL
        self.logleve5 = logging.DEBUG
        self.logleve6 = logging.NOTSET

    def config_log(self, filename=None):
        logger = logging.getLogger(__name__)
        if not bool(filename):
            return self.config_stream_log(logger)
        else:
            return self.config_file_log(logger, filename)

    def config_file_log(self, logger, filename):
        formatter = logging.Formatter(
            (%(asctime)s  %(pathname)s %(levelname)s 第%(lineno)d行
              %(message)s))
        logger.setLevel(self.loglevel)
        ch = logging.StreamHandler()
        ch.setLevel(self.logleve3)
        ch.setFormatter(formatter)
        logger.addHandler(ch)
        ‘‘‘定义文件流‘‘‘
        # fh = TimedRotatingFileHandler(filename=filename, when=‘s‘, interval=1)
        fh = RotatingFileHandler(filename=filename, maxBytes=512*1024*1024,backupCount=20)
        fh.setLevel(self.loglevel)
        fh.setFormatter(formatter)
        # fh.suffix = ‘.log‘
        # fh.suffix = ‘%Y%m%d-%H%M.log‘
        # fh.suffix = ‘%Y-%m-%d %H-%M-%S.log‘
        logger.addHandler(fh)
        return logger

    def config_stream_log(self, logger):
        handler = logging.StreamHandler()
        formatter = logging.Formatter(%(pathname)s--第%(lineno)d行--%(levelname)s--%(message)s)
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        logger.setLevel(self.loglevel)
        return logger

    def get_filesize(path: str):
        ‘‘‘获取文件的大小字节‘‘‘
        file_size = os.stat(path)
        return file_size.st_size
View Code

 

技术图片
import recorder
logger=recorder.get_log().config_log(./logs/response.log)

logger.info(analysis_data=%s % analysis_data)
logger.info(start apply_form-save:product_id=%s % (name))
logger.info(
            end apply_form:product_id=%s analysis_data=%s  % (name, analysis_data))
logger.info(
            end apply_form:product_id=%s parameters=%s  % (name, json.loads(parameters)))
View Code

 

logging

标签:back   mha   lse   utf-8   param   art   hid   turn   ret   

原文地址:https://www.cnblogs.com/xujinjin18/p/11216203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!