mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add formatter style_tags config support
This commit is contained in:
@@ -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(
|
||||
|
||||
+12
-1
@@ -217,6 +217,16 @@ test {
|
||||
}
|
||||
```
|
||||
|
||||
```mbt check
|
||||
test {
|
||||
let config = parse_logger_config_text(
|
||||
"{\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"show_timestamp\":false,\"color_mode\":\"always\",\"style_tags\":{\"accent\":{\"fg\":\"#4cc9f0\",\"bold\":true}}}}}",
|
||||
)
|
||||
let logger = build_logger(config)
|
||||
logger.info("<accent>styled from json</>")
|
||||
}
|
||||
```
|
||||
|
||||
## Formatter Template / 模板格式
|
||||
|
||||
- supported tokens / 支持的 token: `{timestamp}`, `{timestamp_ms}`, `{level}`, `{target}`, `{message}`, `{fields}`
|
||||
@@ -224,7 +234,8 @@ test {
|
||||
- inline style tags / inline 样式标签: `<red>...</>`, `<b>...</>`, `<#ff0000>...</>`, `<bg:#202020>...</>`
|
||||
- runtime style tags / 运行期样式标签: `TextStyle`, `StyleTagRegistry`, `style_tag_registry()`, `default_style_tag_registry()`, `set_tag(...)`, `define_alias(...)`
|
||||
- style tag priority / 标签优先级: formatter local `style_tags` > global style tag registry > builtin tags
|
||||
- custom style tags are runtime-only for now / 自定义样式标签当前仅支持运行期 API
|
||||
- `sink.text_formatter.style_tags` / `sink.text_formatter.style_tags` 现支持最小对象映射: `fg`, `bg`, `bold`, `dim`, `italic`, `underline`
|
||||
- `define_alias(...)` is still runtime-only / `define_alias(...)` 目前仍为运行期 API
|
||||
- disabled or missing parts render as empty text / 被关闭或缺失的部分会渲染为空字符串
|
||||
- `template` is intentionally a simple token replacement layer, not a full DSL / `template` 使用轻量 token 替换方式, 不是完整 DSL
|
||||
|
||||
|
||||
+89
-2
@@ -18,6 +18,7 @@ pub struct TextFormatterConfig {
|
||||
field_separator : String
|
||||
template : String
|
||||
color_mode : ColorMode
|
||||
style_tags : Map[String, TextStyle]
|
||||
}
|
||||
|
||||
pub fn TextFormatterConfig::new(
|
||||
@@ -29,6 +30,7 @@ pub fn TextFormatterConfig::new(
|
||||
field_separator~ : String = " ",
|
||||
template~ : String = "",
|
||||
color_mode~ : ColorMode = ColorMode::Never,
|
||||
style_tags~ : Map[String, TextStyle] = {},
|
||||
) -> TextFormatterConfig {
|
||||
{
|
||||
show_timestamp,
|
||||
@@ -39,9 +41,18 @@ pub fn TextFormatterConfig::new(
|
||||
field_separator,
|
||||
template,
|
||||
color_mode,
|
||||
style_tags,
|
||||
}
|
||||
}
|
||||
|
||||
fn style_tag_registry_from_config(style_tags : Map[String, TextStyle]) -> StyleTagRegistry {
|
||||
let registry = style_tag_registry()
|
||||
for name, style in style_tags {
|
||||
ignore(registry.set_tag(name, style=style))
|
||||
}
|
||||
registry
|
||||
}
|
||||
|
||||
pub fn TextFormatterConfig::to_formatter(self : TextFormatterConfig) -> TextFormatter {
|
||||
text_formatter(
|
||||
show_timestamp=self.show_timestamp,
|
||||
@@ -52,6 +63,11 @@ pub fn TextFormatterConfig::to_formatter(self : TextFormatterConfig) -> TextForm
|
||||
field_separator=self.field_separator,
|
||||
template=self.template,
|
||||
color_mode=self.color_mode,
|
||||
style_tags=if self.style_tags.length() == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(style_tag_registry_from_config(self.style_tags))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -759,6 +775,19 @@ fn get_string(
|
||||
}
|
||||
}
|
||||
|
||||
fn get_optional_string(
|
||||
obj : Map[String, @json_parser.JsonValue],
|
||||
key : String,
|
||||
) -> String? raise ConfigError {
|
||||
match obj.get(key) {
|
||||
None => None
|
||||
Some(value) => match value.as_string() {
|
||||
Some(text) => Some(text)
|
||||
None => raise ConfigError::InvalidConfig("Expected string at key " + key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_bool(
|
||||
obj : Map[String, @json_parser.JsonValue],
|
||||
key : String,
|
||||
@@ -848,9 +877,37 @@ fn parse_text_formatter_config(value : @json_parser.JsonValue) -> TextFormatterC
|
||||
field_separator=get_string(obj, "field_separator", default=" "),
|
||||
template=get_string(obj, "template", default=""),
|
||||
color_mode=parse_color_mode(get_string(obj, "color_mode", default="never")),
|
||||
style_tags=match obj.get("style_tags") {
|
||||
None => {}
|
||||
Some(inner) => parse_style_tags_config(inner)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn parse_text_style_config(
|
||||
value : @json_parser.JsonValue,
|
||||
context : String,
|
||||
) -> TextStyle raise ConfigError {
|
||||
let obj = expect_object(value, context)
|
||||
text_style(
|
||||
fg=get_optional_string(obj, "fg"),
|
||||
bg=get_optional_string(obj, "bg"),
|
||||
bold=get_bool(obj, "bold", default=false),
|
||||
dim=get_bool(obj, "dim", default=false),
|
||||
italic=get_bool(obj, "italic", default=false),
|
||||
underline=get_bool(obj, "underline", default=false),
|
||||
)
|
||||
}
|
||||
|
||||
fn parse_style_tags_config(value : @json_parser.JsonValue) -> Map[String, TextStyle] raise ConfigError {
|
||||
let obj = expect_object(value, "text_formatter.style_tags")
|
||||
let style_tags : Map[String, TextStyle] = {}
|
||||
for name, item in obj {
|
||||
style_tags[name] = parse_text_style_config(item, "text_formatter.style_tags." + name)
|
||||
}
|
||||
style_tags
|
||||
}
|
||||
|
||||
fn parse_queue_config(value : @json_parser.JsonValue) -> QueueConfig raise ConfigError {
|
||||
let obj = expect_object(value, "queue")
|
||||
QueueConfig::new(
|
||||
@@ -934,7 +991,7 @@ pub fn stringify_queue_config(queue : QueueConfig, pretty~ : Bool = false) -> St
|
||||
}
|
||||
|
||||
pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_parser.JsonValue {
|
||||
@json_parser.JsonValue::Object({
|
||||
let obj : Map[String, @json_parser.JsonValue] = {
|
||||
"show_timestamp": @json_parser.JsonValue::Bool(config.show_timestamp),
|
||||
"show_level": @json_parser.JsonValue::Bool(config.show_level),
|
||||
"show_target": @json_parser.JsonValue::Bool(config.show_target),
|
||||
@@ -943,7 +1000,37 @@ pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_pars
|
||||
"field_separator": @json_parser.JsonValue::String(config.field_separator),
|
||||
"template": @json_parser.JsonValue::String(config.template),
|
||||
"color_mode": @json_parser.JsonValue::String(color_mode_label(config.color_mode)),
|
||||
})
|
||||
}
|
||||
if config.style_tags.length() != 0 {
|
||||
obj["style_tags"] = style_tags_config_to_json(config.style_tags)
|
||||
}
|
||||
@json_parser.JsonValue::Object(obj)
|
||||
}
|
||||
|
||||
fn text_style_config_to_json(style : TextStyle) -> @json_parser.JsonValue {
|
||||
let obj : Map[String, @json_parser.JsonValue] = {
|
||||
"bold": @json_parser.JsonValue::Bool(style.bold),
|
||||
"dim": @json_parser.JsonValue::Bool(style.dim),
|
||||
"italic": @json_parser.JsonValue::Bool(style.italic),
|
||||
"underline": @json_parser.JsonValue::Bool(style.underline),
|
||||
}
|
||||
match style.fg {
|
||||
Some(value) => obj["fg"] = @json_parser.JsonValue::String(value)
|
||||
None => ()
|
||||
}
|
||||
match style.bg {
|
||||
Some(value) => obj["bg"] = @json_parser.JsonValue::String(value)
|
||||
None => ()
|
||||
}
|
||||
@json_parser.JsonValue::Object(obj)
|
||||
}
|
||||
|
||||
fn style_tags_config_to_json(style_tags : Map[String, TextStyle]) -> @json_parser.JsonValue {
|
||||
let obj : Map[String, @json_parser.JsonValue] = {}
|
||||
for name, style in style_tags {
|
||||
obj[name] = text_style_config_to_json(style)
|
||||
}
|
||||
@json_parser.JsonValue::Object(obj)
|
||||
}
|
||||
|
||||
pub fn stringify_text_formatter_config(
|
||||
|
||||
Reference in New Issue
Block a user