mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add configurable style markup modes
This commit is contained in:
@@ -74,8 +74,9 @@ 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}}}}}",
|
||||
"{\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"color_mode\":\"always\",\"style_markup\":\"builtin\",\"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(config.sink.text_formatter.style_tags.length(), content="2")
|
||||
match config.sink.text_formatter.style_tags.get("accent") {
|
||||
Some(style) => {
|
||||
@@ -148,6 +149,7 @@ test "logger config stringify roundtrips formatter style tags" {
|
||||
kind=SinkKind::TextConsole,
|
||||
text_formatter=TextFormatterConfig::new(
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Builtin,
|
||||
style_tags={
|
||||
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
|
||||
"panel": text_style(bg=Some("#202020"), dim=true),
|
||||
@@ -158,6 +160,7 @@ 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(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")
|
||||
@@ -204,12 +207,13 @@ test "config subtype json helpers stringify stable shapes" {
|
||||
field_separator=",",
|
||||
template="[{level}] {message} :: {fields}",
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Builtin,
|
||||
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_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\",\"style_tags\":{\"accent\":{\"bold\":true,\"dim\":false,\"italic\":false,\"underline\":false,\"fg\":\"#4cc9f0\"}}}",
|
||||
)
|
||||
inspect(
|
||||
stringify_sink_config(
|
||||
@@ -222,7 +226,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\"},\"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\"},\"rotation\":{\"max_bytes\":128,\"max_backups\":2}}",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -242,6 +246,40 @@ test "config formatter style tags render in built logger" {
|
||||
inspect(rendered, content="[\u{001b}[32mINFO\u{001b}[0m] \u{001b}[38;2;76;201;240;1mtag\u{001b}[0m")
|
||||
}
|
||||
|
||||
test "config builtin style markup ignores custom tags" {
|
||||
let formatter = TextFormatterConfig::new(
|
||||
show_level=false,
|
||||
show_target=false,
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Builtin,
|
||||
style_tags={
|
||||
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
|
||||
},
|
||||
)
|
||||
let rendered = format_text(
|
||||
Record::new(Level::Info, "<accent>custom</> <red>builtin</>"),
|
||||
formatter=formatter.to_formatter(),
|
||||
)
|
||||
inspect(rendered, content="<accent>custom</> \u{001b}[31mbuiltin\u{001b}[0m")
|
||||
}
|
||||
|
||||
test "config disabled style markup keeps raw tags" {
|
||||
let formatter = TextFormatterConfig::new(
|
||||
show_level=false,
|
||||
show_target=false,
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Disabled,
|
||||
style_tags={
|
||||
"accent": text_style(fg=Some("#4cc9f0"), bold=true),
|
||||
},
|
||||
)
|
||||
let rendered = format_text(
|
||||
Record::new(Level::Info, "<accent>raw</> <red>tag</>"),
|
||||
formatter=formatter.to_formatter(),
|
||||
)
|
||||
inspect(rendered, content="<accent>raw</> <red>tag</>")
|
||||
}
|
||||
|
||||
test "build logger from config supports queued text console" {
|
||||
let logger = build_logger(
|
||||
LoggerConfig::new(
|
||||
|
||||
@@ -123,6 +123,39 @@ test "text formatter keeps unknown inline tags as plain text" {
|
||||
)
|
||||
}
|
||||
|
||||
test "text formatter can disable style markup parsing" {
|
||||
let rec = record(Level::Info, "<red>boom</> <b>bold</>", target="svc")
|
||||
inspect(
|
||||
format_text(
|
||||
rec,
|
||||
formatter=text_formatter(color_mode=ColorMode::Always).without_style_markup(),
|
||||
),
|
||||
content="[\u{001b}[32mINFO\u{001b}[0m] [\u{001b}[34msvc\u{001b}[0m] <red>boom</> <b>bold</>",
|
||||
)
|
||||
}
|
||||
|
||||
test "text formatter builtin style markup mode ignores custom tags" {
|
||||
let formatter = text_formatter(
|
||||
show_level=false,
|
||||
show_target=false,
|
||||
color_mode=ColorMode::Always,
|
||||
style_markup=StyleMarkupMode::Builtin,
|
||||
).with_style_tags(
|
||||
style_tag_registry().set_tag("accent", fg=Some("#4cc9f0"), bold=true),
|
||||
)
|
||||
let rec = record(Level::Info, "<accent>custom</> <red>builtin</>")
|
||||
inspect(
|
||||
format_text(rec, formatter=formatter),
|
||||
content="<accent>custom</> \u{001b}[31mbuiltin\u{001b}[0m",
|
||||
)
|
||||
}
|
||||
|
||||
test "style markup mode label is stable" {
|
||||
inspect(style_markup_mode_label(StyleMarkupMode::Disabled), content="disabled")
|
||||
inspect(style_markup_mode_label(StyleMarkupMode::Builtin), content="builtin")
|
||||
inspect(style_markup_mode_label(StyleMarkupMode::Full), content="full")
|
||||
}
|
||||
|
||||
test "text formatter supports custom style tags" {
|
||||
let formatter = text_formatter(
|
||||
show_level=false,
|
||||
|
||||
@@ -206,6 +206,16 @@ test {
|
||||
}
|
||||
```
|
||||
|
||||
```mbt check
|
||||
test {
|
||||
let formatter = text_formatter(
|
||||
color_mode=ColorMode::Always,
|
||||
).without_style_markup()
|
||||
let logger = Logger::new(text_console_sink(formatter), target="raw")
|
||||
logger.info("<red>kept as raw text</>")
|
||||
}
|
||||
```
|
||||
|
||||
```mbt check
|
||||
test {
|
||||
let config = parse_logger_config_text(
|
||||
@@ -227,10 +237,21 @@ test {
|
||||
}
|
||||
```
|
||||
|
||||
```mbt check
|
||||
test {
|
||||
let config = parse_logger_config_text(
|
||||
"{\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"color_mode\":\"always\",\"style_markup\":\"disabled\"}}}",
|
||||
)
|
||||
let logger = build_logger(config)
|
||||
logger.info("<red>still raw</>")
|
||||
}
|
||||
```
|
||||
|
||||
## Formatter Template / 模板格式
|
||||
|
||||
- supported tokens / 支持的 token: `{timestamp}`, `{timestamp_ms}`, `{level}`, `{target}`, `{message}`, `{fields}`
|
||||
- `color_mode` / `color_mode`: `never`, `auto`, `always`
|
||||
- `style_markup` / `style_markup`: `disabled`, `builtin`, `full`
|
||||
- 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
|
||||
|
||||
@@ -18,6 +18,7 @@ pub struct TextFormatterConfig {
|
||||
field_separator : String
|
||||
template : String
|
||||
color_mode : ColorMode
|
||||
style_markup : StyleMarkupMode
|
||||
style_tags : Map[String, TextStyle]
|
||||
}
|
||||
|
||||
@@ -30,6 +31,7 @@ pub fn TextFormatterConfig::new(
|
||||
field_separator~ : String = " ",
|
||||
template~ : String = "",
|
||||
color_mode~ : ColorMode = ColorMode::Never,
|
||||
style_markup~ : StyleMarkupMode = StyleMarkupMode::Full,
|
||||
style_tags~ : Map[String, TextStyle] = {},
|
||||
) -> TextFormatterConfig {
|
||||
{
|
||||
@@ -41,6 +43,7 @@ pub fn TextFormatterConfig::new(
|
||||
field_separator,
|
||||
template,
|
||||
color_mode,
|
||||
style_markup,
|
||||
style_tags,
|
||||
}
|
||||
}
|
||||
@@ -63,6 +66,7 @@ pub fn TextFormatterConfig::to_formatter(self : TextFormatterConfig) -> TextForm
|
||||
field_separator=self.field_separator,
|
||||
template=self.template,
|
||||
color_mode=self.color_mode,
|
||||
style_markup=self.style_markup,
|
||||
style_tags=if self.style_tags.length() == 0 {
|
||||
None
|
||||
} else {
|
||||
@@ -857,6 +861,15 @@ fn parse_color_mode(name : String) -> ColorMode raise ConfigError {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_style_markup_mode(name : String) -> StyleMarkupMode raise ConfigError {
|
||||
match name.to_upper() {
|
||||
"DISABLED" => StyleMarkupMode::Disabled
|
||||
"BUILTIN" => StyleMarkupMode::Builtin
|
||||
"FULL" => StyleMarkupMode::Full
|
||||
_ => raise ConfigError::InvalidConfig("Unsupported style markup mode: " + name)
|
||||
}
|
||||
}
|
||||
|
||||
fn sink_kind_label(kind : SinkKind) -> String {
|
||||
match kind {
|
||||
SinkKind::Console => "console"
|
||||
@@ -877,6 +890,7 @@ 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_markup=parse_style_markup_mode(get_string(obj, "style_markup", default="full")),
|
||||
style_tags=match obj.get("style_tags") {
|
||||
None => {}
|
||||
Some(inner) => parse_style_tags_config(inner)
|
||||
@@ -1000,6 +1014,7 @@ 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)),
|
||||
"style_markup": @json_parser.JsonValue::String(style_markup_mode_label(config.style_markup)),
|
||||
}
|
||||
if config.style_tags.length() != 0 {
|
||||
obj["style_tags"] = style_tags_config_to_json(config.style_tags)
|
||||
|
||||
@@ -6,6 +6,12 @@ pub(all) enum ColorMode {
|
||||
Always
|
||||
}
|
||||
|
||||
pub(all) enum StyleMarkupMode {
|
||||
Disabled
|
||||
Builtin
|
||||
Full
|
||||
}
|
||||
|
||||
pub struct TextStyle {
|
||||
fg : String?
|
||||
bg : String?
|
||||
@@ -165,6 +171,7 @@ pub struct TextFormatter {
|
||||
field_separator : String
|
||||
template : String
|
||||
color_mode : ColorMode
|
||||
style_markup : StyleMarkupMode
|
||||
style_tags : StyleTagRegistry?
|
||||
}
|
||||
|
||||
@@ -177,6 +184,7 @@ pub fn text_formatter(
|
||||
field_separator~ : String = " ",
|
||||
template~ : String = "",
|
||||
color_mode~ : ColorMode = ColorMode::Never,
|
||||
style_markup~ : StyleMarkupMode = StyleMarkupMode::Full,
|
||||
style_tags~ : StyleTagRegistry? = None,
|
||||
) -> TextFormatter {
|
||||
{
|
||||
@@ -188,10 +196,27 @@ pub fn text_formatter(
|
||||
field_separator,
|
||||
template,
|
||||
color_mode,
|
||||
style_markup,
|
||||
style_tags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn style_markup_mode_label(mode : StyleMarkupMode) -> String {
|
||||
match mode {
|
||||
StyleMarkupMode::Disabled => "disabled"
|
||||
StyleMarkupMode::Builtin => "builtin"
|
||||
StyleMarkupMode::Full => "full"
|
||||
}
|
||||
}
|
||||
|
||||
pub fn TextFormatter::with_style_markup(self : TextFormatter, style_markup : StyleMarkupMode) -> TextFormatter {
|
||||
{ ..self, style_markup }
|
||||
}
|
||||
|
||||
pub fn TextFormatter::without_style_markup(self : TextFormatter) -> TextFormatter {
|
||||
{ ..self, style_markup: StyleMarkupMode::Disabled }
|
||||
}
|
||||
|
||||
pub fn TextFormatter::with_style_tags(self : TextFormatter, style_tags : StyleTagRegistry) -> TextFormatter {
|
||||
{ ..self, style_tags: Some(style_tags) }
|
||||
}
|
||||
@@ -427,6 +452,10 @@ fn builtin_text_style_for_tag(tag : String) -> TextStyle? {
|
||||
|
||||
fn resolve_registered_text_style(tag : String, formatter : TextFormatter) -> TextStyle? {
|
||||
let normalized = normalize_style_tag_name(tag)
|
||||
match formatter.style_markup {
|
||||
StyleMarkupMode::Builtin => return builtin_text_style_for_tag(normalized)
|
||||
_ => ()
|
||||
}
|
||||
match formatter.style_tags {
|
||||
Some(registry) => match registry.get(normalized) {
|
||||
Some(style) => Some(style)
|
||||
@@ -522,6 +551,10 @@ fn parse_inline_markup(input : String, formatter : TextFormatter) -> Array[Style
|
||||
}
|
||||
|
||||
fn render_inline_markup(message : String, formatter : TextFormatter) -> String {
|
||||
match formatter.style_markup {
|
||||
StyleMarkupMode::Disabled => return message
|
||||
_ => ()
|
||||
}
|
||||
let enabled = use_ansi_color(formatter.color_mode)
|
||||
let segments = parse_inline_markup(message, formatter)
|
||||
let out = StringBuilder::new()
|
||||
|
||||
Reference in New Issue
Block a user