diff --git a/README.md b/README.md index 6090fee..d997e9e 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,7 @@ match logger.file_runtime_state() { - `bitlogger/`:MoonBit 库 package,本体实现、测试与 Mooncake README - `examples/basic/`:最小可运行示例 +- `examples/async_basic/`:基于 `moonbitlang/async` 的异步 logger 示例 ## 🔗 相关文档 diff --git a/docs/README-en.md b/docs/README-en.md index f4b157e..c614eae 100644 --- a/docs/README-en.md +++ b/docs/README-en.md @@ -213,6 +213,7 @@ match logger.file_runtime_state() { - `bitlogger/`: MoonBit library package, tests, and Mooncake package README - `examples/basic/`: runnable example package +- `examples/async_basic/`: runnable async logger example built on `moonbitlang/async` ## Links diff --git a/docs/changes/0.3.0.md b/docs/changes/0.3.0.md index 053ad03..299dc5e 100644 --- a/docs/changes/0.3.0.md +++ b/docs/changes/0.3.0.md @@ -83,6 +83,7 @@ version 0.3.0 - docs: update `examples/basic` to use `parse_and_build_logger(...)` with explicit error handling in `main` - 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: refresh runnable examples to demonstrate config subtype JSON export, file policy export, and runtime file snapshot dumping - 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 diff --git a/examples/async_basic/main.mbt b/examples/async_basic/main.mbt index 4a44413..05cb9b4 100644 --- a/examples/async_basic/main.mbt +++ b/examples/async_basic/main.mbt @@ -1,12 +1,12 @@ async fn main { - let config = @lib_async.parse_async_logger_build_config_text( - "{\"logger\":{\"min_level\":\"info\",\"target\":\"async.demo\",\"timestamp\":true,\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"show_timestamp\":false,\"separator\":\" | \"}}},\"async_config\":{\"max_pending\":2,\"overflow\":\"DropOldest\"}}", - ) catch { + let raw = "{\"logger\":{\"min_level\":\"info\",\"target\":\"async.demo\",\"timestamp\":true,\"sink\":{\"kind\":\"text_console\",\"text_formatter\":{\"show_timestamp\":false,\"separator\":\" | \"}}},\"async_config\":{\"max_pending\":2,\"overflow\":\"DropOldest\",\"max_batch\":4,\"linger_ms\":5,\"flush\":\"Batch\"}}" + let config = @lib_async.parse_async_logger_build_config_text(raw) catch { err => { ignore(err) return } } + println(@lib_async.stringify_async_logger_build_config(config, pretty=true)) let logger = @lib_async.build_async_logger(config) .with_context_fields([@lib.field("service", "bitlogger")]) @@ -20,6 +20,7 @@ async fn main { group.spawn_bg(allow_failure=true, () => logger.run()) logger.info("one", fields=[@lib.field("token", "secret")]) logger.child("worker").info("two") + logger.child("worker").info("three", fields=[@lib.field("batch", "yes")]) logger.with_target("skip.demo").info("three") logger.shutdown() }) diff --git a/examples/basic/main.mbt b/examples/basic/main.mbt index 86d479d..046dcf9 100644 --- a/examples/basic/main.mbt +++ b/examples/basic/main.mbt @@ -142,4 +142,45 @@ fn main { } config_logger.info("configured from json") ignore(config_logger.flush()) + + let queue_config = @lib.QueueConfig::new(4, overflow=@lib.QueueOverflowPolicy::DropOldest) + println(@lib.stringify_queue_config(queue_config)) + + let formatter_config = @lib.TextFormatterConfig::new( + show_timestamp=false, + separator=" | ", + field_separator=",", + template="[{level}] {target} {message} :: {fields}", + ) + println(@lib.stringify_text_formatter_config(formatter_config)) + + let sink_config = @lib.SinkConfig::new( + kind=@lib.SinkKind::File, + path="bitlogger-config-demo.log", + append=false, + auto_flush=false, + rotation=Some(@lib.file_rotation(128, max_backups=2)), + text_formatter=formatter_config, + ) + println(@lib.stringify_sink_config(sink_config)) + + let file_policy = @lib.FileSinkPolicy::new( + append=false, + auto_flush=false, + rotation=Some(@lib.file_rotation(64, max_backups=2)), + ) + println(@lib.stringify_file_sink_policy(file_policy)) + + let file_runtime_logger = @lib.build_logger( + @lib.LoggerConfig::new( + sink=@lib.SinkConfig::new(kind=@lib.SinkKind::File, path="bitlogger-runtime-demo.log"), + queue=Some(@lib.QueueConfig::new(8)), + ), + ) + file_runtime_logger.info("runtime file logger ready") + match file_runtime_logger.file_runtime_state() { + Some(snapshot) => println(@lib.stringify_runtime_file_state(snapshot, pretty=true)) + None => () + } + ignore(file_runtime_logger.file_close()) }