Skip to content

日志与异常记录

搭建本地 sentry

选择稳定的 onpremise 版本进行 clone,例如https://github.com/getsentry/onpremise/tree/releases/20.7.2,尽量不要直接使用 master

首先配置sentry/config.yml中的邮箱;

1
2
3
4
5
6
7
mail.host: 'smtp.qq.com'
mail.port: 465
mail.username: '954241552@qq.com'
mail.password: '获取的授权码'
# mail.use-tls: false
# The email address to send on behalf of
mail.from: '954241552@qq.com'

onpremise 根目录执行./install.sh
安装过程需要输入登录时需要的帐号和密码

安装完之后 onpremise 根目录执行:docker-compose up -d
所有容器启动后,稍等几分钟,访问 http://127.0.0.1:9000/ 就可以看到页面,用刚才输入的帐号密码进行登录

停止容器 onpremise 根目录执行:docker-compose stop
删除容器 onpremise 根目录执行:docker-compose rm

sentry 使用

可以创建一个 python 项目

在 设置-》项目 -》 选择刚才创建的项目 -》 点击 Client Keys (DSN) 查看
假设 DSN 为 http://87c1faccb9e645c6baaaead091bb1dca@localhost:9000/2

首先使用 pip 安装sentry-sdk

1
pip install --upgrade sentry-sdk==0.16.2

在代码中使用上面的 DSN

1
2
3
4
5
import sentry_sdk

sentry_sdk.init("http://87c1faccb9e645c6baaaead091bb1dca@localhost:9000/2")

b = 1 / 0

执行完成后,用不了多久就能在“问题(issue)”里面看到这个错误了

钉钉

用Python开发钉钉群机器人

自定义机器人接入

https://github.com/zhuifengshen/DingtalkChatbot

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import base64
import hashlib
import hmac
import json
import time
import urllib.parse

import requests


def ding(msg):
    timestamp = str(round(time.time() * 1000))
    secret = "SEC60d23018b74174292c23a4944dad043e0009e7f1efbc1c619eb23440fb1c7a13"
    secret_enc = secret.encode("utf-8")
    string_to_sign = "{}\n{}".format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode("utf-8")
    hmac_code = hmac.new(
        secret_enc, string_to_sign_enc, digestmod=hashlib.sha256
    ).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    print(timestamp)
    print(sign)

    headers = {"Content-Type": "application/json"}  # 定义数据类型
    webhook = (
        "https://oapi.dingtalk.com/robot/send?access_token=f7a5be4639fa488db06d89daf02922485e23a63aecbb3b8992abd851f503a83&timestamp="
        + timestamp
        + "&sign="
        + sign
    )
    # 定义要发送的数据
    data = {
        "msgtype": "text",
        "text": {"content": msg},
        "at": {"atMobiles": ["xxx"], "isAtAll": False},
    }
    res = requests.post(webhook, data=json.dumps(data), headers=headers)  # 发送post请求
    print(res.text)