pub struct LoggerMiddleware { /* private fields */ }Expand description
Built-in logger middleware for logging WebSocket messages.
This middleware automatically logs incoming messages, responses, and errors
with timing information. It integrates with the tracing crate for
structured logging.
§Features
- Automatic timing: Measures and logs processing duration
- Connection tracking: Logs connection ID with each message
- Message type detection: Identifies Text/Binary/Ping/Pong/Close messages
- Error logging: Captures and logs handler errors
- Configurable verbosity: Three log levels (Debug/Info/Warn)
§Performance
The middleware has minimal overhead:
- ~1-2µs per message for timing
- Zero-copy message passing
- Efficient structured logging with
tracing
§Examples
§Default Configuration
use wsforge::prelude::*;
let logger = LoggerMiddleware::new();
// Uses LogLevel::Info by default§Custom Log Level
use wsforge::prelude::*;
// Very verbose logging
let debug_logger = LoggerMiddleware::with_level(LogLevel::Debug);
// Minimal logging
let warn_logger = LoggerMiddleware::with_level(LogLevel::Warn);§In Router
use wsforge::prelude::*;
async fn handler(msg: Message) -> Result<String> {
Ok("processed".to_string())
}
let router = Router::new()
.layer(LoggerMiddleware::new())
.default_handler(handler(handler));
router.listen("127.0.0.1:8080").await?;§Multiple Middleware
use wsforge::prelude::*;
let router = Router::new()
.layer(LoggerMiddleware::new()) // First: log incoming
.layer(auth_middleware()) // Second: authenticate
.layer(rate_limit_middleware()); // Third: rate limitImplementations§
Source§impl LoggerMiddleware
impl LoggerMiddleware
Sourcepub fn new() -> Arc<Self>
pub fn new() -> Arc<Self>
Creates a new logger middleware with default settings.
Uses LogLevel::Info by default, which provides standard logging
suitable for production environments.
§Examples
use wsforge::prelude::*;
let logger = LoggerMiddleware::new();Sourcepub fn with_level(level: LogLevel) -> Arc<Self>
pub fn with_level(level: LogLevel) -> Arc<Self>
Creates a logger middleware with a custom log level.
§Arguments
level- The log level to use
§Examples
use wsforge::prelude::*;
// Debug level for development
let debug = LoggerMiddleware::with_level(LogLevel::Debug);
// Info level for production
let info = LoggerMiddleware::with_level(LogLevel::Info);
// Warn level for minimal logging
let warn = LoggerMiddleware::with_level(LogLevel::Warn);Trait Implementations§
Source§impl Default for LoggerMiddleware
impl Default for LoggerMiddleware
Source§impl Middleware for LoggerMiddleware
impl Middleware for LoggerMiddleware
Source§fn handle<'life0, 'async_trait>(
&'life0 self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
next: Next,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle<'life0, 'async_trait>(
&'life0 self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
next: Next,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handle a message and optionally pass it to the next middleware. Read more
Auto Trait Implementations§
impl Freeze for LoggerMiddleware
impl RefUnwindSafe for LoggerMiddleware
impl Send for LoggerMiddleware
impl Sync for LoggerMiddleware
impl Unpin for LoggerMiddleware
impl UnwindSafe for LoggerMiddleware
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