【实用工具】使用飞书机器人监控工程日志

1.创建一个飞书群聊,设置-->群机器人-->添加机器人-->自定义机器人-->修改机器人名称-->添加

2.复制webhook地址

3.编写日志请求代码

python 复制代码
import logging
import requests
import json
import os
from datetime import datetime
import time
import subprocess

# curl -X POST -H "Content-Type: application/json" \
#     -d '{"msg_type":"text","content":{"text":"request example"}}' \
#     webhook地址


def get_local_ip():
    try:
        # 执行系统命令获取本地 IP 地址
        result = subprocess.check_output(['ip', 'addr']).decode('utf-8')
        # 解析输出,找到本地 IP 地址
        lines = result.split('\n')
        for line in lines:
            if 'inet ' in line and '127.0.0.1' not in line:
                local_ip = line.strip().split(' ')[1]
                return local_ip.split('/')[0]  # 返回不带掩码的 IP 地址
        return None
    except Exception as e:
        print("无法获取本地IP地址:", e)

class send_mes:
    def __init__(self):
        self.data = {
            "msg_type": "text",
            "content": {
                # "text": "pid:%s,ip_addr:%s" % (current_process_id, ip_addr[0])
                "text": ""
            }
        }
        self.url = "webhook地址"
        self.headers = {
            "Content-Type": "application/json"
        }
        self.pid = os.getpid()
        self.ip_addr = get_local_ip()



    # sef send_message()
    def send_message(self, data):

        current_time = datetime.now()

        # 将当前时间转换为字符串格式
        current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S")

        data_li = [current_time_str, self.ip_addr, self.pid, data]

        data_li_str = [str(item) for item in data_li]

        self.log_ = "|".join(data_li_str)

        self.data["content"]["text"] = self.log_

        try:
            response = requests.post(self.url, json=self.data, headers=self.headers)
            if response.status_code == 200:
                print("消息发送成功:", response.json())
            else:
                print("消息发送失败:", response.text)
        except Exception as e:
            print("发送请求时出错:", e)
            
if __name__ == "__main__":
    #实例化发送信息类
    send_mes_ = send_mes()
    #发送消息
    send_mes_.send_message("这是一条日志消息")

4.测试

日志信息将包括日期、执行程序的进程和ip,最后加上日志信息

  • 将error,info,debug放在三个机器人上,平时可以查看,等到真正使用时,把info和debug界别日志去掉,避免发送日志影响性能。
相关推荐
wechatbot8884 小时前
企业微信外部群聊自动回复机器人开发实战
微信·机器人·企业微信·企业微信scrm·企业微信二次开发·企业微信api
wangxiaoxuan21721 小时前
如何设计机器人 API:类型、单位、坐标系与失败语义
机器人·科沃斯八界
深蓝学院1 天前
机器人控制算法 MPC 的预测模型从哪来?完整公式推导
机器人·强化学习·机器人控制
蓝天居士1 天前
ROS2从入门到精通(13)
机器人·ros·ros 2
蓝天居士1 天前
JY61P认识、学习与实战(3)
机器人·嵌入式·传感器
feasibility.1 天前
399美元桌面机器人Doly:可爱电子宠物+树莓派开源平台,但不能像ChatGPT那样聊天
机器人·开源·嵌入式·树莓派·diy·桌宠
民乐团扒谱机2 天前
Rakazo 智能清洁机器人深度评测报告
机器人
feasibility.2 天前
英伟达把物理AI的3D模型“应用商店”开放了:2575个SimReady仿真资产,总计约472GB,可让agent装配机器人训练场景
人工智能·3d·机器人·仿真·建模·具身智能·英伟达
wangxiaoxuan2172 天前
机器人软件如何分层:驱动、算法、能力、动作与业务
机器人·科沃斯八界
kyle~2 天前
模型预测控制(MPC)
算法·机器人·控制·传感器