mi-task/README_log.md

168 lines
3.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 机器人日志模块
基于loguru和rich库实现的增强日志系统提供美观、功能丰富的日志输出功能。
## 特点
- ✅ 彩色日志输出,清晰分层
- ✅ 支持emoji表情增强关键信息视觉效果
- ✅ 自动日志分割和压缩存档
- ✅ 丰富的展示方式:表格、进度条、代码高亮等
- ✅ 简单易用的API
## 安装依赖
```bash
pip install loguru rich
```
## 基本用法
```python
from utils.log_helper import info, warning, error, success, debug
# 基本日志输出
info("这是一条信息")
warning("这是一条警告")
error("这是一条错误")
success("这是一条成功信息")
debug("这是一条调试信息")
# 使用emoji增强关键信息
info("检测到横线", "检测") # 👁️ 检测到横线
warning("电池电量低", "警告") # ⚠️ 电池电量低
error("无法连接设备", "错误") # 🚫 无法连接设备
success("任务完成", "完成") # 🏁 任务完成
```
## 高级功能
### 分节标题
```python
from utils.log_helper import section
# 输出醒目的分节标题
section("系统初始化", "启动") # ================== 🚀 系统初始化 🚀 ==================
```
### 计时日志
```python
import time
from utils.log_helper import timing
# 记录操作耗时
start = time.time()
# ... 执行操作 ...
elapsed = time.time() - start
timing("数据处理", elapsed) # ⏱️ 数据处理耗时: 1.234秒
```
### 表格输出
```python
from utils.log_helper import table
# 创建表格
columns = ["参数", "数值", "单位"]
rows = [
["速度", 1.5, "m/s"],
["角度", 90, "度"],
["距离", 0.5, "米"]
]
table("机器人状态", columns, rows)
```
### 进度条
```python
from utils.log_helper import progress
import time
# 显示进度条
items = range(10)
for item in progress(items, "处理数据", unit="项"):
# 处理每个item
time.sleep(0.1)
```
### 代码语法高亮
```python
from utils.log_helper import syntax
# 显示代码
code = """
def hello():
print("Hello, world!")
"""
syntax(code, "python", "示例代码")
```
### JSON数据美化
```python
from utils.log_helper import json_log
# 美化JSON数据
data = {
"name": "robot_1",
"position": {"x": 1.2, "y": 3.4},
"status": "running"
}
json_log(data, "机器人状态")
```
### 面板显示
```python
from utils.log_helper import panel
# 显示带边框的面板
panel(
"系统启动完成\n等待用户命令...",
"系统状态",
"bold green"
)
```
## 自定义日志器
```python
from utils.log_helper import LogHelper, get_logger
# 创建自定义名称的日志器
logger = get_logger("机器人控制")
# 或
logger = LogHelper("机器人控制")
# 使用自定义日志器
logger.info("初始化完成")
logger.warning("发现障碍物", "警告")
```
## 支持的emoji
| 类型 | emoji | 说明 |
|-----|-------|------|
| 成功 | ✅ | 操作成功 |
| 失败 | ❌ | 操作失败 |
| 警告 | ⚠️ | 警告信息 |
| 错误 | 🚫 | 错误信息 |
| 信息 | | 普通信息 |
| 启动 | 🚀 | 系统启动 |
| 停止 | 🛑 | 系统停止 |
| 旋转 | 🔄 | 旋转操作 |
| 扫描 | 🔍 | 扫描操作 |
| 计算 | 🧮 | 计算过程 |
| 时间 | ⏱️ | 时间相关 |
| 距离 | 📏 | 距离测量 |
| 角度 | 📐 | 角度测量 |
| 调试 | 🐞 | 调试信息 |
| 关键 | 🔑 | 关键信息 |
| 位置 | 📍 | 位置标记 |
| 校准 | 🎯 | 校准操作 |
| 完成 | 🏁 | 完成标记 |
| 移动 | 🚶 | 移动操作 |
| 检测 | 👁️ | 检测操作 |