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

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界别日志去掉,避免发送日志影响性能。
相关推荐
不悔哥4 小时前
小智AI聊天机器人:ESP32上的开源语音助手拆解
人工智能·机器人·开源
黑妹天下第一乖8 小时前
第 08 讲:阿加犀 AidGenSE 端侧大模型服务化与 OpenAI 兼容接口
人工智能·嵌入式硬件·深度学习·机器人·iot
李日华大战鸡红10 小时前
滑膜观测器(学习记录)
stm32·单片机·学习·matlab·机器人
feasibility.14 小时前
一台 2500 美元的开源双足人形平台 LeRobot Humanoid,正在让“教机器人走路”变成可复现的事
机器人·开源
考研保研资料分享15 小时前
自动化与机器人保研经验:四校夏令营笔面试、英语考核及复习规划(2025)
面试·机器人·自动化
zhangrelay1 天前
用ROS2Lyrical实验报告册思路重做蓝桥云课ROS1实验并改进
linux·笔记·学习·ubuntu·机器人
具身AGI1 天前
人类演示补成机器人域,触觉共训才成立
机器人
山顶夕景1 天前
【VLA】Qwen-VLA:VLM+DiT统一多模态理解与连续机器人动作生成
机器人·多模态·扩散模型·具身智能·vla·dit·vlm
mtouch3331 天前
视频与三维场景融合投射系统解析:从地图标定到镜头参数调校的完整流程
人工智能·机器人·虚拟现实·电子沙盘·数字沙盘
黑妹天下第一乖1 天前
第08讲 · 视觉与相机流水:Spectra ISP 与实时检测
人工智能·嵌入式硬件·数码相机·机器人·接口隔离原则·iot