Skip to main content

TestLogger

Struct TestLogger 

Source
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

Source

pub fn new() -> Self

Creates a new test logger.

Source

pub fn with_echo() -> Self

Creates a logger that echoes logs to stderr.

Source

pub fn log(&self, entry: CapturedLog)

Captures a log entry.

Source

pub fn log_entry(&self, entry: &LogEntry)

Captures a log from a LogEntry.

Source

pub fn log_message( &self, level: LogLevel, message: impl Into<String>, request_id: u64, )

Logs a message directly (convenience method).

Source

pub fn logs(&self) -> Vec<CapturedLog>

Gets all captured logs.

Source

pub fn count(&self) -> usize

Gets the number of captured logs.

Source

pub fn clear(&self)

Clears all captured logs.

Source

pub fn contains_message(&self, text: &str) -> bool

Checks if any log contains the given message substring.

Source

pub fn count_by_level(&self, level: LogLevel) -> usize

Counts logs by level.

Source

pub fn logs_at_level(&self, level: LogLevel) -> Vec<CapturedLog>

Gets logs at a specific level.

Source

pub fn failure_context(&self, n: usize) -> String

Gets the last N logs for failure context.

Source

pub fn timings(&self) -> TestTimings

Gets timing breakdown.

Source

pub fn start_phase(&self)

Starts timing a phase.

Source

pub fn end_setup(&self)

Marks end of setup phase.

Source

pub fn end_execute(&self)

Marks end of execute phase.

Source

pub fn end_teardown(&self)

Marks end of teardown phase.

Source

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.

Source

pub fn capture_phased<S, E, D, T>( setup: S, execute: E, teardown: D, ) -> LogCapture<T>
where S: FnOnce(&TestLogger), E: FnOnce(&TestLogger) -> T, D: FnOnce(&TestLogger),

Runs a test with setup, execute, and teardown phases.

Trait Implementations§

Source§

impl Clone for TestLogger

Source§

fn clone(&self) -> TestLogger

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TestLogger

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TestLogger

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T