Python logger Handle Example

起因:
   1)板卡上的程序有时需要上报一些消息给上层平台。
   2)异常可能来自于程序中许多函数处。如果在调度过程完成,相对因难,例如,超时,就不在主调度流程中。
   3)相对简单的办法是在自定义的Exception的子类中(例如JobError)来实现。
   4)但这个想法不现实之处在于,无法得到当前的job的ID。如果没有Job ID,自然无法上报。
   5)如果改变所有的自定义的Exception,加入job_id这样的变量,有几个困难,一是难以保证都会被赋值,二是会破坏原有的设计,因为原设计认为Exception的扩展,并不属于业务体系,而是公共部分。
   5) 仔细分析python的代码后,发现python的logger有handler的功能。该功能可以解决这一系列问题。即种用hook来解决。

示例如下:


import suds
import datetime

import datetime
import logging

from suds.client import Client


wsdl_url = "http://127.0.0.1:9005/?wsdl"


class CcmLoggerHandler(logging.Handler):
    def __init__(self,  job_id ):
        super().__init__()

        self.job_id = str(job_id)
        self.formatter = logging.Formatter("%(message)s")

    def emit(self, record):

        msg = str(self.job_id) + (self.formatter.format(record))
        if (record.levelno >= 40):
            #print("ok")
            return

        #print(msg)
​​​​​​​        client = Client(wsdl_url)
        result = client.service.add_lava_raise_error2report(self.job_id,msg)



    def close(self, linger=-1):
        # If the process crashes really early, the handler will be closed
        # directly by the logging module. In this case, close is called without
        # any arguments.
        super().close()

class Job:
    """
    Populated by the parser, the Job contains all of the
    Actions and their pipelines.
    parameters provides the immutable data about this job:
        action_timeout
        job_name
        priority
        device_type (mapped to target by scheduler)
        logging_level
        job_timeout
    Job also provides the primary access to the Device.
    The NewDevice class only loads the specific configuration of the
    device for this job - one job, one device.
    """

    def __init__(self, job_id):
        self.job_id = job_id
        self.logger = logging.getLogger("mydis")
        self.logger.addHandler(CcmLoggerHandler(job_id))
    def dosomethg(self):
        try:
            self.logger.info("erery ok.")
            b=4/0
        except Exception as exc:
            self.logger.error("Unable to setup the protocols")
            pass
    def dos2(self):
        try:
            b=4/0
        except Exception as exc:
            self.logger.error("new erroe dos2222")
            pass

def main():
    job=Job(99)
    job.dosomethg()
    job.dos2()

    return 0


if __name__ == "__main__":
    main()

解读:关键有如下几点:

1. 初始化和构造过程。

2.注入hook的过程。

3. 注意log level

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值