mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-07-27 10:22:18 +00:00
✨ add wasm coverage and trace context
This commit is contained in:
@@ -455,6 +455,30 @@ async test "async logger with_context_fields prepends shared fields without muta
|
||||
inspect(written_fields.val[2].value, content="test")
|
||||
}
|
||||
|
||||
///|
|
||||
test "async facades bind trace context through standard fields" {
|
||||
let context = @bitlogger.trace_context("trace-async", "span-async")
|
||||
let base = async_logger(
|
||||
@bitlogger.callback_sink(fn(_) { }),
|
||||
config=AsyncLoggerConfig::new(max_pending=4),
|
||||
min_level=@bitlogger.Level::Info,
|
||||
)
|
||||
let derived = base.with_trace_context(context)
|
||||
inspect(base.context_fields.length(), content="0")
|
||||
inspect(derived.context_fields.length(), content="3")
|
||||
inspect(derived.context_fields[0].key, content="trace_id")
|
||||
inspect(derived.context_fields[0].value, content="trace-async")
|
||||
inspect(derived.context_fields[1].key, content="span_id")
|
||||
inspect(derived.context_fields[2].value, content="01")
|
||||
|
||||
let library = LibraryAsyncLogger::new(@bitlogger.callback_sink(fn(_) { })).with_trace_context(
|
||||
context,
|
||||
)
|
||||
let full = library.to_async_logger()
|
||||
inspect(full.context_fields.length(), content="3")
|
||||
inspect(full.context_fields[1].value, content="span-async")
|
||||
}
|
||||
|
||||
///|
|
||||
async test "async logger child composes target and preserves other state" {
|
||||
let written_target : Ref[String] = Ref("")
|
||||
|
||||
@@ -274,6 +274,14 @@ pub fn[S] AsyncLogger::with_context_fields(
|
||||
{ ..self, context_fields: fields }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[S] AsyncLogger::with_trace_context(
|
||||
self : AsyncLogger[S],
|
||||
context : @bitlogger.TraceContext,
|
||||
) -> AsyncLogger[S] {
|
||||
self.with_context_fields(context.as_fields())
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[S] AsyncLogger::with_filter(
|
||||
self : AsyncLogger[S],
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
///|
|
||||
async test "native async file lifecycle flushes rotates and restarts" {
|
||||
if !@bitlogger.native_files_supported() {
|
||||
inspect(@bitlogger.native_files_supported(), content="false")
|
||||
} else {
|
||||
let path = "logs/bitlogger-async-native-e2e.log"
|
||||
let first_flushes : Ref[Int] = Ref(0)
|
||||
let first_sink = @bitlogger.file_sink(
|
||||
path,
|
||||
append=false,
|
||||
auto_flush=false,
|
||||
rotation=Some(@bitlogger.file_rotation(8, max_backups=1)),
|
||||
formatter=fn(rec) { rec.message },
|
||||
)
|
||||
let first = async_logger(
|
||||
first_sink,
|
||||
config=AsyncLoggerConfig::new(
|
||||
max_pending=4,
|
||||
overflow=AsyncOverflowPolicy::Blocking,
|
||||
max_batch=4,
|
||||
linger_ms=0,
|
||||
flush=AsyncFlushPolicy::Batch,
|
||||
),
|
||||
min_level=@bitlogger.Level::Info,
|
||||
target="async.file.e2e",
|
||||
flush=fn(sink) {
|
||||
first_flushes.val += 1
|
||||
ignore(sink.flush())
|
||||
},
|
||||
)
|
||||
|
||||
@async.with_task_group(group => {
|
||||
group.spawn_bg(() => first.run())
|
||||
first.info("first-record")
|
||||
first.shutdown()
|
||||
})
|
||||
|
||||
inspect(first.is_closed(), content="true")
|
||||
inspect(first.has_failed(), content="false")
|
||||
inspect(first.pending_count(), content="0")
|
||||
inspect(first_flushes.val > 0, content="true")
|
||||
inspect(first_sink.path(), content="logs/bitlogger-async-native-e2e.log")
|
||||
inspect(first_sink.rotation_enabled(), content="true")
|
||||
inspect(first_sink.rotation_failures(), content="0")
|
||||
inspect(first_sink.write_failures(), content="0")
|
||||
inspect(first_sink.flush_failures(), content="0")
|
||||
inspect(first_sink.close(), content="true")
|
||||
|
||||
let restart_flushes : Ref[Int] = Ref(0)
|
||||
let restarted_sink = @bitlogger.file_sink(
|
||||
path,
|
||||
append=true,
|
||||
auto_flush=false,
|
||||
rotation=Some(@bitlogger.file_rotation(8, max_backups=1)),
|
||||
formatter=fn(rec) { rec.message },
|
||||
)
|
||||
let restarted = async_logger(
|
||||
restarted_sink,
|
||||
config=AsyncLoggerConfig::new(
|
||||
max_pending=4,
|
||||
overflow=AsyncOverflowPolicy::Blocking,
|
||||
max_batch=4,
|
||||
linger_ms=0,
|
||||
flush=AsyncFlushPolicy::Shutdown,
|
||||
),
|
||||
min_level=@bitlogger.Level::Info,
|
||||
target="async.file.e2e",
|
||||
flush=fn(sink) {
|
||||
restart_flushes.val += 1
|
||||
ignore(sink.flush())
|
||||
},
|
||||
)
|
||||
|
||||
@async.with_task_group(group => {
|
||||
group.spawn_bg(() => restarted.run())
|
||||
restarted.info("second-record")
|
||||
restarted.shutdown()
|
||||
})
|
||||
|
||||
inspect(restarted.is_closed(), content="true")
|
||||
inspect(restarted.has_failed(), content="false")
|
||||
inspect(restarted.pending_count(), content="0")
|
||||
inspect(restart_flushes.val, content="1")
|
||||
inspect(restarted_sink.rotation_enabled(), content="true")
|
||||
inspect(restarted_sink.rotation_failures(), content="0")
|
||||
inspect(restarted_sink.write_failures(), content="0")
|
||||
inspect(restarted_sink.flush_failures(), content="0")
|
||||
inspect(restarted_sink.close(), content="true")
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,14 @@ pub fn[S] LibraryAsyncLogger::bind(
|
||||
self.with_context_fields(fields)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[S] LibraryAsyncLogger::with_trace_context(
|
||||
self : LibraryAsyncLogger[S],
|
||||
context : @bitlogger.TraceContext,
|
||||
) -> LibraryAsyncLogger[S] {
|
||||
library_async_logger(self.inner.with_trace_context(context))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[S] LibraryAsyncLogger::is_enabled(
|
||||
self : LibraryAsyncLogger[S],
|
||||
|
||||
@@ -114,6 +114,7 @@ pub fn[S] AsyncLogger::with_min_level(Self[S], @core.Level) -> Self[S]
|
||||
pub fn[S] AsyncLogger::with_patch(Self[S], (@core.Record) -> @core.Record) -> Self[S]
|
||||
pub fn[S] AsyncLogger::with_target(Self[S], String) -> Self[S]
|
||||
pub fn[S] AsyncLogger::with_timestamp(Self[S], enabled? : Bool) -> Self[S]
|
||||
pub fn[S] AsyncLogger::with_trace_context(Self[S], @core.TraceContext) -> Self[S]
|
||||
|
||||
pub struct LibraryAsyncLogger[S] {
|
||||
inner : AsyncLogger[S]
|
||||
@@ -131,6 +132,7 @@ pub fn[S] LibraryAsyncLogger::to_async_logger(Self[S]) -> AsyncLogger[S]
|
||||
pub async fn[S] LibraryAsyncLogger::warn(Self[S], String, fields? : Array[@core.Field]) -> Unit
|
||||
pub fn[S] LibraryAsyncLogger::with_context_fields(Self[S], Array[@core.Field]) -> Self[S]
|
||||
pub fn[S] LibraryAsyncLogger::with_target(Self[S], String) -> Self[S]
|
||||
pub fn[S] LibraryAsyncLogger::with_trace_context(Self[S], @core.TraceContext) -> Self[S]
|
||||
|
||||
// Type aliases
|
||||
pub type ApplicationAsyncLogger = AsyncLogger[@runtime.RuntimeSink]
|
||||
|
||||
Reference in New Issue
Block a user