Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
# coding:utf-8
import logging
import requests
import json
_session = requests.Session()
# APT KEY
_pushbullet = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
def pushbullet(title, sms):
headers = {"Content-Type": "application/json"}
post_data = {"type": "note",
"title": title,
"body": sms}
a = _session.post("https://api.pushbullet.com/v2/pushes",
auth=(_pushbullet, ""),
headers=headers,
data=json.dumps(post_data))
if "error" in a.text:
raise Exception(u"卧槽,粗大事了")
return True
class PushbulletHandler(logging.Handler):
def __init__(self):
super(PushbulletHandler, self).__init__()
self.setLevel(logging.CRITICAL)
# 懒得写 直接复制的format T_T
FORMAT = '%(asctime)-15s %(levelname)-6s %(message)s'
DATE_FORMAT = '%b %d %H:%M:%S'
formatter = logging.Formatter(fmt=FORMAT, datefmt=DATE_FORMAT)
self.setFormatter(formatter)
def emit(self, record):
pushbullet(u"某某脚本", self.format(record))
if __name__ == '__main__':
log = logging.getLogger(__name__)
log.addHandler(PushbulletHandler())
try:
1 / 0
except:
log.critical(u"紧急信息,请处理", exc_info=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.