Collate output in Python logging MemoryHandler with SMTPHandler

前端 未结 5 1917
傲寒
傲寒 2020-12-01 00:14

I have the logging module MemoryHandler set up to queue debug and error messages for the SMTPHandler target. What I want is for an email to be sent when the process errors t

5条回答
  •  执念已碎
    2020-12-01 00:47

    For this purpose I use the BufferingSMTPHandler suggested by Vinay Sajip with one minor tweak: I set the buffer length to something really big (say 5000 log records) and manualy call the flush method of the handler every some seconds and after checking for internet conectivity.

    # init
    log_handler1 = BufferingSMTPHandler(
        'smtp.host.lala', "from@test.com", ['to@test.com'], 'Log event(s)',5000)
    ...
    logger.addHandler(log_handler1)
    ...
    
    # main code
    ...
    if internet_connection_ok and seconds_since_last_flush>60:
        log_handler1.flush() # send buffered log records (if any)
    

提交回复
热议问题