Home > Blockchain >  Best practice how to send Django app logs to phone
Best practice how to send Django app logs to phone

Time:09-03

I would like to send my logs from my Django app to my phone. I thought of using slack mail integration to send mails to a dedicated channel but this is only available in the paid slack version.

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.

Thanks and all the best

CodePudding user response:

You can look to 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.

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)

CodePudding user response:

Super - thank you!

Implemented the code with a custom handler 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)
  • Related