diff --git a/README.md b/README.md index 9105f00..6090fee 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,7 @@ match logger.file_runtime_state() { ## 📝 配置说明 - 当前提供 JSON 配置层:`parse_logger_config_text(...)`、`stringify_logger_config(...)`、`build_logger(...)` +- `QueueConfig`、`TextFormatterConfig`、`SinkConfig` 也可分别通过 `queue_config_to_json(...)` / `stringify_queue_config(...)`、`text_formatter_config_to_json(...)` / `stringify_text_formatter_config(...)`、`sink_config_to_json(...)` / `stringify_sink_config(...)` 单独导出 JSON - 已支持字段:`min_level`、`target`、`timestamp`、`sink.kind`、`sink.path`、`sink.append`、`sink.auto_flush`、`sink.rotation`、`sink.text_formatter`、`queue` - `sink.rotation` 当前支持 `max_bytes` 与 `max_backups`,提供基础 size-based rotation 和 backup retention - `file_sink(...)` 还提供 `reopen()`、`reopen_with_current_policy()`、`reopen_append()`、`reopen_truncate()`、`open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()`,用于基础可观测性 diff --git a/bitlogger/BitLogger_test.mbt b/bitlogger/BitLogger_test.mbt index 149c3af..6304c0c 100644 --- a/bitlogger/BitLogger_test.mbt +++ b/bitlogger/BitLogger_test.mbt @@ -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( diff --git a/bitlogger/README.mbt.md b/bitlogger/README.mbt.md index ca8bdc5..fc84cb5 100644 --- a/bitlogger/README.mbt.md +++ b/bitlogger/README.mbt.md @@ -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 diff --git a/bitlogger/config.mbt b/bitlogger/config.mbt index 6403920..2b79766 100644 --- a/bitlogger/config.mbt +++ b/bitlogger/config.mbt @@ -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()), diff --git a/docs/README-en.md b/docs/README-en.md index c768bb1..f4b157e 100644 --- a/docs/README-en.md +++ b/docs/README-en.md @@ -222,6 +222,7 @@ match logger.file_runtime_state() { ## Config Notes - BitLogger now includes a JSON config layer via `parse_logger_config_text(...)`, `stringify_logger_config(...)`, and `build_logger(...)`. +- `QueueConfig`, `TextFormatterConfig`, and `SinkConfig` can also be exported independently through `queue_config_to_json(...)` / `stringify_queue_config(...)`, `text_formatter_config_to_json(...)` / `stringify_text_formatter_config(...)`, and `sink_config_to_json(...)` / `stringify_sink_config(...)`. - Supported keys include `min_level`, `target`, `timestamp`, `sink.kind`, `sink.path`, `sink.append`, `sink.auto_flush`, `sink.rotation`, `sink.text_formatter`, and `queue`. - `sink.rotation` currently supports `max_bytes` and `max_backups` for basic size-based rotation and backup retention. - `file_sink(...)` also exposes `reopen()`, `reopen_with_current_policy()`, `reopen_append()`, `reopen_truncate()`, `open_failures()`, `write_failures()`, `flush_failures()`, and `rotation_failures()` for basic observability. diff --git a/docs/changes/0.3.0.md b/docs/changes/0.3.0.md index e51a4ab..053ad03 100644 --- a/docs/changes/0.3.0.md +++ b/docs/changes/0.3.0.md @@ -11,6 +11,7 @@ version 0.3.0 - feat: add `TextFormatterConfig::to_formatter()` for bridging parsed config into runtime formatter setup - feat: add `parse_logger_config_text(...)` for JSON config parsing - feat: add `logger_config_to_json(...)` and `stringify_logger_config(...)` for config serialization +- feat: add standalone JSON export helpers for `QueueConfig`, `TextFormatterConfig`, and `SinkConfig` - feat: add `RuntimeSink`, `ConfiguredLogger`, `build_logger(...)`, and `parse_and_build_logger(...)` for config-driven logger assembly - feat: add `ConfiguredLogger::drain(...)`, `ConfiguredLogger::pending_count()`, and `ConfiguredLogger::dropped_count()` queue observability helpers - feat: add `bitlogger_async/` as a separate native-target async adapter package @@ -56,6 +57,7 @@ version 0.3.0 - test: cover parsing of core logger config fields - test: cover nested formatter and queue config parsing - test: cover config stringify and parse roundtrip behavior +- test: cover stable JSON export for reusable config subtypes - test: cover config-built queued text logger flushing and pending count behavior - test: cover partial drain behavior for config-built queued logger - test: cover dropped-count reporting for bounded config-built queue @@ -82,6 +84,7 @@ version 0.3.0 - docs: add `examples/async_basic` to show async worker startup and queue-backed logging flow - docs: update `examples/async_basic` to use unified JSON-driven async logger config - docs: update root README, English README, and Mooncake README with config usage notes +- docs: document standalone JSON export helpers for reusable config subtypes - docs: update formatter examples to demonstrate template-based text rendering - docs: update file sink examples to demonstrate rotation and backup retention - docs: document file sink reopen and observability counters in README variants