Add formatter style_tags config support

This commit is contained in:
Nanaloveyuki
2026-05-10 15:00:37 +08:00
parent f1b223f203
commit f3e903b578
6 changed files with 203 additions and 6 deletions
+66 -1
View File
@@ -72,6 +72,28 @@ test "logger config parser reads formatter and queue options" {
}
}
test "logger config parser reads formatter style tags" {
let config = parse_logger_config_text(
"{\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"color_mode\":\"always\",\"style_tags\":{\"accent\":{\"fg\":\"#4cc9f0\",\"bold\":true},\"panel\":{\"bg\":\"#202020\",\"underline\":true}}}}}",
)
inspect(config.sink.text_formatter.style_tags.length(), content="2")
match config.sink.text_formatter.style_tags.get("accent") {
Some(style) => {
inspect(style.fg.unwrap(), content="#4cc9f0")
inspect(style.bold, content="true")
inspect(style.bg is None, content="true")
}
None => inspect(false, content="true")
}
match config.sink.text_formatter.style_tags.get("panel") {
Some(style) => {
inspect(style.bg.unwrap(), content="#202020")
inspect(style.underline, content="true")
}
None => inspect(false, content="true")
}
}
test "logger config parser reads file rotation options" {
let config = parse_logger_config_text(
"{\"sink\":{\"kind\":\"file\",\"path\":\"bitlogger.log\",\"rotation\":{\"max_bytes\":128,\"max_backups\":3}}}",
@@ -119,6 +141,30 @@ test "logger config stringify roundtrips stable fields" {
inspect(config.sink.text_formatter.template, content="[{level}] {target} {message}")
}
test "logger config stringify roundtrips formatter style tags" {
let text = stringify_logger_config(
LoggerConfig::new(
sink=SinkConfig::new(
kind=SinkKind::TextConsole,
text_formatter=TextFormatterConfig::new(
color_mode=ColorMode::Always,
style_tags={
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
"panel": text_style(bg=Some("#202020"), dim=true),
},
),
),
),
)
let config = parse_logger_config_text(text)
inspect(color_mode_label(config.sink.text_formatter.color_mode), content="always")
inspect(config.sink.text_formatter.style_tags.length(), content="2")
inspect(config.sink.text_formatter.style_tags.get("accent").unwrap().fg.unwrap(), content="#4cc9f0")
inspect(config.sink.text_formatter.style_tags.get("accent").unwrap().bold, content="true")
inspect(config.sink.text_formatter.style_tags.get("panel").unwrap().bg.unwrap(), content="#202020")
inspect(config.sink.text_formatter.style_tags.get("panel").unwrap().dim, content="true")
}
test "logger config stringify roundtrips file rotation fields" {
let text = stringify_logger_config(
LoggerConfig::new(
@@ -158,9 +204,12 @@ test "config subtype json helpers stringify stable shapes" {
field_separator=",",
template="[{level}] {message} :: {fields}",
color_mode=ColorMode::Always,
style_tags={
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
},
),
),
content="{\"show_timestamp\":false,\"show_level\":true,\"show_target\":false,\"show_fields\":true,\"separator\":\" | \",\"field_separator\":\",\",\"template\":\"[{level}] {message} :: {fields}\",\"color_mode\":\"always\"}",
content="{\"show_timestamp\":false,\"show_level\":true,\"show_target\":false,\"show_fields\":true,\"separator\":\" | \",\"field_separator\":\",\",\"template\":\"[{level}] {message} :: {fields}\",\"color_mode\":\"always\",\"style_tags\":{\"accent\":{\"bold\":true,\"dim\":false,\"italic\":false,\"underline\":false,\"fg\":\"#4cc9f0\"}}}",
)
inspect(
stringify_sink_config(
@@ -177,6 +226,22 @@ test "config subtype json helpers stringify stable shapes" {
)
}
test "config formatter style tags render in built logger" {
let formatter = TextFormatterConfig::new(
show_timestamp=false,
show_target=false,
color_mode=ColorMode::Always,
style_tags={
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
},
)
let rendered = format_text(
Record::new(Level::Info, "<accent>tag</>"),
formatter=formatter.to_formatter(),
)
inspect(rendered, content="[\u{001b}[32mINFO\u{001b}[0m] \u{001b}[38;2;76;201;240;1mtag\u{001b}[0m")
}
test "build logger from config supports queued text console" {
let logger = build_logger(
LoggerConfig::new(