Add file sink rotation and retention

This commit is contained in:
Nanaloveyuki
2026-05-09 21:24:02 +08:00
parent 18479a8b6f
commit fa2a165942
11 changed files with 299 additions and 16 deletions
+7 -3
View File
@@ -24,7 +24,7 @@ BitLogger 是一个基于 MoonBit 编写的结构化日志库。
- 🩹 可变换 Record:支持 `with_patch(...)``patch_sink(...)` 与脱敏/补字段/message 变换 helper。
- 📮 显式队列:支持 `queued_sink(...)` / `with_queue(...)`、有界积压与溢出策略,作为后续 async sink 的 runtime-safe 基础。
- 🧾 可配置文本格式:支持 `text_formatter(...)``format_text(...)``text_console_sink(...)``formatted_callback_sink(...)` 与模板化 `template` 输出。
- 💾 Native 文件输出:支持 `file_sink(...)`当前仅保证 `native/llvm` backend 可用。
- 💾 Native 文件输出:支持 `file_sink(...)`并已提供基础 size rotation / backup retention当前仅保证 `native/llvm` backend 可用。
- 📦 面向 MoonBitAPI 和工程结构围绕 MoonBit 的 package / visibility / toolchain 现实约束设计。
## 🚀 快速开始
@@ -161,7 +161,10 @@ ignore(logger.flush())
```moonbit
if native_files_supported() {
let logger = Logger::new(file_sink("bitlogger.log"), target="file")
let logger = Logger::new(
file_sink("bitlogger.log", rotation=Some(file_rotation(128, max_backups=2))),
target="file",
)
logger.info("hello", fields=[field("kind", "file")])
ignore(logger.sink.flush())
ignore(logger.sink.close())
@@ -182,7 +185,8 @@ if native_files_supported() {
## 📝 配置说明
- 当前提供 JSON 配置层:`parse_logger_config_text(...)``stringify_logger_config(...)``build_logger(...)`
- 已支持字段:`min_level``target``timestamp``sink.kind``sink.path``sink.append``sink.auto_flush``sink.text_formatter``queue`
- 已支持字段:`min_level``target``timestamp``sink.kind``sink.path``sink.append``sink.auto_flush``sink.rotation``sink.text_formatter``queue`
- `sink.rotation` 当前支持 `max_bytes``max_backups`,提供基础 size-based rotation 和 backup retention
- `sink.text_formatter.template` 当前支持固定 token`{timestamp}``{timestamp_ms}``{level}``{target}``{message}``{fields}`
- 当前可由配置直接组装的 sink 类型:`console``json_console``text_console``file`
- `queue` 会作为显式包装层附着在最终 sink 外侧;这仍然是同步 drain 模型,不是 async runtime