Add runtime file policy setters

This commit is contained in:
Nanaloveyuki
2026-05-10 12:45:50 +08:00
parent 14eaf9e178
commit a71471ffb0
8 changed files with 133 additions and 12 deletions
+26
View File
@@ -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(
+20
View File
@@ -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"))
+2 -1
View File
@@ -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
+57
View File
@@ -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()
}
+21 -9
View File
@@ -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