Add explicit reopen-with-policy helper

This commit is contained in:
Nanaloveyuki
2026-05-10 12:52:01 +08:00
parent 0a7af44f06
commit c779befc23
8 changed files with 36 additions and 11 deletions
+4 -4
View File
@@ -26,7 +26,7 @@ BitLogger 是一个基于 MoonBit 编写的结构化日志库。
- 🧷 可绑定上下文:支持 `bind(...)``fields(...)`,更方便地封装复用字段上下文。 - 🧷 可绑定上下文:支持 `bind(...)``fields(...)`,更方便地封装复用字段上下文。
- 📮 显式队列:支持 `queued_sink(...)` / `with_queue(...)`、有界积压与溢出策略,作为后续 async sink 的 runtime-safe 基础。 - 📮 显式队列:支持 `queued_sink(...)` / `with_queue(...)`、有界积压与溢出策略,作为后续 async sink 的 runtime-safe 基础。
- 🧾 可配置文本格式:支持 `text_formatter(...)``format_text(...)``text_console_sink(...)``formatted_callback_sink(...)` 与模板化 `template` 输出。 - 🧾 可配置文本格式:支持 `text_formatter(...)``format_text(...)``text_console_sink(...)``formatted_callback_sink(...)` 与模板化 `template` 输出。
- 💾 Native 文件输出:支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` 与失败计数;当前仅保证 `native/llvm` backend 可用。 - 💾 Native 文件输出:支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` / `reopen_with_current_policy()` 与失败计数;当前仅保证 `native/llvm` backend 可用。
- 📦 面向 MoonBitAPI 和工程结构围绕 MoonBit 的 package / visibility / toolchain 现实约束设计。 - 📦 面向 MoonBitAPI 和工程结构围绕 MoonBit 的 package / visibility / toolchain 现实约束设计。
## 🚀 快速开始 ## 🚀 快速开始
@@ -221,13 +221,13 @@ if native_files_supported() {
- 当前提供 JSON 配置层:`parse_logger_config_text(...)``stringify_logger_config(...)``build_logger(...)` - 当前提供 JSON 配置层:`parse_logger_config_text(...)``stringify_logger_config(...)``build_logger(...)`
- 已支持字段:`min_level``target``timestamp``sink.kind``sink.path``sink.append``sink.auto_flush``sink.rotation``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.rotation` 当前支持 `max_bytes``max_backups`,提供基础 size-based rotation 和 backup retention
- `file_sink(...)` 还提供 `reopen()``open_failures()``write_failures()``flush_failures()``rotation_failures()`,用于基础可观测性 - `file_sink(...)` 还提供 `reopen()``reopen_with_current_policy()``open_failures()``write_failures()``flush_failures()``rotation_failures()`,用于基础可观测性
- `file_sink(...)` 当前还提供 `append_mode()``reopen(append=...)` 在显式传值时会更新后续 reopen 使用的 append 策略 - `file_sink(...)` 当前还提供 `append_mode()``reopen(append=...)` 在显式传值时会更新后续 reopen 使用的 append 策略`reopen_with_current_policy()` 则把“按当前保存策略重开”变成显式动作
- `file_sink(...)` 也支持 `set_append_mode(...)`,用于显式修改后续 reopen 将使用的 append 策略 - `file_sink(...)` 也支持 `set_append_mode(...)`,用于显式修改后续 reopen 将使用的 append 策略
- `file_sink(...)` 也可直接读取 `path()``auto_flush_enabled()` 等基础 file 策略状态 - `file_sink(...)` 也可直接读取 `path()``auto_flush_enabled()` 等基础 file 策略状态
- `file_sink(...)` 还提供 `rotation_enabled()``rotation_config()`,可直接查询当前 rotation 策略是否启用及其参数 - `file_sink(...)` 还提供 `rotation_enabled()``rotation_config()`,可直接查询当前 rotation 策略是否启用及其参数
- `file_sink(...)` 也支持 `set_auto_flush(...)``set_rotation(...)``clear_rotation()`,可在运行期调整基础写出策略 - `file_sink(...)` 也支持 `set_auto_flush(...)``set_rotation(...)``clear_rotation()`,可在运行期调整基础写出策略
- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()``file_flush()``file_close()``file_append_mode()``file_path()``file_auto_flush()``file_rotation_enabled()``file_rotation_config()`,以及 `file_set_append_mode(...)``file_set_auto_flush(...)``file_set_rotation(...)``file_clear_rotation()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制 - `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()``file_reopen_with_current_policy()``file_flush()``file_close()``file_append_mode()``file_path()``file_auto_flush()``file_rotation_enabled()``file_rotation_config()`,以及 `file_set_append_mode(...)``file_set_auto_flush(...)``file_set_rotation(...)``file_clear_rotation()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制
- `sink.text_formatter.template` 当前支持固定 token`{timestamp}``{timestamp_ms}``{level}``{target}``{message}``{fields}` - `sink.text_formatter.template` 当前支持固定 token`{timestamp}``{timestamp_ms}``{level}``{target}``{message}``{fields}`
- 当前可由配置直接组装的 sink 类型:`console``json_console``text_console``file` - 当前可由配置直接组装的 sink 类型:`console``json_console``text_console``file`
- `queue` 会作为显式包装层附着在最终 sink 外侧;这仍然是同步 drain 模型,不是 async runtime - `queue` 会作为显式包装层附着在最终 sink 外侧;这仍然是同步 drain 模型,不是 async runtime
+4
View File
@@ -285,6 +285,8 @@ test "configured logger can reopen built file sink" {
inspect(logger.file_append_mode(), content="false") inspect(logger.file_append_mode(), content="false")
inspect(logger.file_reopen(), content="true") inspect(logger.file_reopen(), content="true")
inspect(logger.file_append_mode(), content="false") inspect(logger.file_append_mode(), content="false")
inspect(logger.file_reopen_with_current_policy(), content="true")
inspect(logger.file_append_mode(), content="false")
inspect(logger.close(), content="true") inspect(logger.close(), content="true")
} else { } else {
inspect(logger.file_append_mode(), content="true") inspect(logger.file_append_mode(), content="true")
@@ -295,6 +297,8 @@ test "configured logger can reopen built file sink" {
inspect(logger.file_open_failures(), content="2") inspect(logger.file_open_failures(), content="2")
inspect(logger.file_reopen(append=Some(false)), content="false") inspect(logger.file_reopen(append=Some(false)), content="false")
inspect(logger.file_append_mode(), content="false") inspect(logger.file_append_mode(), content="false")
inspect(logger.file_reopen_with_current_policy(), content="false")
inspect(logger.file_open_failures(), content="3")
} }
} }
+4
View File
@@ -177,6 +177,8 @@ test "file sink reopen and failure counters reflect backend state" {
inspect(sink.append_mode(), content="false") inspect(sink.append_mode(), content="false")
inspect(sink.reopen(), content="true") inspect(sink.reopen(), content="true")
inspect(sink.append_mode(), content="false") inspect(sink.append_mode(), content="false")
inspect(sink.reopen_with_current_policy(), content="true")
inspect(sink.append_mode(), content="false")
inspect(sink.close(), content="true") inspect(sink.close(), content="true")
ignore(remove_file_internal("bitlogger-reopen.log")) ignore(remove_file_internal("bitlogger-reopen.log"))
} else { } else {
@@ -189,6 +191,8 @@ test "file sink reopen and failure counters reflect backend state" {
inspect(sink.flush_failures(), content="0") inspect(sink.flush_failures(), content="0")
inspect(sink.reopen(append=Some(false)), content="false") inspect(sink.reopen(append=Some(false)), content="false")
inspect(sink.append_mode(), content="false") inspect(sink.append_mode(), content="false")
inspect(sink.reopen_with_current_policy(), content="false")
inspect(sink.open_failures(), content="3")
} }
} }
+3 -3
View File
@@ -46,8 +46,8 @@ BitLogger 是一个基于 MoonBit 的结构化日志库。
- 支持 `parse_logger_config_text(...)``stringify_logger_config(...)` 进行最小 JSON 配置读写 - 支持 `parse_logger_config_text(...)``stringify_logger_config(...)` 进行最小 JSON 配置读写
- config-driven logger assembly via `build_logger(...)` - config-driven logger assembly via `build_logger(...)`
- 支持 `build_logger(...)` 将配置组装为可直接使用的 logger - 支持 `build_logger(...)` 将配置组装为可直接使用的 logger
- native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()`, and failure counters - native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()` / `reopen_with_current_policy()`, and failure counters
- 支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` 与失败计数,但当前仅保证 `native/llvm` backend 可用 - 支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` / `reopen_with_current_policy()` 与失败计数,但当前仅保证 `native/llvm` backend 可用
## Example / 示例 ## Example / 示例
@@ -220,7 +220,7 @@ test {
- basic rotation is size-based / 当前基础轮转为按文件大小触发 - basic rotation is size-based / 当前基础轮转为按文件大小触发
- `file_rotation(max_bytes, max_backups=...)` controls threshold and retained backups / `file_rotation(max_bytes, max_backups=...)` 控制触发阈值和保留备份数 - `file_rotation(max_bytes, max_backups=...)` controls threshold and retained backups / `file_rotation(max_bytes, max_backups=...)` 控制触发阈值和保留备份数
- JSON config uses `sink.rotation.max_bytes` and `sink.rotation.max_backups` / JSON 配置使用 `sink.rotation.max_bytes` 与 `sink.rotation.max_backups` - JSON config uses `sink.rotation.max_bytes` and `sink.rotation.max_backups` / JSON 配置使用 `sink.rotation.max_bytes` 与 `sink.rotation.max_backups`
- `FileSink::reopen()` can explicitly reopen the current file handle / `FileSink::reopen()` 可显式重开当前文件句柄 - `FileSink::reopen()` can explicitly reopen the current file handle, and `FileSink::reopen_with_current_policy()` makes the stored-policy reopen path explicit / `FileSink::reopen()` 可显式重开当前文件句柄,`FileSink::reopen_with_current_policy()` 则把“按当前保存策略重开”变成显式动作
- `append_mode()` exposes the current append policy, and `reopen(append=...)` updates that policy for later reopen calls / `append_mode()` 可读取当前 append 策略,`reopen(append=...)` 会更新后续 reopen 复用的 append 策略 - `append_mode()` exposes the current append policy, and `reopen(append=...)` updates that policy for later reopen calls / `append_mode()` 可读取当前 append 策略,`reopen(append=...)` 会更新后续 reopen 复用的 append 策略
- `set_append_mode(...)` updates the stored append policy without forcing an immediate reopen / `set_append_mode(...)` 可直接更新保存的 append 策略,但不会强制立即 reopen - `set_append_mode(...)` updates the stored append policy without forcing an immediate reopen / `set_append_mode(...)` 可直接更新保存的 append 策略,但不会强制立即 reopen
- `path()` and `auto_flush_enabled()` expose core file sink policy state / `path()` 与 `auto_flush_enabled()` 可读取 file sink 的基础策略状态 - `path()` and `auto_flush_enabled()` expose core file sink policy state / `path()` 与 `auto_flush_enabled()` 可读取 file sink 的基础策略状态
+12
View File
@@ -231,6 +231,14 @@ pub fn RuntimeSink::file_reopen(self : RuntimeSink, append~ : Bool? = None) -> B
} }
} }
pub fn RuntimeSink::file_reopen_with_current_policy(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.reopen_with_current_policy()
QueuedFile(sink) => sink.sink.reopen_with_current_policy()
_ => false
}
}
pub fn RuntimeSink::file_append_mode(self : RuntimeSink) -> Bool { pub fn RuntimeSink::file_append_mode(self : RuntimeSink) -> Bool {
match self { match self {
File(sink) => sink.append_mode() File(sink) => sink.append_mode()
@@ -411,6 +419,10 @@ pub fn ConfiguredLogger::file_reopen(self : ConfiguredLogger, append~ : Bool? =
self.sink.file_reopen(append=append) self.sink.file_reopen(append=append)
} }
pub fn ConfiguredLogger::file_reopen_with_current_policy(self : ConfiguredLogger) -> Bool {
self.sink.file_reopen_with_current_policy()
}
pub fn ConfiguredLogger::file_append_mode(self : ConfiguredLogger) -> Bool { pub fn ConfiguredLogger::file_append_mode(self : ConfiguredLogger) -> Bool {
self.sink.file_append_mode() self.sink.file_append_mode()
} }
+4
View File
@@ -197,6 +197,10 @@ pub fn FileSink::reopen(self : FileSink, append~ : Bool? = None) -> Bool {
} }
} }
pub fn FileSink::reopen_with_current_policy(self : FileSink) -> Bool {
self.reopen()
}
fn rotated_file_path(path : String, index : Int) -> String { fn rotated_file_path(path : String, index : Int) -> String {
"\{path}.\{index}" "\{path}.\{index}"
} }
+4 -4
View File
@@ -26,7 +26,7 @@ BitLogger currently provides:
- bounded backlog with `QueueOverflowPolicy::DropNewest` and `QueueOverflowPolicy::DropOldest` - bounded backlog with `QueueOverflowPolicy::DropNewest` and `QueueOverflowPolicy::DropOldest`
- configurable text formatting via `text_formatter(...)`, `format_text(...)`, `text_console_sink(...)`, and template-driven `template` output - configurable text formatting via `text_formatter(...)`, `format_text(...)`, `text_console_sink(...)`, and template-driven `template` output
- formatter-based callback integration via `formatted_callback_sink(...)` - formatter-based callback integration via `formatted_callback_sink(...)`
- native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()`, and failure counters - native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()` / `reopen_with_current_policy()`, and failure counters
- `native_files_supported()` for backend capability detection - `native_files_supported()` for backend capability detection
- default global logger helpers - default global logger helpers
@@ -206,13 +206,13 @@ if native_files_supported() {
- BitLogger now includes a JSON config layer via `parse_logger_config_text(...)`, `stringify_logger_config(...)`, and `build_logger(...)`. - BitLogger now includes a JSON config layer via `parse_logger_config_text(...)`, `stringify_logger_config(...)`, and `build_logger(...)`.
- Supported keys include `min_level`, `target`, `timestamp`, `sink.kind`, `sink.path`, `sink.append`, `sink.auto_flush`, `sink.rotation`, `sink.text_formatter`, and `queue`. - Supported keys include `min_level`, `target`, `timestamp`, `sink.kind`, `sink.path`, `sink.append`, `sink.auto_flush`, `sink.rotation`, `sink.text_formatter`, and `queue`.
- `sink.rotation` currently supports `max_bytes` and `max_backups` for basic size-based rotation and backup retention. - `sink.rotation` currently supports `max_bytes` and `max_backups` for basic size-based rotation and backup retention.
- `file_sink(...)` also exposes `reopen()`, `open_failures()`, `write_failures()`, `flush_failures()`, and `rotation_failures()` for basic observability. - `file_sink(...)` also exposes `reopen()`, `reopen_with_current_policy()`, `open_failures()`, `write_failures()`, `flush_failures()`, and `rotation_failures()` for basic observability.
- `file_sink(...)` also exposes `append_mode()`. Passing `append=...` to `reopen(...)` updates the current append policy used by later reopen calls. - `file_sink(...)` also exposes `append_mode()`. Passing `append=...` to `reopen(...)` updates the current append policy used by later reopen calls, while `reopen_with_current_policy()` makes that stored-policy reopen path explicit.
- `file_sink(...)` also supports `set_append_mode(...)` for explicitly changing the append policy that later reopen calls will use. - `file_sink(...)` also supports `set_append_mode(...)` for explicitly changing the append policy that later reopen calls will use.
- `file_sink(...)` also exposes `path()` and `auto_flush_enabled()` for reading basic file-sink policy state. - `file_sink(...)` also exposes `path()` and `auto_flush_enabled()` for reading basic file-sink policy state.
- `file_sink(...)` also exposes `rotation_enabled()` and `rotation_config()` for reading whether rotation is active and which parameters are currently in effect. - `file_sink(...)` also exposes `rotation_enabled()` and `rotation_config()` for reading whether rotation is active and which parameters are currently in effect.
- `file_sink(...)` also supports `set_auto_flush(...)`, `set_rotation(...)`, and `clear_rotation()` for runtime policy updates. - `file_sink(...)` also supports `set_auto_flush(...)`, `set_rotation(...)`, and `clear_rotation()` for runtime policy updates.
- `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()`, `file_flush()`, `file_close()`, `file_append_mode()`, `file_path()`, `file_auto_flush()`, `file_rotation_enabled()`, `file_rotation_config()`, plus `file_set_append_mode(...)`, `file_set_auto_flush(...)`, `file_set_rotation(...)`, `file_clear_rotation()`, and the corresponding file failure counters, so config-driven file logging keeps a usable control surface. - `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()`, `file_reopen_with_current_policy()`, `file_flush()`, `file_close()`, `file_append_mode()`, `file_path()`, `file_auto_flush()`, `file_rotation_enabled()`, `file_rotation_config()`, plus `file_set_append_mode(...)`, `file_set_auto_flush(...)`, `file_set_rotation(...)`, `file_clear_rotation()`, and the corresponding file failure counters, so config-driven file logging keeps a usable control surface.
- `sink.text_formatter.template` currently supports fixed tokens: `{timestamp}`, `{timestamp_ms}`, `{level}`, `{target}`, `{message}`, and `{fields}`. - `sink.text_formatter.template` currently supports fixed tokens: `{timestamp}`, `{timestamp_ms}`, `{level}`, `{target}`, `{message}`, and `{fields}`.
- Config-driven sink assembly currently supports `console`, `json_console`, `text_console`, and `file`. - Config-driven sink assembly currently supports `console`, `json_console`, `text_console`, and `file`.
- `queue` remains a synchronous bounded wrapper around the final sink, not an async runtime. - `queue` remains a synchronous bounded wrapper around the final sink, not an async runtime.
+1
View File
@@ -37,6 +37,7 @@ version 0.3.0
- feat: add `rotation_enabled()` / `rotation_config()` on `FileSink` and `file_rotation_enabled()` / `file_rotation_config()` on `ConfiguredLogger` for rotation-policy introspection - feat: add `rotation_enabled()` / `rotation_config()` on `FileSink` and `file_rotation_enabled()` / `file_rotation_config()` on `ConfiguredLogger` for rotation-policy introspection
- feat: add runtime file-policy mutators `set_auto_flush(...)`, `set_rotation(...)`, `clear_rotation()` and corresponding configured-logger forwarding helpers - feat: add runtime file-policy mutators `set_auto_flush(...)`, `set_rotation(...)`, `clear_rotation()` and corresponding configured-logger forwarding helpers
- feat: add explicit append-policy setter `set_append_mode(...)` / `file_set_append_mode(...)` so append strategy can be updated without piggybacking on `reopen(...)` - feat: add explicit append-policy setter `set_append_mode(...)` / `file_set_append_mode(...)` so append strategy can be updated without piggybacking on `reopen(...)`
- feat: add explicit `reopen_with_current_policy()` / `file_reopen_with_current_policy()` helpers so stored append-policy reopen is discoverable instead of implicit
- feat: add `SplitSink`, `split_sink(...)`, and `split_by_level(...)` for routing records into different sinks by predicate or level - feat: add `SplitSink`, `split_sink(...)`, and `split_by_level(...)` for routing records into different sinks by predicate or level
- feat: add `Logger::bind(...)` as an ergonomic context-binding alias and `fields(...)` helper for tuple-based field construction - feat: add `Logger::bind(...)` as an ergonomic context-binding alias and `fields(...)` helper for tuple-based field construction