Change logging backend to tracing

This commit is contained in:
Cappy Ishihara 2024-06-02 04:11:18 +07:00
parent 92d2a0d906
commit ae260cea85
No known key found for this signature in database
GPG key ID: 50862C285CB76906
6 changed files with 27 additions and 50 deletions

View file

@ -8,9 +8,9 @@ use axum::{
routing::get,
Router,
};
use log::{debug, info, trace};
use ring::digest::{self, digest};
use serde::Deserialize;
use tracing::{debug, info, trace};
use uuid::Uuid;
use crate::AppState;
@ -108,7 +108,7 @@ where
.headers
.get("token")
.and_then(|value| value.to_str().ok());
trace!("[Extractor Token] Data: {token:?}");
trace!(token = ?token);
match token {
Some(token) => Ok(Self(Some(token.to_string()))),
None => Ok(Self(None)),
@ -134,6 +134,15 @@ impl ToString for AuthSystem {
}
}
/// Get UUID from JSON response
// Written to be reusable so we don't have to specify the same complex code twice
#[inline]
fn get_id_json(json: &serde_json::Value) -> anyhow::Result<Uuid> {
trace!("json: {json:#?}"); // For debugging, we'll get to this later!
let uuid = Uuid::parse_str(json.get("id").unwrap().as_str().unwrap())?;
Ok(uuid)
}
pub async fn has_joined(
server_id: &str,
username: &str,
@ -147,7 +156,7 @@ pub async fn has_joined(
match res.status().as_u16() {
200 => {
let json = serde_json::from_str::<serde_json::Value>(&res.text().await?)?;
let uuid = Uuid::parse_str(json["id"].as_str().unwrap())?;
let uuid = get_id_json(&json)?;
Ok(Some((uuid, AuthSystem::ElyBy)))
},
401 => Ok(None),
@ -161,7 +170,7 @@ pub async fn has_joined(
match res.status().as_u16() {
200 => {
let json = serde_json::from_str::<serde_json::Value>(&res.text().await?)?;
let uuid = Uuid::parse_str(json["id"].as_str().unwrap())?;
let uuid = get_id_json(&json)?;
Ok(Some((uuid, AuthSystem::Mojang)))
},
204 => Ok(None),

View file

@ -5,10 +5,10 @@ use axum::{
Router,
};
use dashmap::DashMap;
use log::info;
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
use tower_http::trace::TraceLayer;
use tracing::info;
use uuid::Uuid;
// WebSocket worker
@ -97,7 +97,9 @@ pub struct AppState {
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::fmt()
.with_env_filter("trace,axum=info,tower_http=info,tokio=info,tungstenite=info,tokio_tungstenite=info")
.with_env_filter(
"trace,axum=info,tower_http=info,tokio=info,tungstenite=info,tokio_tungstenite=info",
)
.pretty()
.init();

View file

@ -5,7 +5,7 @@ use axum::{
extract::{Path, State},
Json,
};
use log::{debug, warn};
use tracing::{debug, warn};
use serde_json::{json, Value};
use tokio::{
fs,
@ -25,7 +25,7 @@ pub async fn user_info(
Path(uuid): Path<Uuid>,
State(state): State<AppState>, // FIXME: Variable doesn't using!
) -> Json<Value> {
log::info!("Receiving profile information for {}", uuid);
tracing::info!("Receiving profile information for {}", uuid);
let formatted_uuid = format_uuid(&uuid);
@ -88,7 +88,7 @@ pub async fn user_info(
#[debug_handler]
pub async fn download_avatar(Path(uuid): Path<Uuid>) -> Result<Vec<u8>> {
let uuid = format_uuid(&uuid);
log::info!("Requesting an avatar: {}", uuid);
tracing::info!("Requesting an avatar: {}", uuid);
let mut file = if let Ok(file) = fs::File::open(format!("avatars/{}.moon", uuid)).await {
file
} else {
@ -118,7 +118,7 @@ pub async fn upload_avatar(
};
if let Some(user_info) = state.authenticated.get(&token) {
log::info!(
tracing::info!(
"{} ({}) trying to upload an avatar",
user_info.uuid,
user_info.username
@ -152,7 +152,7 @@ pub async fn delete_avatar(Token(token): Token, State(state): State<AppState>) -
None => http_error_ret!(UNAUTHORIZED, "Authentication error!"),
};
if let Some(user_info) = state.authenticated.get(&token) {
log::info!(
tracing::info!(
"{} ({}) is trying to delete the avatar",
user_info.uuid,
user_info.username

View file

@ -8,7 +8,7 @@ use axum::{
response::Response,
};
use dashmap::DashMap;
use log::{debug, error, info, trace, warn};
use tracing::{debug, error, info, trace, warn};
use tokio::sync::{
broadcast::{self, Receiver},
mpsc, Notify,