aboutsummaryrefslogtreecommitdiff
path: root/src/routes/mod.rs
blob: b81a0a15e94012867b24dc41098c3e2f1ce389ab (plain)
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
pub mod auth;
pub mod preferences;
pub mod query;

use crate::logging::logger::AuditLogger;
use tracing::{error, info, warn};

/// Helper function to log errors to both console (tracing) and file (AuditLogger)
/// This eliminates code duplication across route handlers
pub fn log_error_async(
    logging: &AuditLogger,
    request_id: &str,
    error_msg: &str,
    context: Option<&str>,
    username: Option<&str>,
    power: Option<i32>,
) {
    // Log to console immediately
    error!("[{}] {}", request_id, error_msg);

    // Clone everything needed for the async task
    let logging = logging.clone();
    let req_id = request_id.to_string();
    let error_msg = error_msg.to_string();
    let context = context.map(|s| s.to_string());
    let username = username.map(|s| s.to_string());

    // Spawn async task to log to file
    tokio::spawn(async move {
        let _ = logging
            .log_error(
                &req_id,
                chrono::Utc::now(),
                &error_msg,
                context.as_deref(),
                username.as_deref(),
                power,
            )
            .await;
    });
}

/// Helper function to log warnings to both console (tracing) and file (AuditLogger)
/// This eliminates code duplication across route handlers
pub fn log_warning_async(
    logging: &AuditLogger,
    request_id: &str,
    message: &str,
    context: Option<&str>,
    username: Option<&str>,
    power: Option<i32>,
) {
    // Log to console immediately
    warn!("[{}] {}", request_id, message);

    // Clone everything needed for the async task
    let logging = logging.clone();
    let req_id = request_id.to_string();
    let message = message.to_string();
    let context = context.map(|s| s.to_string());
    let username = username.map(|s| s.to_string());

    // Spawn async task to log to file
    tokio::spawn(async move {
        let _ = logging
            .log_warning(
                &req_id,
                chrono::Utc::now(),
                &message,
                context.as_deref(),
                username.as_deref(),
                power,
            )
            .await;
    });
}

/// Helper function to log info messages to both console (tracing) and file (AuditLogger)
/// This eliminates code duplication across route handlers
pub fn log_info_async(
    logging: &AuditLogger,
    request_id: &str,
    message: &str,
    context: Option<&str>,
    username: Option<&str>,
    power: Option<i32>,
) {
    // Log to console immediately
    info!("[{}] {}", request_id, message);

    // Clone everything needed for the async task
    let logging = logging.clone();
    let req_id = request_id.to_string();
    let message = message.to_string();
    let context = context.map(|s| s.to_string());
    let username = username.map(|s| s.to_string());

    // Spawn async task to log to file
    tokio::spawn(async move {
        let _ = logging
            .log_info(
                &req_id,
                chrono::Utc::now(),
                &message,
                context.as_deref(),
                username.as_deref(),
                power,
            )
            .await;
    });
}