Add file policy reset helpers

This commit is contained in:
Nanaloveyuki
2026-05-10 13:20:08 +08:00
parent c637631a30
commit b1ffd9021b
8 changed files with 100 additions and 0 deletions
+37
View File
@@ -355,9 +355,46 @@ test "configured logger file setters update file sink policy state" {
inspect(state.append, content="false")
inspect(state.auto_flush, content="false")
inspect(state.rotation is None, content="true")
inspect(logger.file_reset_policy(), content="true")
inspect(logger.file_append_mode(), content="true")
inspect(logger.file_auto_flush(), content="true")
inspect(logger.file_rotation_config() is None, content="true")
ignore(logger.close())
}
test "configured logger reset policy restores configured file defaults" {
let logger = build_logger(
LoggerConfig::new(
sink=SinkConfig::new(
kind=SinkKind::File,
path="config-reset-policy.log",
append=false,
auto_flush=false,
rotation=Some(file_rotation(36, max_backups=2)),
),
),
)
inspect(logger.file_set_append_mode(true), content="true")
inspect(logger.file_set_auto_flush(true), content="true")
inspect(logger.file_clear_rotation(), content="true")
inspect(logger.file_reset_policy(), content="true")
inspect(logger.file_append_mode(), content="false")
inspect(logger.file_auto_flush(), content="false")
match logger.file_rotation_config() {
Some(rotation) => {
inspect(rotation.max_bytes, content="36")
inspect(rotation.max_backups, content="2")
}
None => inspect(false, content="true")
}
ignore(logger.close())
}
test "configured non-file logger cannot reset file policy" {
let logger = build_logger(LoggerConfig::new(sink=SinkConfig::new(kind=SinkKind::Console)))
inspect(logger.file_reset_policy(), content="false")
}
test "configured logger can reopen built file sink" {
let logger = build_logger(
LoggerConfig::new(
+26
View File
@@ -155,6 +155,32 @@ test "file sink setters update auto flush and rotation state" {
inspect(state.append, content="false")
inspect(state.auto_flush, content="false")
inspect(state.rotation is None, content="true")
sink.reset_policy()
inspect(sink.append_mode(), content="true")
inspect(sink.auto_flush_enabled(), content="true")
inspect(sink.rotation_config() is None, content="true")
}
test "file sink reset policy restores configured defaults" {
let sink = file_sink(
"bitlogger-reset-policy.log",
append=false,
auto_flush=false,
rotation=Some(file_rotation(24, max_backups=3)),
)
sink.set_append_mode(true)
sink.set_auto_flush(true)
sink.clear_rotation()
sink.reset_policy()
inspect(sink.append_mode(), content="false")
inspect(sink.auto_flush_enabled(), content="false")
match sink.rotation_config() {
Some(rotation) => {
inspect(rotation.max_bytes, content="24")
inspect(rotation.max_backups, content="3")
}
None => inspect(false, content="true")
}
}
test "file sink tracks rotation failures on unavailable backend" {
+2
View File
@@ -243,10 +243,12 @@ test {
- `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 快照
- `reset_failure_counters()` clears the open/write/flush/rotation counters after diagnostics or recovery / `reset_failure_counters()` 可在排障或恢复后清空 open/write/flush/rotation 失败计数
- `reset_policy()` restores append, auto-flush, and rotation back to the sink's original defaults / `reset_policy()` 可将 append、auto-flush、rotation 恢复到 sink 初始默认策略
- `file_sink_state_to_json(...)` / `stringify_file_sink_state(...)` and `runtime_file_state_to_json(...)` / `stringify_runtime_file_state(...)` export snapshots as JSON / `file_sink_state_to_json(...)` / `stringify_file_sink_state(...)` 与 `runtime_file_state_to_json(...)` / `stringify_runtime_file_state(...)` 可将快照直接导出为 JSON
- `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 健康状态
- `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 与失败计数访问器
- `ConfiguredLogger::file_runtime_state()` also reports whether a file sink is queue-wrapped and exposes the outer queue pending/drop counters together with the inner file snapshot / `ConfiguredLogger::file_runtime_state()` 还可报告 file sink 是否被 queue 包装,并将外层 queue 的 pending/drop 计数与内层 file 快照一起返回
- `ConfiguredLogger::file_reset_failure_counters()` also clears file failure counters through the config-built control surface / `ConfiguredLogger::file_reset_failure_counters()` 也可通过配置式控制面清空 file 失败计数
- `ConfiguredLogger::file_reset_policy()` also restores runtime file policy back to the initial config values / `ConfiguredLogger::file_reset_policy()` 也可将运行期 file 策略恢复到初始配置值
- current scope is observability-first, not a full self-healing sink runtime / 当前定位仍以可观测性优先,不是完整自愈型 sink runtime
+18
View File
@@ -484,6 +484,20 @@ pub fn RuntimeSink::file_reset_failure_counters(self : RuntimeSink) -> Bool {
}
}
pub fn RuntimeSink::file_reset_policy(self : RuntimeSink) -> Bool {
match self {
File(sink) => {
sink.reset_policy()
true
}
QueuedFile(sink) => {
sink.sink.reset_policy()
true
}
_ => false
}
}
pub fn RuntimeSink::file_state(self : RuntimeSink) -> FileSinkState {
match self {
File(sink) => sink.state()
@@ -626,6 +640,10 @@ pub fn ConfiguredLogger::file_reset_failure_counters(self : ConfiguredLogger) ->
self.sink.file_reset_failure_counters()
}
pub fn ConfiguredLogger::file_reset_policy(self : ConfiguredLogger) -> Bool {
self.sink.file_reset_policy()
}
pub fn ConfiguredLogger::file_state(self : ConfiguredLogger) -> FileSinkState {
self.sink.file_state()
}
+12
View File
@@ -47,10 +47,13 @@ pub impl Sink for JsonConsoleSink with write(self, rec) {
pub struct FileSink {
path : String
append : Ref[Bool]
default_append : Bool
handle : Ref[FileHandle?]
formatter : RecordFormatter
auto_flush : Ref[Bool]
default_auto_flush : Bool
rotation : Ref[FileRotation?]
default_rotation : FileRotation?
open_failures : Ref[Int]
write_failures : Ref[Int]
flush_failures : Ref[Int]
@@ -122,10 +125,13 @@ pub fn file_sink(
{
path,
append: Ref::new(append),
default_append: append,
handle: Ref::new(handle),
formatter,
auto_flush: Ref::new(auto_flush),
default_auto_flush: auto_flush,
rotation: Ref::new(rotation),
default_rotation: rotation,
open_failures: Ref::new(if handle is Some(_) { 0 } else { 1 }),
write_failures: Ref::new(0),
flush_failures: Ref::new(0),
@@ -220,6 +226,12 @@ pub fn FileSink::reset_failure_counters(self : FileSink) -> Unit {
self.rotation_failures.val = 0
}
pub fn FileSink::reset_policy(self : FileSink) -> Unit {
self.append.val = self.default_append
self.auto_flush.val = self.default_auto_flush
self.rotation.val = self.default_rotation
}
pub fn FileSink::state(self : FileSink) -> FileSinkState {
{
path: self.path,