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, }
    

      

  • 相关阅读:
    datagrid表格宽度设置成100%,宽度却不是100%,反而很窄
    Spring MVC 和Struts2对比
    在不知道用户名密码的情况下登录oracle数据库
    IE浏览器的兼容性问题
    在spring mvc 中读取配置文件(porperties)的信息
    godaddy设置方法 控制面板
    easyui datagrid 动态改变大小
    15 个最佳 jQuery 翻书效果插件
    windows7 如何关闭开机启动讲述人
    存储过程代码审核
  • 原文地址:https://www.cnblogs.com/Dreamxin/p/10243835.html
Copyright © 2011-2022 走看看