Add explicit configured file flush and close helpers

This commit is contained in:
Nanaloveyuki
2026-05-10 12:31:00 +08:00
parent 7f4aa199ea
commit 12730fded8
6 changed files with 76 additions and 3 deletions
+1 -1
View File
@@ -222,7 +222,7 @@ if native_files_supported() {
- 已支持字段:`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()``open_failures()``write_failures()``flush_failures()``rotation_failures()`,用于基础可观测性
- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制 - `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()``file_flush()``file_close()` 与对应 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
+40
View File
@@ -231,3 +231,43 @@ test "configured logger can reopen built file sink" {
inspect(logger.file_open_failures(), content="2") inspect(logger.file_open_failures(), content="2")
} }
} }
test "configured logger exposes file flush and close helpers" {
let logger = build_logger(
LoggerConfig::new(
sink=SinkConfig::new(kind=SinkKind::File, path="config-control.log"),
),
)
if logger.file_available() {
logger.info("before flush")
inspect(logger.file_flush(), content="true")
inspect(logger.file_close(), content="true")
inspect(logger.file_available(), content="false")
inspect(logger.file_flush(), content="false")
} else {
inspect(logger.file_flush(), content="false")
inspect(logger.file_close(), content="false")
}
}
test "configured queued file logger flushes queue through file helper" {
let logger = build_logger(
LoggerConfig::new(
sink=SinkConfig::new(kind=SinkKind::File, path="config-queued-file.log"),
queue=Some(QueueConfig::new(4, overflow=QueueOverflowPolicy::DropNewest)),
),
)
logger.info("one")
logger.info("two")
if logger.file_available() {
inspect(logger.pending_count(), content="2")
inspect(logger.file_flush(), content="true")
inspect(logger.pending_count(), content="0")
inspect(logger.file_close(), content="true")
} else {
inspect(logger.pending_count(), content="2")
inspect(logger.file_flush(), content="false")
inspect(logger.pending_count(), content="0")
inspect(logger.file_close(), content="false")
}
}
+1 -1
View File
@@ -222,5 +222,5 @@ test {
- 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 / `FileSink::reopen()` 可显式重开当前文件句柄
- `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 and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen 与失败计数访问器 - `ConfiguredLogger` also forwards file reopen, flush, close, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close 与失败计数访问器
- 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
+30
View File
@@ -231,6 +231,28 @@ pub fn RuntimeSink::file_reopen(self : RuntimeSink, append~ : Bool? = None) -> B
} }
} }
pub fn RuntimeSink::file_flush(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.flush()
QueuedFile(sink) => {
ignore(sink.flush())
sink.sink.flush()
}
_ => false
}
}
pub fn RuntimeSink::file_close(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.close()
QueuedFile(sink) => {
ignore(sink.flush())
sink.sink.close()
}
_ => false
}
}
pub fn RuntimeSink::file_open_failures(self : RuntimeSink) -> Int { pub fn RuntimeSink::file_open_failures(self : RuntimeSink) -> Int {
match self { match self {
File(sink) => sink.open_failures() File(sink) => sink.open_failures()
@@ -293,6 +315,14 @@ 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_flush(self : ConfiguredLogger) -> Bool {
self.sink.file_flush()
}
pub fn ConfiguredLogger::file_close(self : ConfiguredLogger) -> Bool {
self.sink.file_close()
}
pub fn ConfiguredLogger::file_open_failures(self : ConfiguredLogger) -> Int { pub fn ConfiguredLogger::file_open_failures(self : ConfiguredLogger) -> Int {
self.sink.file_open_failures() self.sink.file_open_failures()
} }
+1 -1
View File
@@ -207,7 +207,7 @@ if native_files_supported() {
- 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()`, `open_failures()`, `write_failures()`, `flush_failures()`, and `rotation_failures()` for basic observability.
- `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()` 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_flush()`, `file_close()`, 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.
+3
View File
@@ -31,6 +31,7 @@ version 0.3.0
- feat: support `sink.rotation.max_bytes` and `sink.rotation.max_backups` in JSON logger config - feat: support `sink.rotation.max_bytes` and `sink.rotation.max_backups` in JSON logger config
- feat: add `FileSink::reopen()` and basic file sink failure counters via `open_failures()`, `write_failures()`, and `flush_failures()` - feat: add `FileSink::reopen()` and basic file sink failure counters via `open_failures()`, `write_failures()`, and `flush_failures()`
- feat: forward file sink reopen and failure-counter helpers through `RuntimeSink` and `ConfiguredLogger` - feat: forward file sink reopen and failure-counter helpers through `RuntimeSink` and `ConfiguredLogger`
- feat: add explicit `file_flush()` and `file_close()` helpers for config-built file sinks, including queued file sink drain-before-close behavior
- 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
@@ -46,6 +47,7 @@ version 0.3.0
- test: cover file rotation config parsing, config roundtrip, and native rotation behavior - test: cover file rotation config parsing, config roundtrip, and native rotation behavior
- test: cover file sink reopen behavior and backend-dependent failure counter paths - test: cover file sink reopen behavior and backend-dependent failure counter paths
- test: cover config-built file logger reopen and failure-counter helper access - test: cover config-built file logger reopen and failure-counter helper access
- test: cover config-built file logger flush/close helpers and queued-file drain semantics
- 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
- test: add async logger lifecycle, config roundtrip, and batching/flush policy test seeds - test: add async logger lifecycle, config roundtrip, and batching/flush policy test seeds
@@ -62,6 +64,7 @@ version 0.3.0
- docs: update file sink examples to demonstrate rotation and backup retention - docs: update file sink examples to demonstrate rotation and backup retention
- docs: document file sink reopen and observability counters in README variants - docs: document file sink reopen and observability counters in README variants
- docs: clarify that config-built file loggers keep file control and observability helpers - docs: clarify that config-built file loggers keep file control and observability helpers
- docs: document explicit file flush/close helpers for config-built file loggers
- 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
- docs: update root README and English README with async adapter notes and current scope - docs: update root README and English README with async adapter notes and current scope