简体   繁体   English

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

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

I would like to send my logs from my Django app to my phone.我想将我的日志从我的 Django 应用程序发送到我的手机。 I thought of using slack mail integration to send mails to a dedicated channel but this is only available in the paid slack version.我曾想过使用 Slack 邮件集成将邮件发送到专用频道,但这仅在付费 Slack 版本中可用。

Do you have any ideas with what kind of setup I could achieve this?您对我可以实现的设置有什么想法吗? I don't want to use plain mail since this is too spammy... Discord is also not working well with the webhook since it only pushed all 30 mins.我不想使用普通邮件,因为这太垃圾邮件了...... Discord 也不能很好地与 webhook 一起使用,因为它只推送了所有 30 分钟。

Thanks and all the best谢谢,一切顺利

You can look to Telegram Bot .您可以查看Telegram Bot It's pretty simple to send messages with it.用它发送消息非常简单。

All you need is register bot, add it to the Telegram group in which he should send messages and send GET requests when you need.您只需要注册机器人,将其添加到电报组中,他应该在其中发送消息并在需要时发送 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)

Super - thank you!超级 - 谢谢!

Implemented the code with a custom handler class:使用自定义处理程序 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