📝 Document 0.2 examples and usage patterns

This commit is contained in:
Nanaloveyuki
2026-05-08 16:24:51 +08:00
parent 50013f7f0d
commit 845718224b
5 changed files with 399 additions and 11 deletions
+94
View File
@@ -15,6 +15,17 @@ BitLogger currently provides:
- optional timestamps via `with_timestamp()`
- sink fanout via `fanout_sink(...)`
- custom integration via `callback_sink(...)`
- in-memory buffering via `buffered_sink(...)`
- record filtering via `filter_sink(...)`
- reusable filter helpers such as `target_has_prefix(...)`, `message_contains(...)`, `level_at_least(...)`, and `field_equals(...)`
- record patching via `with_patch(...)` and `patch_sink(...)`
- patch helpers such as `prefix_message(...)`, `append_fields(...)`, and `redact_fields(...)`
- explicit queued delivery via `queued_sink(...)` and `with_queue(...)`
- bounded backlog with `QueueOverflowPolicy::DropNewest` and `QueueOverflowPolicy::DropOldest`
- configurable text formatting via `text_formatter(...)`, `format_text(...)`, and `text_console_sink(...)`
- formatter-based callback integration via `formatted_callback_sink(...)`
- native-only file output via `file_sink(...)`
- `native_files_supported()` for backend capability detection
- default global logger helpers
## Quick Start
@@ -47,6 +58,89 @@ let hook = Logger::new(
hook.info("hello")
```
Basic buffered sink:
```moonbit
let sink = buffered_sink(console_sink(), flush_limit=2)
let logger = Logger::new(sink, target="buffered")
logger.info("one")
logger.info("two")
sink.flush()
```
Basic filter sink:
```moonbit
let sink = filter_sink(console_sink(), fn(rec) {
rec.target == "kept"
})
let kept = Logger::new(sink, target="kept")
let dropped = Logger::new(sink, target="dropped")
kept.info("visible")
dropped.info("hidden")
```
Chained logger filter:
```moonbit
let logger = Logger::new(console_sink(), target="service")
.with_filter(all_of([
target_has_prefix("service"),
message_contains("visible"),
]))
logger.info("hidden")
logger.child("api").info("visible")
```
Record patching:
```moonbit
let logger = Logger::new(console_sink(), target="auth")
.with_patch(compose_patches([
prefix_message("[safe] "),
redact_fields(["token"]),
append_fields([field("service", "bitlogger")]),
]))
logger.info("login", fields=[field("user", "alice"), field("token", "secret")])
```
Explicit queued sink:
```moonbit
let logger = Logger::new(console_sink(), target="queue")
.with_queue(max_pending=2, overflow=QueueOverflowPolicy::DropOldest)
logger.info("one")
logger.info("two")
logger.info("three")
ignore(logger.sink.flush())
```
Custom text formatter:
```moonbit
let formatter = text_formatter(show_timestamp=false, separator=" | ")
let logger = Logger::new(text_console_sink(formatter), target="pretty")
logger.info("hello", fields=[field("mode", "pretty")])
```
Native file sink:
```moonbit
if native_files_supported() {
let logger = Logger::new(file_sink("bitlogger.log"), target="file")
logger.info("hello", fields=[field("kind", "file")])
ignore(logger.sink.flush())
ignore(logger.sink.close())
}
```
## Repository Layout
- `bitlogger/`: MoonBit library package, tests, and Mooncake package README
+47
View File
@@ -0,0 +1,47 @@
## BitLogger Update Changes
version 0.2.0
### Feature
- feat: add `BufferedSink[S]` with in-memory buffering support
- feat: add `buffered_sink(...)` constructor with configurable `flush_limit`
- feat: add `BufferedSink::flush()` for manual drain
- feat: add `BufferedSink::pending_count()` for buffer inspection in tests and integration code
- feat: add `FilterSink[S]` for predicate-based record filtering
- feat: add `filter_sink(...)` constructor for composable sink routing
- feat: add `Logger::with_filter(...)` as a higher-level chained filter entry point
- feat: add reusable filter helpers such as `target_is(...)`, `target_has_prefix(...)`, `message_contains(...)`, `level_at_least(...)`, `has_field(...)`, and `field_equals(...)`
- feat: add predicate combinators `all_of(...)`, `any_of(...)`, and `not_(...)`
- feat: add `PatchSink[S]`, `patch_sink(...)`, and `Logger::with_patch(...)` for record transformation
- feat: add record patch helpers such as `prefix_message(...)`, `set_target(...)`, `append_fields(...)`, `redact_field(...)`, `redact_fields(...)`, and `compose_patches(...)`
- feat: add `QueuedSink[S]`, `queued_sink(...)`, and `Logger::with_queue(...)` for explicit queued delivery
- feat: add `QueueOverflowPolicy` with `DropNewest` and `DropOldest` bounded-backlog strategies
- feat: add configurable text formatter APIs `text_formatter(...)`, `format_text(...)`, and `format_json(...)`
- feat: add formatter-based sinks `formatted_console_sink(...)`, `text_console_sink(...)`, `formatted_callback_sink(...)`, and `text_callback_sink(...)`
- feat: add native-only `file_sink(...)` plus `native_files_supported()` backend capability detection
- feat: add explicit file sink lifecycle helpers `FileSink::is_available()`, `flush()`, and `close()`
### Test
- test: cover manual flush behavior for buffered sink
- test: cover auto flush when buffered records reach the configured limit
- test: cover predicate-based forwarding for filter sink
- test: cover chained `Logger::with_filter(...)` behavior with child target composition
- test: cover helper-based filter composition for target, level, message, and field matching
- test: cover record patch composition, target rewrite, message rewrite, field append, and field redaction
- test: cover queue drain order, bounded backlog drop behavior, and chained `Logger::with_queue(...)` usage
- test: cover formatter customization, formatter-only message mode, formatted callback output, and JSON formatter shape
- test: cover backend-aware file sink availability and lifecycle behavior
- test: keep callback-based composition validation with child target, context fields, and timestamp enabled
### Example
- docs: update `examples/basic` to demonstrate buffered sink usage
- docs: update `examples/basic` to demonstrate filter sink usage
- docs: update examples and README with helper-based chained logger filter usage
- docs: update examples and README with record patch usage for redaction and enrichment
- docs: update examples and README with explicit queued sink usage and overflow policy examples
- docs: update examples and README with configurable text formatter usage
- docs: update examples and README with native file sink usage and backend caveat
- docs: keep README focused on public usage and move change tracking to `docs/changes/`