farlog
farlog 是一个轻量的 Loguru 辅助库,用于按名称拆分日志文件,并提供开箱即用的按日轮转、压缩和保留策略。
特性
- 导入包时不创建目录、不写文件,也不修改 Loguru 的全局 handler
- 每个 logger 名称对应一个独立日志文件
- 日志按日轮转,历史文件自动使用 gzip 压缩
- 可选生成聚合日志
all.log,同时统一控制台输出格式 - 重复获取同名 logger 不会重复添加 handler
- 兼容旧接口
getLogger
环境要求
- Python 3.10 或更高版本
安装
pip install farlog
快速开始
仅使用命名日志
直接调用 get_logger() 会保留应用已有的 Loguru handler,并在默认的 logs/ 目录中创建命名日志文件。
from farlog import get_logger
log = get_logger("worker")
log.info("任务开始")
生成的文件:
logs/
└── worker.log
配置控制台和聚合日志
需要统一控制台格式或生成 all.log 时,在应用启动阶段显式调用 configure():
from farlog import configure, get_logger
configure("logs")
log = get_logger("worker", level="DEBUG")
log.debug("调试信息")
log.info("任务开始")
生成的文件:
logs/
├── all.log
└── worker.log
configure() 会替换 Loguru 的全局 handler。未绑定 module_name 的普通 Loguru 日志仍可正常输出,并在格式中显示为 -。
API
configure(log_dir="logs")
配置彩色控制台输出和聚合日志,并设置后续命名日志使用的目录。
log_dir:日志目录,支持字符串或pathlib.Pathall.log:记录INFO及以上级别,按日轮转,保留最近 30 个文件- 如果已经创建过命名 logger,其文件 handler 会切换到新目录
get_logger(name="default", level="INFO", formatter=None)
获取带有独立文件 handler 的 Loguru logger。
name:logger 名称,同时作为日志文件名;只允许普通文件名,不允许传入路径level:该命名日志文件的最低记录级别formatter:可选的 Loguru 格式字符串- 命名日志按日轮转并压缩,保留最近 7 个文件
- 重复调用会复用同名 logger;修改
level或formatter时会替换旧文件 handler
非法名称会抛出 ValueError:
get_logger("../outside") # ValueError
旧接口仍然可用:
from farlog import getLogger
log = getLogger("worker")
开发
开发依赖(pytest、ruff)定义在 pyproject.toml 的 [dependency-groups].dev 中,由 uv 管理:
uv sync
运行回归测试和静态检查:
uv run pytest -q
uv run ruff check .
uv run ruff format --check .
许可证
本项目使用 MIT License。
关于 farfarfun
farfarfun 是一个专注于实用工具库的开源组织, 涵盖云存储、数据处理、AI、多媒体与开发工具链等方向。
- 🏠 组织主页:https://github.com/farfarfun
- 📦 PyPI:https://pypi.org/user/niuliangtao/
- 📧 联系:farfarfun@qq.com
本项目基于 MIT 协议开源。
Metadata
Release files for farlog 1.1.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| farlog-1.1.12-py3-none-any.whl | Python 3 | none | any | Details for farlog-1.1.12-py3-none-any.whl |
Release files / farlog-1.1.12-py3-none-any.whl
| Download URL | farlog-1.1.12-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba5d439b0ce969d448faec52609e48d6f814e82beb0e3b6de4dd2c6055503731
|
|
BLAKE2b-256 checksum How to use checksums |
3b49082b3e24fb3859dde4a9f6a35d45e50764f3d98f14102d694552de14da05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","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":null}
|
Security for farlog 1.1.12
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.