mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add config JSON export helpers
This commit is contained in:
@@ -139,6 +139,42 @@ test "logger config stringify roundtrips file rotation fields" {
|
||||
}
|
||||
}
|
||||
|
||||
test "config subtype json helpers stringify stable shapes" {
|
||||
inspect(
|
||||
stringify_queue_config(
|
||||
QueueConfig::new(8, overflow=QueueOverflowPolicy::DropOldest),
|
||||
),
|
||||
content="{\"max_pending\":8,\"overflow\":\"DropOldest\"}",
|
||||
)
|
||||
inspect(
|
||||
stringify_text_formatter_config(
|
||||
TextFormatterConfig::new(
|
||||
show_timestamp=false,
|
||||
show_level=true,
|
||||
show_target=false,
|
||||
show_fields=true,
|
||||
separator=" | ",
|
||||
field_separator=",",
|
||||
template="[{level}] {message} :: {fields}",
|
||||
),
|
||||
),
|
||||
content="{\"field_separator\":\",\",\"separator\":\" | \",\"show_fields\":true,\"show_level\":true,\"show_target\":false,\"show_timestamp\":false,\"template\":\"[{level}] {message} :: {fields}\"}",
|
||||
)
|
||||
inspect(
|
||||
stringify_sink_config(
|
||||
SinkConfig::new(
|
||||
kind=SinkKind::File,
|
||||
path="demo.log",
|
||||
append=false,
|
||||
auto_flush=false,
|
||||
rotation=Some(file_rotation(128, max_backups=2)),
|
||||
text_formatter=TextFormatterConfig::new(show_timestamp=false),
|
||||
),
|
||||
),
|
||||
content="{\"append\":false,\"auto_flush\":false,\"kind\":\"file\",\"path\":\"demo.log\",\"rotation\":{\"max_backups\":2,\"max_bytes\":128},\"text_formatter\":{\"field_separator\":\" \",\"separator\":\" \",\"show_fields\":true,\"show_level\":true,\"show_target\":true,\"show_timestamp\":false,\"template\":\"\"}}",
|
||||
)
|
||||
}
|
||||
|
||||
test "build logger from config supports queued text console" {
|
||||
let logger = build_logger(
|
||||
LoggerConfig::new(
|
||||
|
||||
@@ -44,6 +44,8 @@ BitLogger 是一个基于 MoonBit 的结构化日志库。
|
||||
- 支持 `text_formatter(...)`、`format_text(...)`、`text_console_sink(...)` 以及模板化 `template` 文本输出
|
||||
- JSON config parsing via `parse_logger_config_text(...)` and `stringify_logger_config(...)`
|
||||
- 支持 `parse_logger_config_text(...)`、`stringify_logger_config(...)` 进行最小 JSON 配置读写
|
||||
- `QueueConfig` / `TextFormatterConfig` / `SinkConfig` can also be exported independently through dedicated JSON helpers
|
||||
- `QueueConfig` / `TextFormatterConfig` / `SinkConfig` 也可分别通过专用 JSON helper 单独导出
|
||||
- config-driven logger assembly via `build_logger(...)`
|
||||
- 支持 `build_logger(...)` 将配置组装为可直接使用的 logger
|
||||
- native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()` / `reopen_with_current_policy()` / `reopen_append()` / `reopen_truncate()`, and failure counters
|
||||
|
||||
+33
-3
@@ -900,7 +900,7 @@ pub fn parse_logger_config_text(input : String) -> LoggerConfig raise ConfigErro
|
||||
)
|
||||
}
|
||||
|
||||
fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue {
|
||||
pub fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue {
|
||||
@json_parser.JsonValue::Object({
|
||||
"max_pending": @json_parser.JsonValue::Number(queue.max_pending.to_double()),
|
||||
"overflow": @json_parser.JsonValue::String(match queue.overflow {
|
||||
@@ -910,7 +910,16 @@ fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue {
|
||||
})
|
||||
}
|
||||
|
||||
fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_parser.JsonValue {
|
||||
pub fn stringify_queue_config(queue : QueueConfig, pretty~ : Bool = false) -> String {
|
||||
let value = queue_config_to_json(queue)
|
||||
if pretty {
|
||||
@json_parser.stringify_pretty(value, 2)
|
||||
} else {
|
||||
@json_parser.stringify(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_parser.JsonValue {
|
||||
@json_parser.JsonValue::Object({
|
||||
"show_timestamp": @json_parser.JsonValue::Bool(config.show_timestamp),
|
||||
"show_level": @json_parser.JsonValue::Bool(config.show_level),
|
||||
@@ -922,6 +931,18 @@ fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_parser.J
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stringify_text_formatter_config(
|
||||
config : TextFormatterConfig,
|
||||
pretty~ : Bool = false,
|
||||
) -> String {
|
||||
let value = text_formatter_config_to_json(config)
|
||||
if pretty {
|
||||
@json_parser.stringify_pretty(value, 2)
|
||||
} else {
|
||||
@json_parser.stringify(value)
|
||||
}
|
||||
}
|
||||
|
||||
fn file_rotation_config_to_json(config : FileRotation) -> @json_parser.JsonValue {
|
||||
@json_parser.JsonValue::Object({
|
||||
"max_bytes": @json_parser.JsonValue::Number(config.max_bytes.to_double()),
|
||||
@@ -929,7 +950,7 @@ fn file_rotation_config_to_json(config : FileRotation) -> @json_parser.JsonValue
|
||||
})
|
||||
}
|
||||
|
||||
fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue {
|
||||
pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue {
|
||||
let obj : Map[String, @json_parser.JsonValue] = {
|
||||
"kind": @json_parser.JsonValue::String(sink_kind_label(config.kind)),
|
||||
"path": @json_parser.JsonValue::String(config.path),
|
||||
@@ -944,6 +965,15 @@ fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue {
|
||||
@json_parser.JsonValue::Object(obj)
|
||||
}
|
||||
|
||||
pub fn stringify_sink_config(config : SinkConfig, pretty~ : Bool = false) -> String {
|
||||
let value = sink_config_to_json(config)
|
||||
if pretty {
|
||||
@json_parser.stringify_pretty(value, 2)
|
||||
} else {
|
||||
@json_parser.stringify(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn logger_config_to_json(config : LoggerConfig) -> @json_parser.JsonValue {
|
||||
let obj : Map[String, @json_parser.JsonValue] = {
|
||||
"min_level": @json_parser.JsonValue::String(config.min_level.label()),
|
||||
|
||||
Reference in New Issue
Block a user