Python的log的使用

53 篇文章 1 订阅

用法

self.logger = Logger('agent') # agent是应用名
self.logger.add_file_handler('agent') # 生成agent.log文件
try:
    self.logger.info('rollback block: %s | %s' % (str(db_block['height']), str(db_block['hash'])))
except Exception as e:
    self.logger.error('error: %s\nblock:\n%s\n' % (str(e), str(node_block)))
    raise Exception('error: %s', e)

一个封装好的Log类

import logging
import os
from logging.handlers import RotatingFileHandler


class Logger:
    def __init__(self, app):
        self.logger = logging.getLogger(app)
        self.logger.setLevel(logging.DEBUG)
        self.fmt = logging.Formatter(
            '%(asctime)s %(levelname)s <%(name)s>: %(message)s '
            '[in %(pathname)s:%(lineno)d]')
        self.app = app

    def add_stream_handler(self, clevel = logging.DEBUG):
        sh = logging.StreamHandler()
        sh.setLevel(clevel)
        sh.setFormatter(self.fmt)
        self.logger.addHandler(sh)

    def add_file_handler(self, file_name=None, flevel=logging.DEBUG):
        file_name = self.app if file_name is None else file_name
        if not os.path.exists('./logs/'):
            os.mkdir('./logs/')
        log_path = os.path.join('./logs', '%s.log' % file_name)
        fh = RotatingFileHandler(log_path, maxBytes=100000, backupCount=10)
        fh.setLevel(flevel)
        fh.setFormatter(self.fmt)
        self.logger.addHandler(fh)

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

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

    def warn(self, message):
        self.logger.warn(message)

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

    def cri(self, message):
        self.logger.critical(message)


if __name__ == '__main__':
    logyyx = Logger('test')
    logyyx.add_stream_handler()
    logyyx.add_file_handler('my_log_name')
    logyyx.debug('a bug message')
    logyyx.info('an info message')
    logyyx.warn('a warning message')
    logyyx.error('a error message')
    logyyx.cri('a critical message')

终端输出:

2018-05-18 13:16:41,756 DEBUG <test>: a bug message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:32]
2018-05-18 13:16:41,756 INFO <test>: an info message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:35]
2018-05-18 13:16:41,756 WARNING <test>: a warning message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:38]
2018-05-18 13:16:41,756 ERROR <test>: a error message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:41]
2018-05-18 13:16:41,756 CRITICAL <test>: a critical message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:44]

在./logs目录下生成log文件 my_log_name.log m y _ l o g _ n a m e . l o g

2018-05-18 13:16:41,756 DEBUG <test>: a bug message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:32]
2018-05-18 13:16:41,756 INFO <test>: an info message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:35]
2018-05-18 13:16:41,756 WARNING <test>: a warning message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:38]
2018-05-18 13:16:41,756 ERROR <test>: a error message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:41]
2018-05-18 13:16:41,756 CRITICAL <test>: a critical message [in C:/Users/doubl/btmscan_log/btmscan/collector/agent/log.py:44]

参数配置说明

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值