Add logger config presets

This commit is contained in:
Nanaloveyuki
2026-05-20 10:06:51 +08:00
parent 3c15d8ed13
commit 4c25a81b03
2 changed files with 73 additions and 9 deletions
+4 -1
View File
@@ -47,7 +47,10 @@ pub fn file(
auto_flush~ : Bool = true, auto_flush~ : Bool = true,
rotation~ : FileRotation? = None, rotation~ : FileRotation? = None,
text_formatter~ : TextFormatterConfig = default_text_formatter_config(), text_formatter~ : TextFormatterConfig = default_text_formatter_config(),
) -> LoggerConfig { ) -> LoggerConfig raise ConfigError {
if path == "" {
raise ConfigError::InvalidConfig("File sink requires non-empty path")
}
LoggerConfig::new( LoggerConfig::new(
min_level=min_level, min_level=min_level,
target=target, target=target,
+69 -8
View File
@@ -41,14 +41,22 @@ test "file preset uses file sink defaults" {
inspect(config.queue is None, content="true") inspect(config.queue is None, content="true")
} }
test "file preset preserves empty path as configured" { test "file preset rejects empty path like parser file sink config" {
let config = file("") let preset_error = (fn() -> String raise ConfigError {
inspect(match config.sink.kind { ignore(file(""))
SinkKind::File => "File" "no error"
_ => "other" })() catch {
}, content="File") ConfigError::InvalidConfig(message) => message
inspect(config.sink.path, content="") }
inspect(config.sink.rotation is None, content="true") inspect(preset_error, content="File sink requires non-empty path")
let parser_error = (fn() -> String raise ConfigError {
ignore(parse_logger_config_text("{\"sink\":{\"kind\":\"file\",\"path\":\"\"}}"))
"no error"
})() catch {
ConfigError::InvalidConfig(message) => message
}
inspect(parser_error, content="File sink requires non-empty path")
} }
test "preset helpers compose queue and file rotation without losing config" { test "preset helpers compose queue and file rotation without losing config" {
@@ -94,6 +102,33 @@ test "preset helpers compose queue and file rotation without losing config" {
} }
} }
test "queue helper preserves file rotation when applied after rotation" {
let config = with_queue(
with_file_rotation(file("service.log", append=false), 256, max_backups=2),
max_pending=8,
overflow=QueueOverflowPolicy::DropNewest,
)
inspect(config.sink.path, content="service.log")
inspect(config.sink.append, content="false")
match config.queue {
Some(queue) => {
inspect(queue.max_pending, content="8")
inspect(match queue.overflow {
QueueOverflowPolicy::DropNewest => "DropNewest"
QueueOverflowPolicy::DropOldest => "DropOldest"
}, content="DropNewest")
}
None => inspect(false, content="true")
}
match config.sink.rotation {
Some(rotation) => {
inspect(rotation.max_bytes, content="256")
inspect(rotation.max_backups, content="2")
}
None => inspect(false, content="true")
}
}
test "file rotation helper leaves non-file presets unchanged" { test "file rotation helper leaves non-file presets unchanged" {
let console_config = with_file_rotation(console(target="console"), 64, max_backups=2) let console_config = with_file_rotation(console(target="console"), 64, max_backups=2)
inspect(match console_config.sink.kind { inspect(match console_config.sink.kind {
@@ -116,3 +151,29 @@ test "file rotation helper leaves non-file presets unchanged" {
inspect(text_config.sink.text_formatter.separator, content=" :: ") inspect(text_config.sink.text_formatter.separator, content=" :: ")
inspect(text_config.sink.rotation is None, content="true") inspect(text_config.sink.rotation is None, content="true")
} }
test "file rotation helper keeps queued non-file presets unchanged" {
let config = with_queue(
json_console(min_level=Level::Error, target="json"),
max_pending=4,
overflow=QueueOverflowPolicy::DropOldest,
)
let rotated = with_file_rotation(config, 512, max_backups=5)
inspect(rotated.min_level.label(), content="ERROR")
inspect(rotated.target, content="json")
inspect(match rotated.sink.kind {
SinkKind::JsonConsole => "JsonConsole"
_ => "other"
}, content="JsonConsole")
inspect(rotated.sink.rotation is None, content="true")
match rotated.queue {
Some(queue) => {
inspect(queue.max_pending, content="4")
inspect(match queue.overflow {
QueueOverflowPolicy::DropNewest => "DropNewest"
QueueOverflowPolicy::DropOldest => "DropOldest"
}, content="DropOldest")
}
None => inspect(false, content="true")
}
}