pub struct TestLogger { /* private fields */ }Expand description
Test logger that captures logs for per-test isolation and assertions.
Use TestLogger::capture to run a test with isolated log capture,
then examine captured logs for assertions.
§Example
ⓘ
use fastapi_core::testing::TestLogger;
use fastapi_core::logging::LogLevel;
let capture = TestLogger::capture(|| {
let ctx = RequestContext::for_testing();
log_info!(ctx, "Hello from test");
log_debug!(ctx, "Debug info");
});
// Assert on captured logs
assert!(capture.contains_message("Hello from test"));
assert_eq!(capture.count_by_level(LogLevel::Info), 1);
// Get failure context (last N logs)
let context = capture.failure_context(5);Implementations§
Source§impl TestLogger
impl TestLogger
Sourcepub fn log(&self, entry: CapturedLog)
pub fn log(&self, entry: CapturedLog)
Captures a log entry.
Sourcepub fn log_message(
&self,
level: LogLevel,
message: impl Into<String>,
request_id: u64,
)
pub fn log_message( &self, level: LogLevel, message: impl Into<String>, request_id: u64, )
Logs a message directly (convenience method).
Sourcepub fn logs(&self) -> Vec<CapturedLog>
pub fn logs(&self) -> Vec<CapturedLog>
Gets all captured logs.
Sourcepub fn contains_message(&self, text: &str) -> bool
pub fn contains_message(&self, text: &str) -> bool
Checks if any log contains the given message substring.
Sourcepub fn count_by_level(&self, level: LogLevel) -> usize
pub fn count_by_level(&self, level: LogLevel) -> usize
Counts logs by level.
Sourcepub fn logs_at_level(&self, level: LogLevel) -> Vec<CapturedLog>
pub fn logs_at_level(&self, level: LogLevel) -> Vec<CapturedLog>
Gets logs at a specific level.
Sourcepub fn failure_context(&self, n: usize) -> String
pub fn failure_context(&self, n: usize) -> String
Gets the last N logs for failure context.
Sourcepub fn timings(&self) -> TestTimings
pub fn timings(&self) -> TestTimings
Gets timing breakdown.
Sourcepub fn start_phase(&self)
pub fn start_phase(&self)
Starts timing a phase.
Sourcepub fn end_execute(&self)
pub fn end_execute(&self)
Marks end of execute phase.
Sourcepub fn end_teardown(&self)
pub fn end_teardown(&self)
Marks end of teardown phase.
Sourcepub fn capture<F, T>(f: F) -> LogCapture<T>where
F: FnOnce(&TestLogger) -> T,
pub fn capture<F, T>(f: F) -> LogCapture<T>where
F: FnOnce(&TestLogger) -> T,
Runs a closure with log capture, returning a LogCapture result.
This is the primary API for isolated test logging.
Sourcepub fn capture_phased<S, E, D, T>(
setup: S,
execute: E,
teardown: D,
) -> LogCapture<T>
pub fn capture_phased<S, E, D, T>( setup: S, execute: E, teardown: D, ) -> LogCapture<T>
Runs a test with setup, execute, and teardown phases.
Trait Implementations§
Source§impl Clone for TestLogger
impl Clone for TestLogger
Source§fn clone(&self) -> TestLogger
fn clone(&self) -> TestLogger
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TestLogger
impl Debug for TestLogger
Auto Trait Implementations§
impl Freeze for TestLogger
impl !RefUnwindSafe for TestLogger
impl Send for TestLogger
impl Sync for TestLogger
impl Unpin for TestLogger
impl !UnwindSafe for TestLogger
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).