mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add scoped target and field style markup
This commit is contained in:
@@ -74,9 +74,11 @@ 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_markup\":\"builtin\",\"style_tags\":{\"accent\":{\"fg\":\"#4cc9f0\",\"bold\":true},\"panel\":{\"bg\":\"#202020\",\"underline\":true}}}}}",
|
||||
"{\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"color_mode\":\"always\",\"style_markup\":\"builtin\",\"target_style_markup\":\"builtin\",\"fields_style_markup\":\"disabled\",\"style_tags\":{\"accent\":{\"fg\":\"#4cc9f0\",\"bold\":true},\"panel\":{\"bg\":\"#202020\",\"underline\":true}}}}}",
|
||||
)
|
||||
inspect(style_markup_mode_label(config.sink.text_formatter.style_markup), content="builtin")
|
||||
inspect(style_markup_mode_label(config.sink.text_formatter.target_style_markup), content="builtin")
|
||||
inspect(style_markup_mode_label(config.sink.text_formatter.fields_style_markup), content="disabled")
|
||||
inspect(config.sink.text_formatter.style_tags.length(), content="2")
|
||||
match config.sink.text_formatter.style_tags.get("accent") {
|
||||
Some(style) => {
|
||||
@@ -150,6 +152,8 @@ test "logger config stringify roundtrips formatter style tags" {
|
||||
text_formatter=TextFormatterConfig::new(
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Builtin,
|
||||
target_style_markup=StyleMarkupMode::Builtin,
|
||||
fields_style_markup=StyleMarkupMode::Disabled,
|
||||
style_tags={
|
||||
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
|
||||
"panel": text_style(bg=Some("#202020"), dim=true),
|
||||
@@ -161,6 +165,8 @@ test "logger config stringify roundtrips formatter style tags" {
|
||||
let config = parse_logger_config_text(text)
|
||||
inspect(color_mode_label(config.sink.text_formatter.color_mode), content="always")
|
||||
inspect(style_markup_mode_label(config.sink.text_formatter.style_markup), content="builtin")
|
||||
inspect(style_markup_mode_label(config.sink.text_formatter.target_style_markup), content="builtin")
|
||||
inspect(style_markup_mode_label(config.sink.text_formatter.fields_style_markup), content="disabled")
|
||||
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")
|
||||
@@ -208,12 +214,14 @@ test "config subtype json helpers stringify stable shapes" {
|
||||
template="[{level}] {message} :: {fields}",
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Builtin,
|
||||
target_style_markup=StyleMarkupMode::Builtin,
|
||||
fields_style_markup=StyleMarkupMode::Disabled,
|
||||
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\",\"style_markup\":\"builtin\",\"style_tags\":{\"accent\":{\"bold\":true,\"dim\":false,\"italic\":false,\"underline\":false,\"fg\":\"#4cc9f0\"}}}",
|
||||
content="{\"show_timestamp\":false,\"show_level\":true,\"show_target\":false,\"show_fields\":true,\"separator\":\" | \",\"field_separator\":\",\",\"template\":\"[{level}] {message} :: {fields}\",\"color_mode\":\"always\",\"style_markup\":\"builtin\",\"target_style_markup\":\"builtin\",\"fields_style_markup\":\"disabled\",\"style_tags\":{\"accent\":{\"bold\":true,\"dim\":false,\"italic\":false,\"underline\":false,\"fg\":\"#4cc9f0\"}}}",
|
||||
)
|
||||
inspect(
|
||||
stringify_sink_config(
|
||||
@@ -226,7 +234,7 @@ test "config subtype json helpers stringify stable shapes" {
|
||||
text_formatter=TextFormatterConfig::new(show_timestamp=false, color_mode=ColorMode::Auto),
|
||||
),
|
||||
),
|
||||
content="{\"kind\":\"file\",\"path\":\"demo.log\",\"append\":false,\"auto_flush\":false,\"text_formatter\":{\"show_timestamp\":false,\"show_level\":true,\"show_target\":true,\"show_fields\":true,\"separator\":\" \",\"field_separator\":\" \",\"template\":\"\",\"color_mode\":\"auto\",\"style_markup\":\"full\"},\"rotation\":{\"max_bytes\":128,\"max_backups\":2}}",
|
||||
content="{\"kind\":\"file\",\"path\":\"demo.log\",\"append\":false,\"auto_flush\":false,\"text_formatter\":{\"show_timestamp\":false,\"show_level\":true,\"show_target\":true,\"show_fields\":true,\"separator\":\" \",\"field_separator\":\" \",\"template\":\"\",\"color_mode\":\"auto\",\"style_markup\":\"full\",\"target_style_markup\":\"disabled\",\"fields_style_markup\":\"disabled\"},\"rotation\":{\"max_bytes\":128,\"max_backups\":2}}",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -280,6 +288,29 @@ test "config disabled style markup keeps raw tags" {
|
||||
inspect(rendered, content="<accent>raw</> <red>tag</>")
|
||||
}
|
||||
|
||||
test "config target and fields markup modes render separately" {
|
||||
let formatter = TextFormatterConfig::new(
|
||||
show_level=false,
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Disabled,
|
||||
target_style_markup=StyleMarkupMode::Builtin,
|
||||
fields_style_markup=StyleMarkupMode::Builtin,
|
||||
)
|
||||
let rendered = format_text(
|
||||
Record::new(
|
||||
Level::Info,
|
||||
"<danger>message stays raw</>",
|
||||
target="<danger>svc</>",
|
||||
fields=[field("status", "<success>ok</>")],
|
||||
),
|
||||
formatter=formatter.to_formatter(),
|
||||
)
|
||||
inspect(
|
||||
rendered,
|
||||
content="[\u{001b}[34m\u{001b}[31;1msvc\u{001b}[0m\u{001b}[0m] <danger>message stays raw</> \u{001b}[35mstatus=\u{001b}[32;1mok\u{001b}[0m\u{001b}[0m",
|
||||
)
|
||||
}
|
||||
|
||||
test "build logger from config supports queued text console" {
|
||||
let logger = build_logger(
|
||||
LoggerConfig::new(
|
||||
|
||||
@@ -259,6 +259,47 @@ test "builtin semantic style tags can still be overridden" {
|
||||
)
|
||||
}
|
||||
|
||||
test "text formatter can enable markup for target separately" {
|
||||
let formatter = text_formatter(
|
||||
show_level=false,
|
||||
color_mode=ColorMode::Always,
|
||||
target_style_markup=StyleMarkupMode::Builtin,
|
||||
)
|
||||
let rec = record(Level::Info, "hello", target="<danger>svc</>")
|
||||
inspect(
|
||||
format_text(rec, formatter=formatter),
|
||||
content="[\u{001b}[34m\u{001b}[31;1msvc\u{001b}[0m\u{001b}[0m] hello",
|
||||
)
|
||||
}
|
||||
|
||||
test "text formatter can enable markup for field values separately" {
|
||||
let formatter = text_formatter(
|
||||
show_level=false,
|
||||
show_target=false,
|
||||
color_mode=ColorMode::Always,
|
||||
fields_style_markup=StyleMarkupMode::Builtin,
|
||||
)
|
||||
let rec = record(Level::Info, "hello", fields=[field("status", "<success>ok</>")])
|
||||
inspect(
|
||||
format_text(rec, formatter=formatter),
|
||||
content="hello \u{001b}[35mstatus=\u{001b}[32;1mok\u{001b}[0m\u{001b}[0m",
|
||||
)
|
||||
}
|
||||
|
||||
test "text formatter leaves field keys raw when field markup is enabled" {
|
||||
let formatter = text_formatter(
|
||||
show_level=false,
|
||||
show_target=false,
|
||||
color_mode=ColorMode::Always,
|
||||
fields_style_markup=StyleMarkupMode::Builtin,
|
||||
)
|
||||
let rec = record(Level::Info, "hello", fields=[field("<danger>status</>", "ok")])
|
||||
inspect(
|
||||
format_text(rec, formatter=formatter),
|
||||
content="hello \u{001b}[35m<danger>status</>=ok\u{001b}[0m",
|
||||
)
|
||||
}
|
||||
|
||||
test "text formatter template respects disabled fields" {
|
||||
let rec = record(Level::Warn, "just message", target="svc")
|
||||
let formatter = text_formatter(
|
||||
|
||||
@@ -252,11 +252,13 @@ test {
|
||||
- supported tokens / 支持的 token: `{timestamp}`, `{timestamp_ms}`, `{level}`, `{target}`, `{message}`, `{fields}`
|
||||
- `color_mode` / `color_mode`: `never`, `auto`, `always`
|
||||
- `style_markup` / `style_markup`: `disabled`, `builtin`, `full`
|
||||
- `target_style_markup` / `target_style_markup`, `fields_style_markup` / `fields_style_markup`: `disabled`, `builtin`, `full`
|
||||
- inline style tags / inline 样式标签: `<red>...</>`, `<b>...</>`, `<#ff0000>...</>`, `<bg:#202020>...</>`
|
||||
- builtin semantic tags / 内置语义标签: `<accent>`, `<info>`, `<success>`, `<warning>`, `<danger>`, `<muted>`
|
||||
- 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
|
||||
- `sink.text_formatter.style_tags` / `sink.text_formatter.style_tags` 现支持最小对象映射: `fg`, `bg`, `bold`, `dim`, `italic`, `underline`
|
||||
- `fields_style_markup` currently affects values only / `fields_style_markup` 当前仅影响 field value, 不影响 field key
|
||||
- `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
|
||||
|
||||
@@ -19,6 +19,8 @@ pub struct TextFormatterConfig {
|
||||
template : String
|
||||
color_mode : ColorMode
|
||||
style_markup : StyleMarkupMode
|
||||
target_style_markup : StyleMarkupMode
|
||||
fields_style_markup : StyleMarkupMode
|
||||
style_tags : Map[String, TextStyle]
|
||||
}
|
||||
|
||||
@@ -32,6 +34,8 @@ pub fn TextFormatterConfig::new(
|
||||
template~ : String = "",
|
||||
color_mode~ : ColorMode = ColorMode::Never,
|
||||
style_markup~ : StyleMarkupMode = StyleMarkupMode::Full,
|
||||
target_style_markup~ : StyleMarkupMode = StyleMarkupMode::Disabled,
|
||||
fields_style_markup~ : StyleMarkupMode = StyleMarkupMode::Disabled,
|
||||
style_tags~ : Map[String, TextStyle] = {},
|
||||
) -> TextFormatterConfig {
|
||||
{
|
||||
@@ -44,6 +48,8 @@ pub fn TextFormatterConfig::new(
|
||||
template,
|
||||
color_mode,
|
||||
style_markup,
|
||||
target_style_markup,
|
||||
fields_style_markup,
|
||||
style_tags,
|
||||
}
|
||||
}
|
||||
@@ -67,6 +73,8 @@ pub fn TextFormatterConfig::to_formatter(self : TextFormatterConfig) -> TextForm
|
||||
template=self.template,
|
||||
color_mode=self.color_mode,
|
||||
style_markup=self.style_markup,
|
||||
target_style_markup=self.target_style_markup,
|
||||
fields_style_markup=self.fields_style_markup,
|
||||
style_tags=if self.style_tags.length() == 0 {
|
||||
None
|
||||
} else {
|
||||
@@ -891,6 +899,8 @@ fn parse_text_formatter_config(value : @json_parser.JsonValue) -> TextFormatterC
|
||||
template=get_string(obj, "template", default=""),
|
||||
color_mode=parse_color_mode(get_string(obj, "color_mode", default="never")),
|
||||
style_markup=parse_style_markup_mode(get_string(obj, "style_markup", default="full")),
|
||||
target_style_markup=parse_style_markup_mode(get_string(obj, "target_style_markup", default="disabled")),
|
||||
fields_style_markup=parse_style_markup_mode(get_string(obj, "fields_style_markup", default="disabled")),
|
||||
style_tags=match obj.get("style_tags") {
|
||||
None => {}
|
||||
Some(inner) => parse_style_tags_config(inner)
|
||||
@@ -1015,6 +1025,8 @@ pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_pars
|
||||
"template": @json_parser.JsonValue::String(config.template),
|
||||
"color_mode": @json_parser.JsonValue::String(color_mode_label(config.color_mode)),
|
||||
"style_markup": @json_parser.JsonValue::String(style_markup_mode_label(config.style_markup)),
|
||||
"target_style_markup": @json_parser.JsonValue::String(style_markup_mode_label(config.target_style_markup)),
|
||||
"fields_style_markup": @json_parser.JsonValue::String(style_markup_mode_label(config.fields_style_markup)),
|
||||
}
|
||||
if config.style_tags.length() != 0 {
|
||||
obj["style_tags"] = style_tags_config_to_json(config.style_tags)
|
||||
|
||||
+38
-10
@@ -178,6 +178,8 @@ pub struct TextFormatter {
|
||||
template : String
|
||||
color_mode : ColorMode
|
||||
style_markup : StyleMarkupMode
|
||||
target_style_markup : StyleMarkupMode
|
||||
fields_style_markup : StyleMarkupMode
|
||||
style_tags : StyleTagRegistry?
|
||||
}
|
||||
|
||||
@@ -191,6 +193,8 @@ pub fn text_formatter(
|
||||
template~ : String = "",
|
||||
color_mode~ : ColorMode = ColorMode::Never,
|
||||
style_markup~ : StyleMarkupMode = StyleMarkupMode::Full,
|
||||
target_style_markup~ : StyleMarkupMode = StyleMarkupMode::Disabled,
|
||||
fields_style_markup~ : StyleMarkupMode = StyleMarkupMode::Disabled,
|
||||
style_tags~ : StyleTagRegistry? = None,
|
||||
) -> TextFormatter {
|
||||
{
|
||||
@@ -203,6 +207,8 @@ pub fn text_formatter(
|
||||
template,
|
||||
color_mode,
|
||||
style_markup,
|
||||
target_style_markup,
|
||||
fields_style_markup,
|
||||
style_tags,
|
||||
}
|
||||
}
|
||||
@@ -223,6 +229,20 @@ pub fn TextFormatter::without_style_markup(self : TextFormatter) -> TextFormatte
|
||||
{ ..self, style_markup: StyleMarkupMode::Disabled }
|
||||
}
|
||||
|
||||
pub fn TextFormatter::with_target_style_markup(
|
||||
self : TextFormatter,
|
||||
style_markup : StyleMarkupMode,
|
||||
) -> TextFormatter {
|
||||
{ ..self, target_style_markup: style_markup }
|
||||
}
|
||||
|
||||
pub fn TextFormatter::with_fields_style_markup(
|
||||
self : TextFormatter,
|
||||
style_markup : StyleMarkupMode,
|
||||
) -> TextFormatter {
|
||||
{ ..self, fields_style_markup: style_markup }
|
||||
}
|
||||
|
||||
pub fn TextFormatter::with_style_tags(self : TextFormatter, style_tags : StyleTagRegistry) -> TextFormatter {
|
||||
{ ..self, style_tags: Some(style_tags) }
|
||||
}
|
||||
@@ -562,13 +582,14 @@ fn parse_inline_markup(input : String, formatter : TextFormatter) -> Array[Style
|
||||
segments
|
||||
}
|
||||
|
||||
fn render_inline_markup(message : String, formatter : TextFormatter) -> String {
|
||||
match formatter.style_markup {
|
||||
StyleMarkupMode::Disabled => return message
|
||||
fn render_styled_text(text : String, formatter : TextFormatter, mode : StyleMarkupMode) -> String {
|
||||
match mode {
|
||||
StyleMarkupMode::Disabled => return text
|
||||
_ => ()
|
||||
}
|
||||
let enabled = use_ansi_color(formatter.color_mode)
|
||||
let segments = parse_inline_markup(message, formatter)
|
||||
let scoped = { ..formatter, style_markup: mode }
|
||||
let segments = parse_inline_markup(text, scoped)
|
||||
let out = StringBuilder::new()
|
||||
for segment in segments {
|
||||
out.write_string(ansi_wrap_with_style(segment.text, segment.style, enabled))
|
||||
@@ -576,6 +597,10 @@ fn render_inline_markup(message : String, formatter : TextFormatter) -> String {
|
||||
out.to_string()
|
||||
}
|
||||
|
||||
fn render_inline_markup(message : String, formatter : TextFormatter) -> String {
|
||||
render_styled_text(message, formatter, formatter.style_markup)
|
||||
}
|
||||
|
||||
fn level_ansi_code(level : Level) -> String {
|
||||
match level {
|
||||
Level::Trace => "90"
|
||||
@@ -594,10 +619,6 @@ fn fields_to_json(fields : Array[Field]) -> Json {
|
||||
Json::object(obj)
|
||||
}
|
||||
|
||||
fn format_fields(fields : Array[Field], separator : String) -> String {
|
||||
fields.map(fn(f) { "\{f.key}=\{f.value}" }).join(separator)
|
||||
}
|
||||
|
||||
fn timestamp_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_timestamp && rec.timestamp_ms != 0UL {
|
||||
ansi_wrap(rec.timestamp_ms.to_string(), "90", use_ansi_color(formatter.color_mode))
|
||||
@@ -616,15 +637,22 @@ fn level_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
|
||||
fn target_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_target && rec.target != "" {
|
||||
ansi_wrap(rec.target, "34", use_ansi_color(formatter.color_mode))
|
||||
let rendered = render_styled_text(rec.target, formatter, formatter.target_style_markup)
|
||||
ansi_wrap(rendered, "34", use_ansi_color(formatter.color_mode))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
fn format_field_text(field : Field, formatter : TextFormatter) -> String {
|
||||
let value = render_styled_text(field.value, formatter, formatter.fields_style_markup)
|
||||
"\{field.key}=\{value}"
|
||||
}
|
||||
|
||||
fn fields_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_fields && rec.fields.length() != 0 {
|
||||
ansi_wrap(format_fields(rec.fields, formatter.field_separator), "35", use_ansi_color(formatter.color_mode))
|
||||
let content = rec.fields.map(fn(field) { format_field_text(field, formatter) }).join(formatter.field_separator)
|
||||
ansi_wrap(content, "35", use_ansi_color(formatter.color_mode))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user