Improve logging

This commit is contained in:
Lennart
2025-06-09 19:04:08 +02:00
parent 487e99216a
commit e000165555
3 changed files with 24 additions and 15 deletions

View File

@@ -1,14 +1,13 @@
pub use error::Error;
use serde::Serialize;
use sqlx::{Pool, Sqlite, SqlitePool, sqlite::SqliteConnectOptions};
use tracing::info;
pub mod addressbook_store;
pub mod calendar_store;
pub mod error;
pub mod principal_store;
pub mod subscription_store;
pub use error::Error;
#[derive(Debug, Clone, Serialize, sqlx::Type)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ChangeOperation {
@@ -38,7 +37,7 @@ pub async fn create_db_pool(db_url: &str, migrate: bool) -> Result<Pool<Sqlite>,
)
.await?;
if migrate {
println!("Running database migrations");
info!("Running database migrations");
sqlx::migrate!("./migrations").run(&db).await?;
}
Ok(db)

View File

@@ -1,6 +1,7 @@
use axum::Router;
use axum::extract::Request;
use axum::response::Response;
use reqwest::StatusCode;
use rustical_caldav::caldav_router;
use rustical_carddav::carddav_router;
use rustical_frontend::nextcloud_login::{NextcloudFlows, nextcloud_login_router};
@@ -16,6 +17,7 @@ use tower_http::trace::TraceLayer;
use tower_sessions::cookie::SameSite;
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
use tracing::Span;
use tracing::field::display;
use crate::config::NextcloudLoginConfig;
@@ -76,7 +78,7 @@ pub fn make_app<AS: AddressbookStore, CS: CalendarStore, S: SubscriptionStore>(
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request| {
tracing::debug_span!(
tracing::info_span!(
"http-request",
status_code = tracing::field::Empty,
otel.name = tracing::field::display(format!(
@@ -86,13 +88,22 @@ pub fn make_app<AS: AddressbookStore, CS: CalendarStore, S: SubscriptionStore>(
)),
)
})
.on_request(|_req: &Request, _span: &Span| {})
.on_request(|req: &Request, span: &Span| {
span.record("method", display(req.method()));
span.record("path", display(req.uri()));
})
.on_response(|response: &Response, _latency: Duration, span: &Span| {
span.record("status_code", tracing::field::display(response.status()));
span.record("status_code", display(response.status()));
if response.status().is_server_error() {
tracing::error!("status server error");
tracing::error!("server error");
} else if response.status().is_client_error() {
tracing::error!("status client error");
if response.status() == StatusCode::UNAUTHORIZED {
// The iOS client always tries an unauthenticated request first so
// logging 401's as errors would clog up our logs
tracing::debug!("unauthorized");
} else {
tracing::error!("client error");
}
};
})
.on_failure(

View File

@@ -22,6 +22,7 @@ use std::sync::Arc;
use tokio::sync::mpsc::Receiver;
use tower::Layer;
use tower_http::normalize_path::NormalizePathLayer;
use tracing::info;
mod app;
mod commands;
@@ -125,12 +126,10 @@ async fn main() -> Result<()> {
NormalizePathLayer::trim_trailing_slash().layer(app),
);
let listener = tokio::net::TcpListener::bind(&format!(
"{}:{}",
config.http.host, config.http.port
))
.await?;
tasks.push(tokio::spawn(async {
let address = format!("{}:{}", config.http.host, config.http.port);
let listener = tokio::net::TcpListener::bind(&address).await?;
tasks.push(tokio::spawn(async move {
info!("RustiCal serving on http://{address}");
axum::serve(listener, app).await.unwrap()
}));