LOG日志系统

# coding=utf-8
import datetime
import logging
import os
import sys
from logging.handlers import TimedRotatingFileHandler


class Logger(object):
    level_relations = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'critical': logging.CRITICAL
    }

    def __init__(self, name=None, date=None, filename=None, level='info', fmt='%(asctime)s [module:%(name)s][line:%(lineno)d] - %(levelname)s: %(message)s'):
        self.level = self.level_relations.get(level)
        fmt = logging.Formatter(fmt)
        path = './log'
        if not os.path.exists(path):
            os.makedirs(path)
        filename = path + '/record'
        self.logger = logging.getLogger(name)
        self.logger.setLevel(self.level)
        sh = logging.StreamHandler(sys.stdout)
        sh.setFormatter(fmt)
        sh.setLevel(self.level)

        # fh = logging.FileHandler(filename)
        # fh.setFormatter(fmt)
        # fh.setLevel(logging.INFO)

        fh = TimedRotatingFileHandler(filename, when="MIDNIGHT", backupCount=0)
        fh.setFormatter(fmt)
        fh.setLevel(logging.DEBUG)

        # self.logger.addHandler(sh)
        self.logger.addHandler(fh)

  

使用方法:

logging = Logger('arbitrager', level='info', date=datetime.date.today()).logger

 输出效果

2018-12-21 14:41:58,072 [module:arbitrager][line:106] - INFO: 余额:{'RMB': 0, 'BTC': 8.54261856644365, 'ETH': 0.0472080574819629, 'USDT': 47.34626115212543,}
2018-12-21 14:57:40,146 [module:arbitrager][line:106] - INFO: 余额:{'RMB': 0, 'BTC': 8.54261856644365, 'ETH': 0.0472080574819629, 'USDT': 47.34626115212543,}
2018-12-21 14:58:22,290 [module:arbitrager][line:106] - INFO: 余额:{'RMB': 0, 'BTC': 8.54261856644365, 'ETH': 0.0472080574819629, 'USDT': 47.34626115212543,}
2018-12-21 14:58:51,587 [module:arbitrager][line:106] - INFO: 余额:{'RMB': 0, 'BTC': 8.54261856644365, 'ETH': 0.0472080574819629, 'USDT': 47.34626115212543,}
2018-12-21 15:06:57,794 [module:arbitrager][line:106] - INFO: 余额:{'RMB': 0, 'BTC': 8.54261856644365, 'ETH': 0.0472080574819629, 'USDT': 47.34626115212543, }

  

 

posted @ 2019-01-09 13:43  Dream鑫  阅读(413)  评论(0编辑  收藏  举报