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

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界别日志去掉,避免发送日志影响性能。
相关推荐
kobesdu2 小时前
【ROS2实战笔记-12】rosshow:终端里的盲文可视化与无头机器人的现场调试
笔记·机器人·ros·移动机器人
workflower2 小时前
具身智能行业应用-生活服务业
大数据·人工智能·机器人·动态规划·生活
xwz小王子4 小时前
Science Robotics 让机器人学会“削果皮”:一种曲面物体操作任务转移的新方法
人工智能·机器人
xiaoduo AI4 小时前
客服机器人非工作时间能休眠?智能Agent开放平台定时唤醒,无人值守省资源?
大数据·人工智能·机器人
code_pgf7 小时前
Octo 算法详解-开源通用机器人策略模型技术报告
算法·机器人·开源
经济元宇宙9 小时前
全场景 AI 智能交互 专业级语音机器人推荐什么?
人工智能·机器人·语音识别
EriccoShaanxi11 小时前
高性能MEMS陀螺仪:精准导航,引领未来科技新风尚
科技·机器人·无人机
IT观测11 小时前
电话机器人服务商选型指南:核心维度与实操解析
人工智能·机器人·语音识别
Jump 不二11 小时前
揭秘腾讯 Ima 知识库架构:从开源 WeKnora 看 RAG + 知识图谱落地实践
人工智能·语言模型·架构·机器人·开源·知识图谱
Deepoch13 小时前
Deepoc 具身模型开发板在果蔬采摘机器人自主作业中的技术研究
机器人·开发板·采摘机器人·具身模型·deepoc