From 7f4aa199ea6bbecab0d8e5a9053178a727ddbf5a Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Sun, 10 May 2026 12:27:15 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Forward=20file=20sink=20helpers=20t?= =?UTF-8?q?hrough=20configured=20logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + bitlogger/BitLogger_test.mbt | 37 ++++++++++++++++++++++ bitlogger/README.mbt.md | 1 + bitlogger/config.mbt | 60 ++++++++++++++++++++++++++++++++++++ docs/README-en.md | 1 + docs/changes/0.3.0.md | 3 ++ 6 files changed, 103 insertions(+) diff --git a/README.md b/README.md index 8f2807b..126cc79 100644 --- a/README.md +++ b/README.md @@ -222,6 +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` - `sink.rotation` 当前支持 `max_bytes` 与 `max_backups`,提供基础 size-based rotation 和 backup retention - `file_sink(...)` 还提供 `reopen()`、`open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()`,用于基础可观测性 +- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制 - `sink.text_formatter.template` 当前支持固定 token:`{timestamp}`、`{timestamp_ms}`、`{level}`、`{target}`、`{message}`、`{fields}` - 当前可由配置直接组装的 sink 类型:`console`、`json_console`、`text_console`、`file` - `queue` 会作为显式包装层附着在最终 sink 外侧;这仍然是同步 drain 模型,不是 async runtime diff --git a/bitlogger/BitLogger_test.mbt b/bitlogger/BitLogger_test.mbt index 55c80fd..c3a078d 100644 --- a/bitlogger/BitLogger_test.mbt +++ b/bitlogger/BitLogger_test.mbt @@ -194,3 +194,40 @@ test "configured logger reports dropped_count for bounded queue" { inspect(logger.dropped_count(), content="2") inspect(logger.flush(), content="2") } + +test "configured logger exposes file sink observability helpers" { + let logger = build_logger( + LoggerConfig::new( + sink=SinkConfig::new(kind=SinkKind::File, path="config-file.log"), + ), + ) + inspect(logger.file_available() == native_files_supported(), content="true") + inspect(logger.file_open_failures(), content=if logger.file_available() { "0" } else { "1" }) + inspect(logger.file_write_failures(), content="0") + inspect(logger.file_flush_failures(), content="0") + inspect(logger.file_rotation_failures(), content="0") + ignore(logger.close()) +} + +test "configured logger can reopen built file sink" { + let logger = build_logger( + LoggerConfig::new( + sink=SinkConfig::new(kind=SinkKind::File, path="config-reopen.log"), + ), + ) + if logger.file_available() { + inspect(logger.close(), content="true") + inspect(logger.file_reopen(), content="true") + inspect(logger.file_available(), content="true") + inspect(logger.file_open_failures(), content="0") + logger.info("reopened from config") + inspect(logger.file_write_failures(), content="0") + inspect(logger.close(), content="true") + } else { + inspect(logger.file_open_failures(), content="1") + logger.info("dropped") + inspect(logger.file_write_failures(), content="1") + inspect(logger.file_reopen(), content="false") + inspect(logger.file_open_failures(), content="2") + } +} diff --git a/bitlogger/README.mbt.md b/bitlogger/README.mbt.md index 448ce0a..e04998c 100644 --- a/bitlogger/README.mbt.md +++ b/bitlogger/README.mbt.md @@ -222,4 +222,5 @@ test { - 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()` 可显式重开当前文件句柄 - `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 与失败计数访问器 - current scope is observability-first, not a full self-healing sink runtime / 当前定位仍以可观测性优先,不是完整自愈型 sink runtime diff --git a/bitlogger/config.mbt b/bitlogger/config.mbt index c6f3ed3..fba92f9 100644 --- a/bitlogger/config.mbt +++ b/bitlogger/config.mbt @@ -223,6 +223,46 @@ pub fn RuntimeSink::file_available(self : RuntimeSink) -> Bool { } } +pub fn RuntimeSink::file_reopen(self : RuntimeSink, append~ : Bool? = None) -> Bool { + match self { + File(sink) => sink.reopen(append=append) + QueuedFile(sink) => sink.sink.reopen(append=append) + _ => false + } +} + +pub fn RuntimeSink::file_open_failures(self : RuntimeSink) -> Int { + match self { + File(sink) => sink.open_failures() + QueuedFile(sink) => sink.sink.open_failures() + _ => 0 + } +} + +pub fn RuntimeSink::file_write_failures(self : RuntimeSink) -> Int { + match self { + File(sink) => sink.write_failures() + QueuedFile(sink) => sink.sink.write_failures() + _ => 0 + } +} + +pub fn RuntimeSink::file_flush_failures(self : RuntimeSink) -> Int { + match self { + File(sink) => sink.flush_failures() + QueuedFile(sink) => sink.sink.flush_failures() + _ => 0 + } +} + +pub fn RuntimeSink::file_rotation_failures(self : RuntimeSink) -> Int { + match self { + File(sink) => sink.rotation_failures() + QueuedFile(sink) => sink.sink.rotation_failures() + _ => 0 + } +} + pub type ConfiguredLogger = Logger[RuntimeSink] pub fn ConfiguredLogger::flush(self : ConfiguredLogger) -> Int { @@ -249,6 +289,26 @@ pub fn ConfiguredLogger::file_available(self : ConfiguredLogger) -> Bool { self.sink.file_available() } +pub fn ConfiguredLogger::file_reopen(self : ConfiguredLogger, append~ : Bool? = None) -> Bool { + self.sink.file_reopen(append=append) +} + +pub fn ConfiguredLogger::file_open_failures(self : ConfiguredLogger) -> Int { + self.sink.file_open_failures() +} + +pub fn ConfiguredLogger::file_write_failures(self : ConfiguredLogger) -> Int { + self.sink.file_write_failures() +} + +pub fn ConfiguredLogger::file_flush_failures(self : ConfiguredLogger) -> Int { + self.sink.file_flush_failures() +} + +pub fn ConfiguredLogger::file_rotation_failures(self : ConfiguredLogger) -> Int { + self.sink.file_rotation_failures() +} + fn expect_object( value : @json_parser.JsonValue, context : String, diff --git a/docs/README-en.md b/docs/README-en.md index ada88a7..4671c8d 100644 --- a/docs/README-en.md +++ b/docs/README-en.md @@ -207,6 +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`. - `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. +- `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. - `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`. - `queue` remains a synchronous bounded wrapper around the final sink, not an async runtime. diff --git a/docs/changes/0.3.0.md b/docs/changes/0.3.0.md index 2fc7dd1..7d0a8ff 100644 --- a/docs/changes/0.3.0.md +++ b/docs/changes/0.3.0.md @@ -30,6 +30,7 @@ version 0.3.0 - feat: add `FileRotation`, `file_rotation(...)`, and size-based file rotation with retained backups for `file_sink(...)` - 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: forward file sink reopen and failure-counter helpers through `RuntimeSink` and `ConfiguredLogger` - 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 @@ -44,6 +45,7 @@ version 0.3.0 - test: cover template-based formatter rendering and disabled token 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 config-built file logger reopen and failure-counter helper access - test: cover split sink predicate routing and level-based routing behavior - test: cover `bind(...)` context composition and `fields(...)` helper behavior - test: add async logger lifecycle, config roundtrip, and batching/flush policy test seeds @@ -59,6 +61,7 @@ version 0.3.0 - docs: update formatter examples to demonstrate template-based text rendering - docs: update file sink examples to demonstrate rotation and backup retention - 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: add split sink examples for level-based routing - docs: add `bind(...)` examples for reusable context binding - docs: update root README and English README with async adapter notes and current scope