Add file state JSON export helpers

This commit is contained in:
Nanaloveyuki
2026-05-10 13:05:06 +08:00
parent 41c9ca3eb8
commit d209b7d618
7 changed files with 220 additions and 23 deletions
+20
View File
@@ -206,6 +206,25 @@ if native_files_supported() {
```
</details>
<details><summary>file runtime state dump 示例</summary>
```moonbit
let logger = build_logger(
LoggerConfig::new(
sink=SinkConfig::new(kind=SinkKind::File, path="bitlogger-runtime.log"),
queue=Some(QueueConfig::new(16)),
),
)
logger.info("queued hello")
match logger.file_runtime_state() {
Some(snapshot) => println(stringify_runtime_file_state(snapshot, pretty=true))
None => ()
}
```
</details>
## 📂 仓库结构
- `bitlogger/`MoonBit 库 package,本体实现、测试与 Mooncake README
@@ -230,6 +249,7 @@ if native_files_supported() {
- `file_sink(...)` 也支持 `set_auto_flush(...)``set_rotation(...)``clear_rotation()`,可在运行期调整基础写出策略
- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()``file_reopen_with_current_policy()``file_reopen_append()``file_reopen_truncate()``file_flush()``file_close()``file_append_mode()``file_path()``file_auto_flush()``file_rotation_enabled()``file_rotation_config()``file_state()`,以及 `file_set_append_mode(...)``file_set_auto_flush(...)``file_set_rotation(...)``file_clear_rotation()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制
- `ConfiguredLogger` 还提供 `file_runtime_state()`,可在 file sink 外层包了 queue 时同时读取底层 file 快照、是否 queue 包装、当前 pending 与 dropped 计数
- `file_sink_state_to_json(...)``stringify_file_sink_state(...)``runtime_file_state_to_json(...)``stringify_runtime_file_state(...)` 可直接把 file / queued-file 快照导出为 JSON,便于排障或上报
- `sink.text_formatter.template` 当前支持固定 token`{timestamp}``{timestamp_ms}``{level}``{target}``{message}``{fields}`
- 当前可由配置直接组装的 sink 类型:`console``json_console``text_console``file`
- `queue` 会作为显式包装层附着在最终 sink 外侧;这仍然是同步 drain 模型,不是 async runtime