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

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界别日志去掉,避免发送日志影响性能。
相关推荐
万俟淋曦40 分钟前
【论文速递】2025年第51周(Dec-14-20)(Robotics/Embodied AI/LLM)
人工智能·深度学习·机器人·大模型·论文·robotics·具身智能
WWZZ20251 小时前
SLAM进阶——数据集
人工智能·计算机视觉·机器人·大模型·slam·具身智能
才兄说1 小时前
机器人租来了,谁来教怎么用?
机器人
xwz小王子1 小时前
TRO 基于光纤触觉传感的多指欠驱动机器人手及其远程操控研究
机器人·欠驱动
万俟淋曦1 小时前
【论文速递】2025年第49周(Nov-30-Dec-06)(Robotics/Embodied AI/LLM)
人工智能·深度学习·机器学习·机器人·大模型·论文·具身智能
亦复何言??11 小时前
机器人强化学习入门笔记(四)
笔记·机器人
haing201914 小时前
七轴协作机器人运动学正解计算方法
算法·机器学习·机器人
亦复何言??15 小时前
四足机器人强化学习步态奖励:脚部空中时间 vs 步态相位建模
机器人
亦复何言??17 小时前
机器人强化学习入门笔记(五)
笔记·机器人
小陈phd1 天前
大语言模型实战(十)——基于通义千问 + FastMCP 打造天气查询机器人
qt·语言模型·机器人