mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 21:42:34 +00:00
Improve logging
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
|
pub use error::Error;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use sqlx::{Pool, Sqlite, SqlitePool, sqlite::SqliteConnectOptions};
|
use sqlx::{Pool, Sqlite, SqlitePool, sqlite::SqliteConnectOptions};
|
||||||
|
use tracing::info;
|
||||||
pub mod addressbook_store;
|
pub mod addressbook_store;
|
||||||
pub mod calendar_store;
|
pub mod calendar_store;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod principal_store;
|
pub mod principal_store;
|
||||||
pub mod subscription_store;
|
pub mod subscription_store;
|
||||||
|
|
||||||
pub use error::Error;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, sqlx::Type)]
|
#[derive(Debug, Clone, Serialize, sqlx::Type)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub(crate) enum ChangeOperation {
|
pub(crate) enum ChangeOperation {
|
||||||
@@ -38,7 +37,7 @@ pub async fn create_db_pool(db_url: &str, migrate: bool) -> Result<Pool<Sqlite>,
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
if migrate {
|
if migrate {
|
||||||
println!("Running database migrations");
|
info!("Running database migrations");
|
||||||
sqlx::migrate!("./migrations").run(&db).await?;
|
sqlx::migrate!("./migrations").run(&db).await?;
|
||||||
}
|
}
|
||||||
Ok(db)
|
Ok(db)
|
||||||
|
|||||||
21
src/app.rs
21
src/app.rs
@@ -1,6 +1,7 @@
|
|||||||
use axum::Router;
|
use axum::Router;
|
||||||
use axum::extract::Request;
|
use axum::extract::Request;
|
||||||
use axum::response::Response;
|
use axum::response::Response;
|
||||||
|
use reqwest::StatusCode;
|
||||||
use rustical_caldav::caldav_router;
|
use rustical_caldav::caldav_router;
|
||||||
use rustical_carddav::carddav_router;
|
use rustical_carddav::carddav_router;
|
||||||
use rustical_frontend::nextcloud_login::{NextcloudFlows, nextcloud_login_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::cookie::SameSite;
|
||||||
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
|
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
|
||||||
use tracing::Span;
|
use tracing::Span;
|
||||||
|
use tracing::field::display;
|
||||||
|
|
||||||
use crate::config::NextcloudLoginConfig;
|
use crate::config::NextcloudLoginConfig;
|
||||||
|
|
||||||
@@ -76,7 +78,7 @@ pub fn make_app<AS: AddressbookStore, CS: CalendarStore, S: SubscriptionStore>(
|
|||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http()
|
TraceLayer::new_for_http()
|
||||||
.make_span_with(|request: &Request| {
|
.make_span_with(|request: &Request| {
|
||||||
tracing::debug_span!(
|
tracing::info_span!(
|
||||||
"http-request",
|
"http-request",
|
||||||
status_code = tracing::field::Empty,
|
status_code = tracing::field::Empty,
|
||||||
otel.name = tracing::field::display(format!(
|
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| {
|
.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() {
|
if response.status().is_server_error() {
|
||||||
tracing::error!("status server error");
|
tracing::error!("server error");
|
||||||
} else if response.status().is_client_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(
|
.on_failure(
|
||||||
|
|||||||
11
src/main.rs
11
src/main.rs
@@ -22,6 +22,7 @@ use std::sync::Arc;
|
|||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
use tower::Layer;
|
use tower::Layer;
|
||||||
use tower_http::normalize_path::NormalizePathLayer;
|
use tower_http::normalize_path::NormalizePathLayer;
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod commands;
|
mod commands;
|
||||||
@@ -125,12 +126,10 @@ async fn main() -> Result<()> {
|
|||||||
NormalizePathLayer::trim_trailing_slash().layer(app),
|
NormalizePathLayer::trim_trailing_slash().layer(app),
|
||||||
);
|
);
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind(&format!(
|
let address = format!("{}:{}", config.http.host, config.http.port);
|
||||||
"{}:{}",
|
let listener = tokio::net::TcpListener::bind(&address).await?;
|
||||||
config.http.host, config.http.port
|
tasks.push(tokio::spawn(async move {
|
||||||
))
|
info!("RustiCal serving on http://{address}");
|
||||||
.await?;
|
|
||||||
tasks.push(tokio::spawn(async {
|
|
||||||
axum::serve(listener, app).await.unwrap()
|
axum::serve(listener, app).await.unwrap()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user