rustycord 0.1.5

A fast, lightweight, and feature-rich Discord bot library written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Logging Example

Comprehensive guide to logging in rustycord applications.

## Overview

RustyCord provides a sophisticated logging system built on top of Rust's `log` crate with `fern` for configuration. The logging system supports multiple output targets, log levels, and custom formatting.

## Basic Logging Setup

```rust
use rustycord::{Bot, logger};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Basic logging setup
    logger::setup_logger("info".to_string())?;
    
    log::info!("๐Ÿš€ Starting RustyCord bot...");
    
    let token = std::env::var("DISCORD_TOKEN")?;
    let mut bot = Bot::new(None).await;
    
    log::info!("๐Ÿ”‘ Logging in to Discord...");
    let user_response = bot.login(token).await;
    log::info!("โœ… Logged in as: {}", user_response.username);
    
    log::info!("๐ŸŽฏ Starting bot event loop...");
    bot.start().await?;
    
    Ok(())
}
```

## Log Levels

RustyCord supports five log levels:

```rust
use log::{trace, debug, info, warn, error};

// TRACE: Very detailed information, typically only of interest when diagnosing problems
trace!("๐Ÿ” Processing message ID: {}", message.id);

// DEBUG: Detailed information, typically only of interest when diagnosing problems
debug!("๐Ÿ› Message handler executed: {}", handler_name);

// INFO: General information about program execution
info!("๐Ÿ“ข Bot connected to Discord Gateway");

// WARN: Something unexpected happened, but the program can continue
warn!("โš ๏ธ Rate limit approaching for endpoint: {}", endpoint);

// ERROR: Serious problems that might cause the program to abort
error!("โŒ Failed to send message: {}", error);
```

## Advanced Logging Configuration

```rust
use rustycord::logger;
use log::LevelFilter;
use fern::Dispatch;
use chrono::Local;

fn setup_advanced_logging() -> Result<(), Box<dyn std::error::Error>> {
    Dispatch::new()
        .format(|out, message, record| {
            out.finish(format_args!(
                "{}[{}][{}] {}",
                Local::now().format("%Y-%m-%d %H:%M:%S"),
                record.target(),
                record.level(),
                message
            ))
        })
        .level(LevelFilter::Info)
        .level_for("rustycord::gateway", LevelFilter::Debug)
        .level_for("rustycord::http", LevelFilter::Warn)
        .chain(std::io::stdout())
        .chain(fern::log_file("bot.log")?)
        .apply()?;
    
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    setup_advanced_logging()?;
    
    log::info!("๐Ÿš€ Bot starting with advanced logging...");
    
    // Your bot code here...
    
    Ok(())
}
```

## Structured Logging

```rust
use serde_json::json;

// Structured logging for better analytics
fn log_command_execution(user_id: &str, command: &str, execution_time: u64, success: bool) {
    let log_data = json!({
        "event": "command_execution",
        "user_id": user_id,
        "command": command,
        "execution_time_ms": execution_time,
        "success": success,
        "timestamp": chrono::Utc::now().to_rfc3339()
    });
    
    if success {
        info!("Command executed: {}", log_data);
    } else {
        warn!("Command failed: {}", log_data);
    }
}

// Usage in message handler
#[async_trait]
impl MessageHandler for LoggingHandler {
    async fn handle_message(&self, message: &Message) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
        let start_time = std::time::Instant::now();
        
        if message.content.starts_with("!test") {
            debug!("๐Ÿงช Processing test command from user: {}", message.author.username);
            
            let result = self.process_test_command(message).await;
            let execution_time = start_time.elapsed().as_millis() as u64;
            
            log_command_execution(
                &message.author.id,
                "test",
                execution_time,
                result.is_ok()
            );
            
            result
        } else {
            Ok(None)
        }
    }
}
```

## File Logging with Rotation

```rust
use fern::Dispatch;
use log::LevelFilter;
use chrono::Local;
use std::fs;

fn setup_file_logging() -> Result<(), Box<dyn std::error::Error>> {
    // Create logs directory if it doesn't exist
    fs::create_dir_all("logs")?;
    
    let log_filename = format!("logs/bot-{}.log", Local::now().format("%Y-%m-%d"));
    
    Dispatch::new()
        .format(|out, message, record| {
            out.finish(format_args!(
                "{} [{}] [{}:{}] {}",
                Local::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                record.level(),
                record.file().unwrap_or("unknown"),
                record.line().unwrap_or(0),
                message
            ))
        })
        .level(LevelFilter::Info)
        .chain(
            Dispatch::new()
                .filter(|metadata| metadata.target().starts_with("rustycord"))
                .chain(fern::log_file(&log_filename)?)
        )
        .chain(
            Dispatch::new()
                .level(LevelFilter::Warn)
                .chain(std::io::stderr())
        )
        .apply()?;
    
    info!("๐Ÿ“ Logging to file: {}", log_filename);
    Ok(())
}
```

## Performance Logging

```rust
use std::time::Instant;

struct PerformanceLogger;

impl PerformanceLogger {
    fn log_function_duration<F, R>(func_name: &str, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let start = Instant::now();
        let result = f();
        let duration = start.elapsed();
        
        if duration.as_millis() > 100 {
            warn!("โฑ๏ธ Slow operation: {} took {}ms", func_name, duration.as_millis());
        } else {
            debug!("โšก Fast operation: {} took {}ms", func_name, duration.as_millis());
        }
        
        result
    }
    
    async fn log_async_duration<F, Fut, R>(func_name: &str, f: F) -> R
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = R>,
    {
        let start = Instant::now();
        let result = f().await;
        let duration = start.elapsed();
        
        info!("๐Ÿš€ Async operation: {} completed in {}ms", func_name, duration.as_millis());
        result
    }
}

// Usage example
impl MessageHandler for PerformanceMonitoringHandler {
    async fn handle_message(&self, message: &Message) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
        if message.content == "!performance" {
            let result = PerformanceLogger::log_async_duration("database_query", || async {
                // Simulate database query
                tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
                "Query completed"
            }).await;
            
            Ok(Some(format!("Performance test: {}", result)))
        } else {
            Ok(None)
        }
    }
}
```

## Error Logging with Context

```rust
use anyhow::{Context, Result};

async fn process_user_command(message: &Message) -> Result<String> {
    debug!("๐Ÿ”„ Processing command from user: {}", message.author.username);
    
    let user_data = fetch_user_data(&message.author.id)
        .await
        .with_context(|| format!("Failed to fetch data for user {}", message.author.id))?;
    
    let processed_data = process_data(user_data)
        .with_context(|| "Failed to process user data")?;
    
    info!("โœ… Successfully processed command for user: {}", message.author.username);
    Ok(processed_data)
}

async fn fetch_user_data(user_id: &str) -> Result<String> {
    // Simulate potential failure
    if user_id == "error_user" {
        error!("โŒ Simulated error for user: {}", user_id);
        anyhow::bail!("User not found in database");
    }
    
    debug!("๐Ÿ“Š Fetched data for user: {}", user_id);
    Ok(format!("data_for_{}", user_id))
}

fn process_data(data: String) -> Result<String> {
    if data.contains("invalid") {
        error!("โŒ Invalid data format: {}", data);
        anyhow::bail!("Invalid data format");
    }
    
    debug!("๐Ÿ”„ Processing data: {}", data);
    Ok(format!("processed_{}", data))
}
```

## Logging Best Practices

### 1. Use Appropriate Log Levels

```rust
// โœ… Good: Appropriate log levels
debug!("๐Ÿ” Validating message format");
info!("๐Ÿ“ข User {} joined the server", username);
warn!("โš ๏ธ Rate limit reached, slowing down requests");
error!("โŒ Failed to connect to database: {}", error);

// โŒ Bad: Wrong log levels
error!("User sent a message"); // This should be debug or trace
info!("Database connection failed"); // This should be error
```

### 2. Include Context

```rust
// โœ… Good: Includes relevant context
info!("๐Ÿ“จ Message received in #{} from {}: {}", 
      channel_name, username, message_preview);
warn!("โš ๏ธ Rate limit hit for endpoint {} (remaining: {})", 
      endpoint, remaining_requests);

// โŒ Bad: No context
info!("Message received");
warn!("Rate limit hit");
```

### 3. Use Emojis for Visual Scanning

```rust
trace!("๐Ÿ” Detailed debug info");
debug!("๐Ÿ› Debug information");
info!("๐Ÿ“ข General information");
warn!("โš ๏ธ Warning message");
error!("โŒ Error occurred");
```

### 4. Log Structured Data

```rust
// For important events, log structured data
info!(
    "user_action"; 
    "user_id" => %user.id,
    "action" => "message_sent",
    "channel_id" => %channel.id,
    "message_length" => message.content.len(),
    "timestamp" => %chrono::Utc::now()
);
```

## Production Logging Setup

```rust
use fern::Dispatch;
use log::LevelFilter;

pub fn setup_production_logging() -> Result<(), Box<dyn std::error::Error>> {
    let log_level = std::env::var("LOG_LEVEL")
        .unwrap_or_else(|_| "info".to_string())
        .to_lowercase();
    
    let level = match log_level.as_str() {
        "trace" => LevelFilter::Trace,
        "debug" => LevelFilter::Debug,
        "info" => LevelFilter::Info,
        "warn" => LevelFilter::Warn,
        "error" => LevelFilter::Error,
        _ => LevelFilter::Info,
    };
    
    Dispatch::new()
        .format(|out, message, record| {
            out.finish(format_args!(
                "{} [{}] [{}] {}",
                chrono::Utc::now().to_rfc3339(),
                record.level(),
                record.target(),
                message
            ))
        })
        .level(level)
        // Reduce noise from dependencies
        .level_for("hyper", LevelFilter::Warn)
        .level_for("reqwest", LevelFilter::Warn)
        .level_for("tungstenite", LevelFilter::Warn)
        // File output for persistent logs
        .chain(fern::log_file("bot.log")?)
        // Console output for immediate feedback
        .chain(std::io::stdout())
        .apply()?;
    
    info!("๐Ÿš€ Logging initialized at level: {}", log_level);
    Ok(())
}
```

## Monitoring and Alerts

```rust
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

pub struct LoggingMetrics {
    error_count: AtomicU64,
    warn_count: AtomicU64,
    last_error_time: AtomicU64,
}

impl LoggingMetrics {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            error_count: AtomicU64::new(0),
            warn_count: AtomicU64::new(0),
            last_error_time: AtomicU64::new(0),
        })
    }
    
    pub fn record_error(&self) {
        self.error_count.fetch_add(1, Ordering::Relaxed);
        self.last_error_time.store(
            chrono::Utc::now().timestamp() as u64,
            Ordering::Relaxed
        );
        
        let error_count = self.error_count.load(Ordering::Relaxed);
        if error_count % 10 == 0 {
            warn!("๐Ÿšจ High error rate detected: {} errors", error_count);
        }
    }
    
    pub fn record_warning(&self) {
        self.warn_count.fetch_add(1, Ordering::Relaxed);
    }
    
    pub fn get_stats(&self) -> (u64, u64, u64) {
        (
            self.error_count.load(Ordering::Relaxed),
            self.warn_count.load(Ordering::Relaxed),
            self.last_error_time.load(Ordering::Relaxed),
        )
    }
}
```

## Testing Logging

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use log::Level;

    #[test]
    fn test_logging_setup() {
        // Test that logging can be initialized
        assert!(logger::setup_logger("debug".to_string()).is_ok());
        
        // Test logging at different levels
        log::debug!("Test debug message");
        log::info!("Test info message");
        log::warn!("Test warning message");
        log::error!("Test error message");
    }
    
    #[test]
    fn test_log_levels() {
        let levels = vec!["trace", "debug", "info", "warn", "error"];
        
        for level in levels {
            assert!(logger::setup_logger(level.to_string()).is_ok());
        }
    }
}
```

## Related Documentation

- [Basic Bot Example]basic-bot.md - Simple logging usage
- [Message Handler Example]message-handler.md - Logging in handlers
- [Error Handling Guide]../user-guide/error-handling.md - Error logging patterns