diff --git a/README.md b/README.md index 5c7089d..0491fc6 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,8 @@ if native_files_supported() { - `file_sink(...)` 当前还提供 `append_mode()`;`reopen(append=...)` 在显式传值时会更新后续 reopen 使用的 append 策略 - `file_sink(...)` 也可直接读取 `path()` 与 `auto_flush_enabled()` 等基础 file 策略状态 - `file_sink(...)` 还提供 `rotation_enabled()` 与 `rotation_config()`,可直接查询当前 rotation 策略是否启用及其参数 -- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()`、`file_flush()`、`file_close()`、`file_append_mode()`、`file_path()`、`file_auto_flush()`、`file_rotation_enabled()`、`file_rotation_config()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制 +- `file_sink(...)` 也支持 `set_auto_flush(...)`、`set_rotation(...)`、`clear_rotation()`,可在运行期调整基础写出策略 +- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()`、`file_flush()`、`file_close()`、`file_append_mode()`、`file_path()`、`file_auto_flush()`、`file_rotation_enabled()`、`file_rotation_config()`,以及 `file_set_auto_flush(...)`、`file_set_rotation(...)`、`file_clear_rotation()` 与对应 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 68edd4a..126f6e8 100644 --- a/bitlogger/BitLogger_test.mbt +++ b/bitlogger/BitLogger_test.mbt @@ -236,6 +236,32 @@ test "configured logger reports disabled rotation when file sink has none" { ignore(logger.close()) } +test "configured logger file setters update file sink policy state" { + let logger = build_logger( + LoggerConfig::new( + sink=SinkConfig::new(kind=SinkKind::File, path="config-setters.log"), + queue=Some(QueueConfig::new(2, overflow=QueueOverflowPolicy::DropNewest)), + ), + ) + inspect(logger.file_auto_flush(), content="true") + inspect(logger.file_rotation_enabled(), content="false") + inspect(logger.file_set_auto_flush(false), content="true") + inspect(logger.file_auto_flush(), content="false") + inspect(logger.file_set_rotation(Some(file_rotation(48, max_backups=4))), content="true") + inspect(logger.file_rotation_enabled(), content="true") + match logger.file_rotation_config() { + Some(rotation) => { + inspect(rotation.max_bytes, content="48") + inspect(rotation.max_backups, content="4") + } + None => inspect(false, content="true") + } + inspect(logger.file_clear_rotation(), content="true") + inspect(logger.file_rotation_enabled(), content="false") + inspect(logger.file_rotation_config() is None, content="true") + ignore(logger.close()) +} + test "configured logger can reopen built file sink" { let logger = build_logger( LoggerConfig::new( diff --git a/bitlogger/BitLogger_wbtest.mbt b/bitlogger/BitLogger_wbtest.mbt index 37a1b80..48950e8 100644 --- a/bitlogger/BitLogger_wbtest.mbt +++ b/bitlogger/BitLogger_wbtest.mbt @@ -122,6 +122,26 @@ test "file sink rotation config normalizes invalid inputs" { } } +test "file sink setters update auto flush and rotation state" { + let sink = file_sink("bitlogger-setters.log") + inspect(sink.auto_flush_enabled(), content="true") + inspect(sink.rotation_enabled(), content="false") + sink.set_auto_flush(false) + inspect(sink.auto_flush_enabled(), content="false") + sink.set_rotation(Some(file_rotation(32, max_backups=2))) + inspect(sink.rotation_enabled(), content="true") + match sink.rotation_config() { + Some(config) => { + inspect(config.max_bytes, content="32") + inspect(config.max_backups, content="2") + } + None => inspect(false, content="true") + } + sink.clear_rotation() + inspect(sink.rotation_enabled(), content="false") + inspect(sink.rotation_config() is None, content="true") +} + test "file sink tracks rotation failures on unavailable backend" { let sink = file_sink("bitlogger-rotate.log", rotation=Some(file_rotation(1, max_backups=1))) sink.write(record(Level::Info, "a")) diff --git a/bitlogger/README.mbt.md b/bitlogger/README.mbt.md index 35f13de..7b45714 100644 --- a/bitlogger/README.mbt.md +++ b/bitlogger/README.mbt.md @@ -224,6 +224,7 @@ test { - `append_mode()` exposes the current append policy, and `reopen(append=...)` updates that policy for later reopen calls / `append_mode()` 可读取当前 append 策略,`reopen(append=...)` 会更新后续 reopen 复用的 append 策略 - `path()` and `auto_flush_enabled()` expose core file sink policy state / `path()` 与 `auto_flush_enabled()` 可读取 file sink 的基础策略状态 - `rotation_enabled()` and `rotation_config()` expose whether rotation is active and which settings are currently applied / `rotation_enabled()` 与 `rotation_config()` 可读取 rotation 是否启用及当前配置参数 +- `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, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close、append-mode、path、auto-flush、rotation 配置与失败计数访问器 +- `ConfiguredLogger` also forwards file reopen, flush, close, append-mode, path, auto-flush, rotation-config, setter, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close、append-mode、path、auto-flush、rotation 配置、setter 与失败计数访问器 - 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 505e61b..3d1cab6 100644 --- a/bitlogger/config.mbt +++ b/bitlogger/config.mbt @@ -271,6 +271,48 @@ pub fn RuntimeSink::file_rotation_config(self : RuntimeSink) -> FileRotation? { } } +pub fn RuntimeSink::file_set_auto_flush(self : RuntimeSink, enabled : Bool) -> Bool { + match self { + File(sink) => { + sink.set_auto_flush(enabled) + true + } + QueuedFile(sink) => { + sink.sink.set_auto_flush(enabled) + true + } + _ => false + } +} + +pub fn RuntimeSink::file_set_rotation(self : RuntimeSink, rotation : FileRotation?) -> Bool { + match self { + File(sink) => { + sink.set_rotation(rotation) + true + } + QueuedFile(sink) => { + sink.sink.set_rotation(rotation) + true + } + _ => false + } +} + +pub fn RuntimeSink::file_clear_rotation(self : RuntimeSink) -> Bool { + match self { + File(sink) => { + sink.clear_rotation() + true + } + QueuedFile(sink) => { + sink.sink.clear_rotation() + true + } + _ => false + } +} + pub fn RuntimeSink::file_flush(self : RuntimeSink) -> Bool { match self { File(sink) => sink.flush() @@ -375,6 +417,21 @@ pub fn ConfiguredLogger::file_rotation_config(self : ConfiguredLogger) -> FileRo self.sink.file_rotation_config() } +pub fn ConfiguredLogger::file_set_auto_flush(self : ConfiguredLogger, enabled : Bool) -> Bool { + self.sink.file_set_auto_flush(enabled) +} + +pub fn ConfiguredLogger::file_set_rotation( + self : ConfiguredLogger, + rotation : FileRotation?, +) -> Bool { + self.sink.file_set_rotation(rotation) +} + +pub fn ConfiguredLogger::file_clear_rotation(self : ConfiguredLogger) -> Bool { + self.sink.file_clear_rotation() +} + pub fn ConfiguredLogger::file_flush(self : ConfiguredLogger) -> Bool { self.sink.file_flush() } diff --git a/bitlogger/sinks.mbt b/bitlogger/sinks.mbt index f0ba5b8..bd70939 100644 --- a/bitlogger/sinks.mbt +++ b/bitlogger/sinks.mbt @@ -49,8 +49,8 @@ pub struct FileSink { append : Ref[Bool] handle : Ref[FileHandle?] formatter : RecordFormatter - auto_flush : Bool - rotation : FileRotation? + auto_flush : Ref[Bool] + rotation : Ref[FileRotation?] open_failures : Ref[Int] write_failures : Ref[Int] flush_failures : Ref[Int] @@ -88,8 +88,8 @@ pub fn file_sink( append: Ref::new(append), handle: Ref::new(handle), formatter, - auto_flush, - rotation, + auto_flush: Ref::new(auto_flush), + rotation: Ref::new(rotation), open_failures: Ref::new(if handle is Some(_) { 0 } else { 1 }), write_failures: Ref::new(0), flush_failures: Ref::new(0), @@ -123,15 +123,27 @@ pub fn FileSink::path(self : FileSink) -> String { } pub fn FileSink::auto_flush_enabled(self : FileSink) -> Bool { - self.auto_flush + self.auto_flush.val } pub fn FileSink::rotation_enabled(self : FileSink) -> Bool { - self.rotation is Some(_) + self.rotation.val is Some(_) } pub fn FileSink::rotation_config(self : FileSink) -> FileRotation? { - self.rotation + self.rotation.val +} + +pub fn FileSink::set_auto_flush(self : FileSink, enabled : Bool) -> Unit { + self.auto_flush.val = enabled +} + +pub fn FileSink::set_rotation(self : FileSink, rotation : FileRotation?) -> Unit { + self.rotation.val = rotation +} + +pub fn FileSink::clear_rotation(self : FileSink) -> Unit { + self.rotation.val = None } pub fn FileSink::close(self : FileSink) -> Bool { @@ -214,7 +226,7 @@ fn rotate_file_sink_internal(sink : FileSink, rotation : FileRotation) -> Bool { } fn rotate_if_needed_internal(sink : FileSink, next_line_bytes : Int) -> Bool { - match sink.rotation { + match sink.rotation.val { None => true Some(rotation) => match sink.handle.val { None => false @@ -250,7 +262,7 @@ pub impl Sink for FileSink with write(self, rec) { Some(active) => { let wrote = write_file_handle_internal(active, line) if wrote { - if self.auto_flush { + if self.auto_flush.val { let flushed = flush_file_handle_internal(active) if !flushed { self.flush_failures.val += 1 diff --git a/docs/README-en.md b/docs/README-en.md index 61cdbbb..d154c0c 100644 --- a/docs/README-en.md +++ b/docs/README-en.md @@ -210,7 +210,8 @@ if native_files_supported() { - `file_sink(...)` also exposes `append_mode()`. Passing `append=...` to `reopen(...)` updates the current append policy used by later reopen calls. - `file_sink(...)` also exposes `path()` and `auto_flush_enabled()` for reading basic file-sink policy state. - `file_sink(...)` also exposes `rotation_enabled()` and `rotation_config()` for reading whether rotation is active and which parameters are currently in effect. -- `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()`, `file_flush()`, `file_close()`, `file_append_mode()`, `file_path()`, `file_auto_flush()`, `file_rotation_enabled()`, `file_rotation_config()`, and the corresponding file failure counters, so config-driven file logging keeps a usable control surface. +- `file_sink(...)` also supports `set_auto_flush(...)`, `set_rotation(...)`, and `clear_rotation()` for runtime policy updates. +- `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()`, `file_flush()`, `file_close()`, `file_append_mode()`, `file_path()`, `file_auto_flush()`, `file_rotation_enabled()`, `file_rotation_config()`, plus `file_set_auto_flush(...)`, `file_set_rotation(...)`, `file_clear_rotation()`, 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 be3819a..81f9c1b 100644 --- a/docs/changes/0.3.0.md +++ b/docs/changes/0.3.0.md @@ -35,6 +35,7 @@ version 0.3.0 - feat: make file append mode queryable via `append_mode()` / `file_append_mode()` and persist explicit `reopen(append=...)` mode updates for later reopen calls - feat: add `path()` / `auto_flush_enabled()` on `FileSink` and `file_path()` / `file_auto_flush()` on `ConfiguredLogger` for basic file-policy introspection - feat: add `rotation_enabled()` / `rotation_config()` on `FileSink` and `file_rotation_enabled()` / `file_rotation_config()` on `ConfiguredLogger` for rotation-policy introspection +- feat: add runtime file-policy mutators `set_auto_flush(...)`, `set_rotation(...)`, `clear_rotation()` and corresponding configured-logger forwarding helpers - 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 @@ -54,6 +55,7 @@ version 0.3.0 - test: cover append-mode observability and reopen-mode persistence 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 runtime auto-flush and rotation policy mutation for direct and config-built file sinks - 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 @@ -74,6 +76,7 @@ version 0.3.0 - docs: clarify append-mode observability and reopen append-policy semantics - 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: 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