Add file policy JSON export helpers

This commit is contained in:
Nanaloveyuki
2026-05-10 13:41:23 +08:00
parent 0877ab02c7
commit 4b258209e6
6 changed files with 52 additions and 0 deletions
+1
View File
@@ -259,6 +259,7 @@ match logger.file_runtime_state() {
- `ConfiguredLogger` 也支持 `file_set_policy(...)`,可一次性改写配置式 file sink 的当前运行期策略 - `ConfiguredLogger` 也支持 `file_set_policy(...)`,可一次性改写配置式 file sink 的当前运行期策略
- `ConfiguredLogger` 也支持 `file_reset_failure_counters()`,可在配置式 file sink 上统一清空失败计数 - `ConfiguredLogger` 也支持 `file_reset_failure_counters()`,可在配置式 file sink 上统一清空失败计数
- `ConfiguredLogger` 也支持 `file_reset_policy()`,可将配置式 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,便于排障或上报 - `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.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`
+21
View File
@@ -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" { test "configured logger reports disabled rotation when file sink has none" {
let logger = build_logger( let logger = build_logger(
LoggerConfig::new( LoggerConfig::new(
+1
View File
@@ -247,6 +247,7 @@ test {
- `set_policy(...)` applies append, auto-flush, and rotation as a bundled runtime update / `set_policy(...)` 可将 append、auto-flush、rotation 作为一组运行期策略一次性写回 - `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_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 初始默认策略 - `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 - `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 的基础策略 - `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 健康状态
+25
View File
@@ -153,6 +153,31 @@ pub fn RuntimeFileState::new(
{ file, queued, pending_count, dropped_count } { 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 { fn file_sink_state_to_json_value(state : FileSinkState) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = { let obj : Map[String, @json_parser.JsonValue] = {
"path": @json_parser.JsonValue::String(state.path), "path": @json_parser.JsonValue::String(state.path),
+1
View File
@@ -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_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_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. - `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. - `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}`. - `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`.
+3
View File
@@ -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 `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 `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 `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 `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
@@ -67,6 +68,7 @@ version 0.3.0
- test: cover path and auto-flush introspection for direct and config-built file sinks - 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 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 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 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 split sink predicate routing and level-based routing behavior
- test: cover `bind(...)` context composition and `fields(...)` helper 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 file path and auto-flush introspection helpers
- docs: document rotation introspection helpers for direct and config-built file sinks - 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 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: clarify explicit append-policy setter semantics for direct and config-built file sinks
- docs: add split sink examples for level-based routing - docs: add split sink examples for level-based routing
- docs: add `bind(...)` examples for reusable context binding - docs: add `bind(...)` examples for reusable context binding