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

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界别日志去掉,避免发送日志影响性能。
相关推荐
QYR-分析3 小时前
机器人安全控制器行业高速扩容 本土替代迎来全新发展窗口期
人工智能·安全·机器人
多米哇卡3 小时前
WAIC现场|启元T1机器人亮相,支持轮足、四足形态自适应切换
机器人·启元t1机器人·waic展会
CyL_Cly3 小时前
安川机器人全流程学习资料 51G安川机器人编程指令说明书
机器人
ywl4708120874 小时前
【SpringAI 10】对话机器人快速入门
机器人·springai
茯苓gao11 小时前
机器人产业的三级火箭:卖电机、卖模组与卖整机,三种商业模式的终局推演
笔记·学习·机器人
深圳慧闻智造技术有限公司14 小时前
从宇树G1做手术登Nature,看机器人精密零件加工的四大难点与核心能力
机器人·精密零件加工·机器人零件加工·精密机械加工
ZPC821014 小时前
model bingxing
网络·人工智能·网络协议·机器人
code_pgf15 小时前
具身智能的核心组成部分及其作用
人工智能·机器人
QYR-分析15 小时前
柔性智造赋能升级!机器人视觉检测系统行业高速扩容,国产替代迎来新机遇
人工智能·机器人
消失的旧时光-19431 天前
第七篇:RTK + 地图 + 机器人:完整定位系统架构解析
android·系统架构·机器人·imu·rtk