feat: add logging
This commit is contained in:
parent
0f0f7f3961
commit
ed4322b8e9
5 changed files with 254 additions and 6 deletions
|
|
@ -1,9 +1,11 @@
|
|||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use serde::Serialize;
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
#[derive(Clone, Debug, strum_macros::AsRefStr)]
|
||||
#[derive(Clone, Debug, Serialize, strum_macros::AsRefStr)]
|
||||
#[serde(tag = "type", content = "data")]
|
||||
pub enum Error {
|
||||
LoginFail,
|
||||
|
||||
|
|
|
|||
65
src/log.rs
Normal file
65
src/log.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
use std::time::SystemTime;
|
||||
|
||||
use crate::{Error, Result};
|
||||
use crate::ctx::Ctx;
|
||||
use crate::error::ClientError;
|
||||
use axum::http::{Method, Uri};
|
||||
use serde::Serialize;
|
||||
use serde_json::{json, Value};
|
||||
use serde_with::skip_serializing_none;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn log_request(
|
||||
uuid: Uuid,
|
||||
req_method: Method,
|
||||
uri: Uri,
|
||||
ctx: Option<Ctx>,
|
||||
service_error: Option<&Error>,
|
||||
client_error: Option<ClientError>,
|
||||
) -> Result<()> {
|
||||
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis();
|
||||
|
||||
let error_type = service_error.map(|se| se.as_ref().to_string());
|
||||
let error_data = serde_json::to_value(service_error).ok().and_then(|mut v| v.get_mut("data").map(|v| v.take()));
|
||||
|
||||
// Create the RequestLogLine.
|
||||
let log_line = RequestLogLine {
|
||||
uuid: uuid.to_string(),
|
||||
timestamp: timestamp.to_string(),
|
||||
|
||||
req_path: uri.path().to_string(),
|
||||
req_method: req_method.to_string(),
|
||||
|
||||
user_id: ctx.map(|ctx| ctx.user_id()),
|
||||
|
||||
client_error_type: client_error.map(|e| e.as_ref().to_string()),
|
||||
|
||||
error_type,
|
||||
error_data,
|
||||
};
|
||||
|
||||
println!(" ->> log request: \n{}", json!(log_line));
|
||||
|
||||
// TODO: Send to cloud-watch or something.
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Serialize)]
|
||||
struct RequestLogLine {
|
||||
uuid: String, // uuid string formatted
|
||||
timestamp: String, // (should be iso8601)
|
||||
|
||||
// -- User and context attributes.
|
||||
user_id: Option<u64>,
|
||||
|
||||
// -- http request attributes.
|
||||
req_path: String,
|
||||
req_method: String,
|
||||
|
||||
// -- Errors attributes.
|
||||
client_error_type: Option<String>,
|
||||
error_type: Option<String>,
|
||||
error_data: Option<Value>,
|
||||
}
|
||||
10
src/main.rs
10
src/main.rs
|
|
@ -3,6 +3,8 @@
|
|||
use crate::model::ModelController;
|
||||
|
||||
pub use self::error::{Error, Result};
|
||||
use self::ctx::Ctx;
|
||||
use self::log::log_request;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
|
|
@ -20,6 +22,7 @@ use uuid::Uuid;
|
|||
|
||||
mod ctx;
|
||||
mod error;
|
||||
mod log;
|
||||
mod web;
|
||||
mod model;
|
||||
|
||||
|
|
@ -57,7 +60,7 @@ async fn main() -> Result<()>{
|
|||
/// Map the response to add headers, etc.
|
||||
///
|
||||
/// * `res`: the response to map
|
||||
async fn main_response_mapper(res: Response) -> Response {
|
||||
async fn main_response_mapper(ctx: Option<Ctx>, uri: Uri, req_method: Method, res: Response) -> Response {
|
||||
println!("->> {:<12} - main_response_mapper", "RES_MAPPER");
|
||||
let uuid = Uuid::new_v4();
|
||||
|
||||
|
|
@ -82,8 +85,9 @@ async fn main_response_mapper(res: Response) -> Response {
|
|||
(*status_code, Json(client_error_body)).into_response()
|
||||
});
|
||||
|
||||
// -- TODO: Build and log the server log line.
|
||||
println!(" ->> server log line - {uuid} - Error: {service_error:?}");
|
||||
// -- Build and log the server log line.
|
||||
let client_error = client_status_error.unzip().1;
|
||||
log_request(uuid, req_method, uri, ctx, service_error, client_error).await;
|
||||
|
||||
println!();
|
||||
error_response.unwrap_or(res)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue