zoukankan      html  css  js  c++  java
  • 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, }
    

      

  • 相关阅读:
    [Leetcode] Combination Sum II
    [Leetcode] Search in Rotated Sorted Array
    [LeetCode] Number of 1 Bits
    [Jobdu] 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)
    [Jobdu] 题目1377:缓变序列
    [LeetCode] Add Two Numbers
    [LeetCode] Repeated DNA Sequences
    [LeetCode] Reverse Bits
    [Jobdu] 题目1139:最大子矩阵
    [LeetCode] Clone Graph
  • 原文地址:https://www.cnblogs.com/Dreamxin/p/10243835.html
Copyright © 2011-2022 走看看