繁体   English   中英

最佳实践如何将 Django 应用程序日志发送到手机

[英]Best practice how to send Django app logs to phone

我想将我的日志从我的 Django 应用程序发送到我的手机。 我曾想过使用 Slack 邮件集成将邮件发送到专用频道,但这仅在付费 Slack 版本中可用。

您对我可以实现的设置有什么想法吗? 我不想使用普通邮件,因为这太垃圾邮件了...... Discord 也不能很好地与 webhook 一起使用,因为它只推送了所有 30 分钟。

谢谢,一切顺利

您可以查看Telegram Bot 用它发送消息非常简单。

您只需要注册机器人,将其添加到电报组中,他应该在其中发送消息并在需要时发送 GET 请求。

import requests

url = f"https://api.telegram.org/bot{your_bot_token}/sendMessage?chat_id= 
{your_group_with_bot_id}&parse_mode=Markdown&text={your_message}"
requests.get(url)

超级 - 谢谢!

使用自定义处理程序 class 实现代码:

import requests
import logging.handlers

##### CUSTOM LOGGING

class TelegramHTTPHandler(logging.Handler):
    def __init__(self, your_bot_token = "", your_group_with_bot_id = ""):
        '''
        Initializes the custom telegram handler
        Parameters:

        '''
        self.your_bot_token = your_bot_token
        self.your_group_with_bot_id = your_group_with_bot_id

        super().__init__()

    def emit(self, record):
        # this is called multiple times. 


        your_message = str(self.format(record))

        url = f"https://api.telegram.org/bot{self.your_bot_token}/sendMessage?chat_id={self.your_group_with_bot_id}&parse_mode=Markdown&text={your_message}"
        requests.get(url)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM