freel00p-colored-logger
一个极简、轻量的 Python 日志工具,提供:
- 控制台输出带 ANSI 颜色的时间戳 + 日志级别
- 可选的纯文本文件日志输出
- 零额外依赖(只依赖标准库
logging) - 简单易用,一行代码获取 logger
特性
- 彩色控制台输出(时间戳蓝色、INFO 绿色、WARNING 黄色、ERROR 红色、CRITICAL 紫色)
- 支持显示线程名(可关闭)
- 支持自定义日志名称、级别、文件路径
- 自动创建日志文件所在目录
- 防止重复添加 handler(适合 Jupyter、多模块使用)
- 完全兼容 Python 3.8+
安装
pip install freel00p-colored-logger
或使用 uv(推荐,更快):
uv pip install freel00p-colored-logger
快速使用
from freel00p_colored_logger import get_logger
# 1. 只输出到控制台(带颜色)
logger = get_logger("my_app", level="INFO")
logger.debug("这条不会显示(级别太低)")
logger.info("开始处理数据...")
logger.warning("内存使用率较高")
logger.error("文件读取失败", exc_info=True)
输出示例(控制台彩色效果):
2025-03-04 18:45:23 [INFO] [MainThread] 开始处理数据...
2025-03-04 18:45:24 [WARNING] [MainThread] 内存使用率较高
2025-03-04 18:45:25 [ERROR] [MainThread] 文件读取失败
Traceback (most recent call last): ...
# 2. 同时输出到文件(文件是纯文本格式)
logger = get_logger(
name="pdf_processor",
level="INFO",
log_file="logs/pdf.log", # 自动创建目录
file_level="DEBUG" # 文件记录更详细
)
logger.debug("详细调试信息,只写到文件")
logger.info("这条同时出现在控制台和文件")
完整参数说明
get_logger(
name: str = "app", # logger 名称
level: int | str = logging.INFO, # 控制台最低级别(可传字符串 "INFO")
log_file: str | Path | None = None, # 文件路径,None 则不写文件
file_level: int | str = logging.DEBUG, # 文件最低级别
datefmt: str = "%Y-%m-%d %H:%M:%S", # 时间格式
propagate: bool = False, # 是否向上传播(通常设为 False)
show_thread: bool = True # 是否显示线程名
) -> logging.Logger
推荐用法
在项目入口文件或主模块中:
# main.py
from freel00p_colored_logger import get_logger
logger = get_logger("my_project", log_file="logs/app.log")
# 全项目共用这个 logger
def some_function():
logger.info("函数开始执行")
为什么选择这个库?
- 极简:只有 ~100 行代码,无任何第三方依赖
- 颜色直观:适合开发/调试阶段快速定位问题
- 安全:避免重复配置 handler 的常见坑
- 跨平台:ANSI 颜色在大多数终端(Windows Terminal、VS Code、iTerm 等)正常显示
贡献
欢迎提交 issue 或 PR!
- Fork 本仓库
- 创建 feature 分支 (
git checkout -b feature/xxx) - 提交代码 (
git commit -m 'Add some feature') - Push 到分支 (
git push origin feature/xxx) - 提交 Pull Request
许可证
MIT License
Copyright (c) 2025 FreeL00P
详见 LICENSE 文件。
Metadata
Release files for freel00p-colored-logger 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| freel00p_colored_logger-0.1.5.tar.gz | 4.9 kB | Without provenance | Details for freel00p_colored_logger-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| freel00p_colored_logger-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for freel00p_colored_logger-0.1.5-py3-none-any.whl |
Total release size: 10.2 kB
Release files / freel00p_colored_logger-0.1.5.tar.gz
| Download URL | freel00p_colored_logger-0.1.5.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2e75e043c64d036c8c60dea47d6c0070c2dfd61d38fb18a7f54be658f6af1988
|
|
BLAKE2b-256 checksum How to use checksums |
a2bd7192a8d2df586a52e7b97182a666d84f06e2416490d2b2324209c8a7a659
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / freel00p_colored_logger-0.1.5-py3-none-any.whl
| Download URL | freel00p_colored_logger-0.1.5-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2745916214e178afdf7613d1a98532eaa2d69b42ef24e8c631780b821252ce20
|
|
BLAKE2b-256 checksum How to use checksums |
6845a3b4d31f22e7458dcc0831816e79c9cf468bef192024b951a9a5b67762b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for freel00p-colored-logger 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.