Add file sink state snapshots

This commit is contained in:
Nanaloveyuki
2026-05-10 12:58:16 +08:00
parent 1a9dfe2397
commit cb94d80aab
8 changed files with 74 additions and 3 deletions
+2 -1
View File
@@ -226,8 +226,9 @@ if native_files_supported() {
- `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(...)` 还提供 `state()`,可一次性读取 path、available、append、auto_flush、rotation 与各类 failure counter 快照
- `file_sink(...)` 也支持 `set_auto_flush(...)``set_rotation(...)``clear_rotation()`,可在运行期调整基础写出策略 - `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_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_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 计数访问器,便于配置式接入后继续运维控制
- `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
+9
View File
@@ -206,8 +206,13 @@ test "configured logger exposes file sink observability helpers" {
), ),
), ),
) )
let state = logger.file_state()
inspect(logger.file_available() == native_files_supported(), content="true") inspect(logger.file_available() == native_files_supported(), content="true")
inspect(logger.file_path(), content="config-file.log") inspect(logger.file_path(), content="config-file.log")
inspect(state.path, content="config-file.log")
inspect(state.available == logger.file_available(), content="true")
inspect(state.append == logger.file_append_mode(), content="true")
inspect(state.auto_flush == logger.file_auto_flush(), content="true")
inspect(logger.file_append_mode(), content="true") inspect(logger.file_append_mode(), content="true")
inspect(logger.file_auto_flush(), content="false") inspect(logger.file_auto_flush(), content="false")
inspect(logger.file_rotation_enabled(), content="true") inspect(logger.file_rotation_enabled(), content="true")
@@ -264,6 +269,10 @@ test "configured logger file setters update file sink policy state" {
inspect(logger.file_rotation_config() is None, content="true") inspect(logger.file_rotation_config() is None, content="true")
inspect(logger.file_reopen(), content=if logger.file_available() { "true" } else { "false" }) inspect(logger.file_reopen(), content=if logger.file_available() { "true" } else { "false" })
inspect(logger.file_append_mode(), content="false") inspect(logger.file_append_mode(), content="false")
let state = logger.file_state()
inspect(state.append, content="false")
inspect(state.auto_flush, content="false")
inspect(state.rotation is None, content="true")
ignore(logger.close()) ignore(logger.close())
} }
+10
View File
@@ -89,8 +89,14 @@ test "native file support flag is queryable" {
test "file sink availability reflects backend support" { test "file sink availability reflects backend support" {
let sink = file_sink("bitlogger-test.log") let sink = file_sink("bitlogger-test.log")
let state = sink.state()
inspect(sink.path(), content="bitlogger-test.log") inspect(sink.path(), content="bitlogger-test.log")
inspect(sink.is_available() == native_files_supported(), content="true") inspect(sink.is_available() == native_files_supported(), content="true")
inspect(state.path, content="bitlogger-test.log")
inspect(state.available == sink.is_available(), content="true")
inspect(state.append == sink.append_mode(), content="true")
inspect(state.auto_flush == sink.auto_flush_enabled(), content="true")
inspect((state.rotation is None) == (sink.rotation_config() is None), content="true")
inspect(sink.append_mode(), content="true") inspect(sink.append_mode(), content="true")
inspect(sink.auto_flush_enabled(), content="true") inspect(sink.auto_flush_enabled(), content="true")
inspect(sink.rotation_enabled(), content="false") inspect(sink.rotation_enabled(), content="false")
@@ -145,6 +151,10 @@ test "file sink setters update auto flush and rotation state" {
inspect(sink.rotation_config() is None, content="true") inspect(sink.rotation_config() is None, content="true")
inspect(sink.reopen(), content=if sink.is_available() { "true" } else { "false" }) inspect(sink.reopen(), content=if sink.is_available() { "true" } else { "false" })
inspect(sink.append_mode(), content="false") inspect(sink.append_mode(), content="false")
let state = sink.state()
inspect(state.append, content="false")
inspect(state.auto_flush, content="false")
inspect(state.rotation is None, content="true")
} }
test "file sink tracks rotation failures on unavailable backend" { test "file sink tracks rotation failures on unavailable backend" {
+2 -1
View File
@@ -225,7 +225,8 @@ test {
- `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 的基础策略状态
- `rotation_enabled()` and `rotation_config()` expose whether rotation is active and which settings are currently applied / `rotation_enabled()` 与 `rotation_config()` 可读取 rotation 是否启用及当前配置参数 - `rotation_enabled()` and `rotation_config()` expose whether rotation is active and which settings are currently applied / `rotation_enabled()` 与 `rotation_config()` 可读取 rotation 是否启用及当前配置参数
- `state()` exposes a single file-sink snapshot including path, availability, append policy, auto-flush, rotation config, and failure counters / `state()` 可一次性读取包含 path、availability、append、auto-flush、rotation 配置与失败计数的 file sink 快照
- `set_auto_flush(...)`, `set_rotation(...)`, and `clear_rotation()` allow runtime tuning of core file sink policies / `set_auto_flush(...)`、`set_rotation(...)`、`clear_rotation()` 可在运行期调整 file sink 的基础策略 - `set_auto_flush(...)`, `set_rotation(...)`, and `clear_rotation()` allow runtime tuning of core file sink policies / `set_auto_flush(...)`、`set_rotation(...)`、`clear_rotation()` 可在运行期调整 file sink 的基础策略
- `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` expose basic sink health counters / `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` 可用于观察基础 sink 健康状态 - `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` expose basic sink health counters / `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` 可用于观察基础 sink 健康状态
- `ConfiguredLogger` also forwards file reopen, flush, close, append-mode, path, auto-flush, rotation-config, append setter, policy setter, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close、append-mode、path、auto-flush、rotation 配置、append setter、策略 setter 与失败计数访问器 - `ConfiguredLogger` also forwards file reopen, flush, close, append-mode, path, auto-flush, rotation-config, state snapshot, append setter, policy setter, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close、append-mode、path、auto-flush、rotation 配置、state 快照、append setter、策略 setter 与失败计数访问器
- current scope is observability-first, not a full self-healing sink runtime / 当前定位仍以可观测性优先,不是完整自愈型 sink runtime - current scope is observability-first, not a full self-healing sink runtime / 当前定位仍以可观测性优先,不是完整自愈型 sink runtime
+22
View File
@@ -405,6 +405,24 @@ pub fn RuntimeSink::file_rotation_failures(self : RuntimeSink) -> Int {
} }
} }
pub fn RuntimeSink::file_state(self : RuntimeSink) -> FileSinkState {
match self {
File(sink) => sink.state()
QueuedFile(sink) => sink.sink.state()
_ => {
path: "",
available: false,
append: false,
auto_flush: false,
rotation: None,
open_failures: 0,
write_failures: 0,
flush_failures: 0,
rotation_failures: 0,
}
}
}
pub type ConfiguredLogger = Logger[RuntimeSink] pub type ConfiguredLogger = Logger[RuntimeSink]
pub fn ConfiguredLogger::flush(self : ConfiguredLogger) -> Int { pub fn ConfiguredLogger::flush(self : ConfiguredLogger) -> Int {
@@ -510,6 +528,10 @@ pub fn ConfiguredLogger::file_rotation_failures(self : ConfiguredLogger) -> Int
self.sink.file_rotation_failures() self.sink.file_rotation_failures()
} }
pub fn ConfiguredLogger::file_state(self : ConfiguredLogger) -> FileSinkState {
self.sink.file_state()
}
fn expect_object( fn expect_object(
value : @json_parser.JsonValue, value : @json_parser.JsonValue,
context : String, context : String,
+26
View File
@@ -62,6 +62,18 @@ pub struct FileRotation {
max_backups : Int max_backups : Int
} }
pub struct FileSinkState {
path : String
available : Bool
append : Bool
auto_flush : Bool
rotation : FileRotation?
open_failures : Int
write_failures : Int
flush_failures : Int
rotation_failures : Int
}
pub fn file_rotation(max_bytes : Int, max_backups~ : Int = 1) -> FileRotation { pub fn file_rotation(max_bytes : Int, max_backups~ : Int = 1) -> FileRotation {
{ {
max_bytes: if max_bytes <= 0 { 1 } else { max_bytes }, max_bytes: if max_bytes <= 0 { 1 } else { max_bytes },
@@ -177,6 +189,20 @@ pub fn FileSink::flush_failures(self : FileSink) -> Int {
self.flush_failures.val self.flush_failures.val
} }
pub fn FileSink::state(self : FileSink) -> FileSinkState {
{
path: self.path,
available: self.is_available(),
append: self.append.val,
auto_flush: self.auto_flush.val,
rotation: self.rotation.val,
open_failures: self.open_failures.val,
write_failures: self.write_failures.val,
flush_failures: self.flush_failures.val,
rotation_failures: self.rotation_failures.val,
}
}
pub fn FileSink::reopen(self : FileSink, append~ : Bool? = None) -> Bool { pub fn FileSink::reopen(self : FileSink, append~ : Bool? = None) -> Bool {
let append_mode = append.unwrap_or(self.append.val) let append_mode = append.unwrap_or(self.append.val)
self.append.val = append_mode self.append.val = append_mode
+2 -1
View File
@@ -211,8 +211,9 @@ if native_files_supported() {
- `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 exposes `state()` for reading a single snapshot that includes path, availability, append policy, auto-flush flag, rotation config, and all current failure counters.
- `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_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()`, 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_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()`, 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
@@ -39,6 +39,7 @@ version 0.3.0
- 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 explicit `reopen_with_current_policy()` / `file_reopen_with_current_policy()` helpers so stored append-policy reopen is discoverable instead of implicit
- feat: add `reopen_append()` / `reopen_truncate()` and configured-logger forwarding helpers so common append-vs-truncate reopen flows do not need raw `Some(true/false)` calls - feat: add `reopen_append()` / `reopen_truncate()` and configured-logger forwarding helpers so common append-vs-truncate reopen flows do not need raw `Some(true/false)` calls
- feat: add `FileSinkState`, `FileSink::state()`, and `file_state()` helpers so file-sink runtime policy and failure counters can be read as one snapshot
- 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