日志管理器
Project description
np_log 智能日志系统 使用手册
简介
np_log 是一个功能强大的 Python 日志系统,提供智能调用者识别、彩色终端输出、自动日志管理和多平台通知等功能。
安装
pip install np_log
快速开始
from np_log import log, setup_logging
# 简单使用
log("Debug message") # 默认DEBUG级别
log("Info message", "INFO") # 指定级别
log("Error with bot", "ERROR", {"bot": True}) # 触发机器人通知
# 高级配置
logger = setup_logging(
name="my_app", # 自定义日志名称
console_level="INFO", # 控制台日志级别
file_level="DEBUG", # 文件日志级别
log_max_days=30, # 日志保留天数
log_max_size=100 # 单个日志文件大小(MB)
)
logger.info("Custom logger message")
核心功能
1. 智能调用追踪
- 自动识别真实调用源文件位置
- 准确显示文件名和行号
- 跳过logging模块内部调用
2. 彩色终端输出
- DEBUG: 青色
- INFO: 绿色
- WARNING: 黄色
- ERROR: 红色
- CRITICAL: 紫色
3. 自动日志管理
- 按日期分目录存储
- 支持日志轮转(默认50MB/文件)
- 可设置保留天数(默认7天)
- UTF-8编码保证兼容性
4. 机器人通知
- 支持企业微信和飞书
- 通过
.env文件配置 - 按需发送关键日志
配置说明
setup_logging 参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
name |
str | None | 自动检测调用文件 |
is_logfile |
bool | True | 是否启用文件日志 |
console_level |
str | "DEBUG" | 控制台日志级别 |
file_level |
str | "DEBUG" | 文件日志级别 |
log_max_days |
int | 7 | 日志保留天数 |
log_max_size |
int | 50 | 单个日志文件大小(MB) |
机器人配置
在项目根目录创建.env文件:
# 企业微信机器人(完整URL或KEY)
WECHAT_WEBHOOK_URL=your_wechat_key
# 飞书机器人
FEISHU_WEBHOOK_URL=your_feishu_key
最佳实践
- 项目初始化配置:
# app.py
from np_log import setup_logging
logger = setup_logging()
- 模块中使用:
# module.py
from np_log import log
def my_function():
log("Function started")
- 关键错误通知:
try:
critical_operation()
except Exception as e:
log(f"操作失败: {str(e)}", "ERROR", {"bot": True})
问题排查
常见问题
-
看不到颜色输出:
- 检查终端是否支持ANSI颜色
- 确保未重定向输出到文件
-
机器人不工作:
- 检查
.env文件配置 - 确认网络可访问
- 检查是否设置了
{"bot": True}
- 检查
-
行号显示不正确:
- 确保未使用
from np_log import * - 检查是否有中间包装函数
- 确保未使用
版本信息
- 当前版本:0.1.9
- 更新日期:2025-05-20
联系方式
如果您在使用过程中遇到任何问题,或者有任何建议和反馈,可以通过以下方式联系我们:
参与贡献
欢迎提交Issue或PR,请包含:
- 问题描述/功能建议
- 重现步骤
- 预期与实际行为
许可证
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
np_log-0.2.4-py3-none-any.whl
(18.6 kB
view details)
File details
Details for the file np_log-0.2.4-py3-none-any.whl.
File metadata
- Download URL: np_log-0.2.4-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b74ccf6f6b840dc8800a3c2e088a91ad1727c27e687cb8554ea60276af22d2e
|
|
| MD5 |
76b20c89c5a91dd745000f6d39a6477e
|
|
| BLAKE2b-256 |
c3fd6ee39d030764a3e0a2eb506cb169ff4ecb0799c3cf82131921a85b37a867
|