From 4b258209e6c6465b8556d4ba822deeba18de5899 Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Sun, 10 May 2026 13:41:23 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20file=20policy=20JSON=20export?= =?UTF-8?q?=20helpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + bitlogger/BitLogger_test.mbt | 21 +++++++++++++++++++++ bitlogger/README.mbt.md | 1 + bitlogger/config.mbt | 25 +++++++++++++++++++++++++ docs/README-en.md | 1 + docs/changes/0.3.0.md | 3 +++ 6 files changed, 52 insertions(+) diff --git a/README.md b/README.md index 44e9312..9105f00 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,7 @@ match logger.file_runtime_state() { - `ConfiguredLogger` 也支持 `file_set_policy(...)`,可一次性改写配置式 file sink 的当前运行期策略 - `ConfiguredLogger` 也支持 `file_reset_failure_counters()`,可在配置式 file sink 上统一清空失败计数 - `ConfiguredLogger` 也支持 `file_reset_policy()`,可将配置式 file sink 的运行期策略恢复到初始配置 +- `file_sink_policy_to_json(...)`、`stringify_file_sink_policy(...)` 也可将独立 file policy 直接导出为 JSON,便于策略快照、配置对比或诊断上报 - `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` diff --git a/bitlogger/BitLogger_test.mbt b/bitlogger/BitLogger_test.mbt index a827ecc..149c3af 100644 --- a/bitlogger/BitLogger_test.mbt +++ b/bitlogger/BitLogger_test.mbt @@ -301,6 +301,27 @@ test "runtime file state json helpers stringify queue snapshots" { ) } +test "file sink policy json helpers stringify stable policies" { + inspect( + @json_parser.stringify( + file_sink_policy_to_json( + FileSinkPolicy::new( + append=false, + auto_flush=true, + rotation=Some(file_rotation(96, max_backups=3)), + ), + ), + ), + content="{\"append\":false,\"auto_flush\":true,\"rotation\":{\"max_backups\":3,\"max_bytes\":96}}", + ) + inspect( + stringify_file_sink_policy( + FileSinkPolicy::new(append=true, auto_flush=false, rotation=None), + ), + content="{\"append\":true,\"auto_flush\":false,\"rotation\":null}", + ) +} + test "configured logger reports disabled rotation when file sink has none" { let logger = build_logger( LoggerConfig::new( diff --git a/bitlogger/README.mbt.md b/bitlogger/README.mbt.md index ced5a41..ca8bdc5 100644 --- a/bitlogger/README.mbt.md +++ b/bitlogger/README.mbt.md @@ -247,6 +247,7 @@ test { - `set_policy(...)` applies append, auto-flush, and rotation as a bundled runtime update / `set_policy(...)` 可将 append、auto-flush、rotation 作为一组运行期策略一次性写回 - `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_policy_to_json(...)` / `stringify_file_sink_policy(...)` also export standalone file policy snapshots as JSON / `file_sink_policy_to_json(...)` / `stringify_file_sink_policy(...)` 也可将独立 file policy 快照直接导出为 JSON - `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 健康状态 diff --git a/bitlogger/config.mbt b/bitlogger/config.mbt index 4097c60..6403920 100644 --- a/bitlogger/config.mbt +++ b/bitlogger/config.mbt @@ -153,6 +153,31 @@ pub fn RuntimeFileState::new( { file, queued, pending_count, dropped_count } } +fn file_sink_policy_to_json_value(policy : FileSinkPolicy) -> @json_parser.JsonValue { + let obj : Map[String, @json_parser.JsonValue] = { + "append": @json_parser.JsonValue::Bool(policy.append), + "auto_flush": @json_parser.JsonValue::Bool(policy.auto_flush), + } + match policy.rotation { + None => obj["rotation"] = @json_parser.JsonValue::Null + Some(rotation) => obj["rotation"] = file_rotation_config_to_json(rotation) + } + @json_parser.JsonValue::Object(obj) +} + +pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> @json_parser.JsonValue { + file_sink_policy_to_json_value(policy) +} + +pub fn stringify_file_sink_policy(policy : FileSinkPolicy, pretty~ : Bool = false) -> String { + let value = file_sink_policy_to_json_value(policy) + if pretty { + @json_parser.stringify_pretty(value, 2) + } else { + @json_parser.stringify(value) + } +} + fn file_sink_state_to_json_value(state : FileSinkState) -> @json_parser.JsonValue { let obj : Map[String, @json_parser.JsonValue] = { "path": @json_parser.JsonValue::String(state.path), diff --git a/docs/README-en.md b/docs/README-en.md index e8406ca..c768bb1 100644 --- a/docs/README-en.md +++ b/docs/README-en.md @@ -243,6 +243,7 @@ match logger.file_runtime_state() { - `ConfiguredLogger` also exposes `file_set_policy(...)` for applying a bundled runtime file policy through the config-built control surface. - `ConfiguredLogger` also exposes `file_reset_failure_counters()` for clearing file failure counters through the config-built control surface. - `ConfiguredLogger` also exposes `file_reset_policy()` for restoring runtime file policy back to the initial config values. +- `file_sink_policy_to_json(...)` and `stringify_file_sink_policy(...)` can export standalone file-policy snapshots directly as JSON for policy diffing, diagnostics, or reporting. - `file_sink_state_to_json(...)`, `stringify_file_sink_state(...)`, `runtime_file_state_to_json(...)`, and `stringify_runtime_file_state(...)` can export file and queued-file snapshots directly as JSON for diagnostics or reporting. - `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`. diff --git a/docs/changes/0.3.0.md b/docs/changes/0.3.0.md index 806b03d..e51a4ab 100644 --- a/docs/changes/0.3.0.md +++ b/docs/changes/0.3.0.md @@ -47,6 +47,7 @@ version 0.3.0 - feat: add `policy()` / `default_policy()` and configured-logger forwarding helpers so current runtime file policy and original defaults can be read explicitly - feat: add `set_policy()` / `file_set_policy()` so bundled append, auto-flush, and rotation policy can be applied in one runtime operation - feat: add `policy_matches_default()` / `file_policy_matches_default()` so runtime file policy drift can be checked explicitly +- feat: add `file_sink_policy_to_json(...)` and `stringify_file_sink_policy(...)` so standalone file-policy snapshots can be exported directly as JSON - 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 @@ -67,6 +68,7 @@ version 0.3.0 - test: cover path and auto-flush introspection for direct and config-built file sinks - test: cover rotation introspection for direct and config-built file sinks with and without configured rotation - test: cover runtime auto-flush and rotation policy mutation for direct and config-built file sinks +- test: cover standalone file-policy JSON export helpers with and without rotation - test: cover append-policy setter behavior and confirm subsequent reopen calls inherit the updated append mode - test: cover split sink predicate routing and level-based routing behavior - test: cover `bind(...)` context composition and `fields(...)` helper behavior @@ -89,6 +91,7 @@ version 0.3.0 - docs: document file path and auto-flush introspection helpers - docs: document rotation introspection helpers for direct and config-built file sinks - docs: document runtime file-policy setter helpers for direct and config-built file sinks +- docs: document standalone file-policy JSON export helpers alongside file state snapshot exports - docs: clarify explicit append-policy setter semantics for direct and config-built file sinks - docs: add split sink examples for level-based routing - docs: add `bind(...)` examples for reusable context binding