flaskのコンソールに出るログをlogging使ってDiscordにWebhookで送る
これを
https://gyazo.com/d0b08eaabf2d5555c27409131bd2d5ef
こうする手順
https://gyazo.com/cfc0910b43f1176b2eb80c519757c714
code:handlers.py
import logging
import requests
class DiscordHandler(logging.StreamHandler):
def __init__(self, url):
super(DiscordHandler, self).__init__()
self.url = url
def emit(self, record):
msg = self.format(record)
self.send_message(msg)
def send_message(self, text):
message = {
'content': text,
}
requests.post(self.url, json=message)
code:main.py
from flask import Flask
import logging
from handlers import DiscordHandler
app = Flask(__name__)
# Discord Handler の作成
discord_handler = DiscordHandler(webhook_url)
discord_handler.setLevel(logging.INFO)
werkzeug_logger = logging.getLogger('werkzeug')
werkzeug_logger.addHandler(discord_handler)
# 標準出力要らないよって人はこの辺削除
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
werkzeug_logger.addHandler(console_handler)
if __name__ == '__main__':
app.run()
これを実際に実装したやつ
参考