mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
📝 Refresh runnable examples
This commit is contained in:
@@ -229,6 +229,7 @@ match logger.file_runtime_state() {
|
||||
|
||||
- `bitlogger/`:MoonBit 库 package,本体实现、测试与 Mooncake README
|
||||
- `examples/basic/`:最小可运行示例
|
||||
- `examples/async_basic/`:基于 `moonbitlang/async` 的异步 logger 示例
|
||||
|
||||
## 🔗 相关文档
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user