Use tracing library

This commit is contained in:
Greg Shuflin 2025-02-04 23:57:02 -08:00
parent 69aa7393fe
commit ab5e187212
8 changed files with 49 additions and 17 deletions

2
Cargo.lock generated
View File

@ -2677,6 +2677,8 @@ dependencies = [
"sqlx",
"time",
"tokio",
"tracing",
"tracing-subscriber",
"url",
"uuid",
]

View File

@ -22,3 +22,5 @@ tokio = "1.43.0"
time = "0.3.37"
base64 = "0.21"
getrandom = "0.2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

View File

@ -1,5 +1,6 @@
use crate::feeds::Feed;
use crate::user::User;
use tracing::info;
struct DemoFeed {
name: &'static str,
@ -75,5 +76,5 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
.expect("Failed to create demo feed");
}
println!("Successfully set up demo data");
info!("Successfully set up demo data");
}

View File

@ -1,29 +1,31 @@
use feed_rs;
use url::Url;
use tracing::{error, info};
#[derive(Debug)]
pub struct FeedError;
pub async fn fetch_feed(url: &Url) -> Result<feed_rs::model::Feed, FeedError> {
println!("Making a request to fetch feed `{url}`");
info!("Making a request to fetch feed `{url}`");
// Fetch the feed content
let response = reqwest::get(url.as_ref()).await.map_err(|e| {
eprintln!("Failed to fetch feed: {}", e);
error!("Failed to fetch feed: {}", e);
FeedError
})?;
let content = response.text().await.map_err(|e| {
eprintln!("Failed to read response body: {}", e);
error!("Failed to read response body: {}", e);
FeedError
})?;
// Parse the feed
let feed_data = feed_rs::parser::parse(content.as_bytes()).map_err(|e| {
eprintln!("Failed to parse feed content: {}", e);
error!("Failed to parse feed content: {}", e);
FeedError
})?;
println!("Fetched feed: {}", url.as_ref());
info!("Fetched feed: {}", url.as_ref());
Ok(feed_data)
}

View File

@ -3,6 +3,7 @@ use rocket::serde::{self, json::Json, Deserialize, Serialize};
use rocket_db_pools::Connection;
use sqlx::types::JsonValue;
use sqlx::Executor;
use tracing::error;
use url::Url;
use uuid::Uuid;
@ -42,7 +43,7 @@ impl Feed {
{
// Convert categorization to JSON value
let categorization_json = serde::json::to_value(&self.categorization).map_err(|e| {
eprintln!("Failed to serialize categorization: {}", e);
error!("Failed to serialize categorization: {}", e);
sqlx::Error::Decode(Box::new(e))
})?;

View File

@ -2,6 +2,8 @@
extern crate rocket;
use clap::Parser;
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
mod demo;
mod feed_utils;
@ -89,8 +91,24 @@ async fn setup_database(demo: bool, rocket: Rocket<Build>) -> fairing::Result {
#[launch]
fn rocket() -> _ {
// Initialize the tracing subscriber
FmtSubscriber::builder()
.with_max_level(Level::INFO)
.with_target(false)
.with_thread_ids(true)
.with_file(true)
.with_line_number(true)
.with_thread_names(true)
.pretty()
.init();
let args = Args::parse();
info!("Starting RSS Reader");
if args.demo {
info!("Running in demo mode");
}
let db_url = if args.demo {
// Use a temporary file for demo mode instead of in-memory database
let temp_db = "/tmp/rss-reader-temp-db.sqlite";
@ -100,6 +118,7 @@ fn rocket() -> _ {
}
// Create the new database file
std::fs::File::create(temp_db).expect("Failed to create temporary database file");
info!("Created temporary database at {}", temp_db);
format!("sqlite:{}", temp_db)
} else {
let database = args
@ -109,6 +128,7 @@ fn rocket() -> _ {
if !std::path::Path::new(&database).exists() {
use std::fs::File;
File::create(&database).expect("Failed to create database file");
info!("Created new database at {}", database);
}
format!("sqlite:{}", database)
};

View File

@ -7,6 +7,7 @@ use rocket::serde::uuid::Uuid;
use rocket::serde::{self, json::Json, Serialize};
use rocket_db_pools::Connection;
use sqlx::{Acquire, SqliteConnection};
use tracing::{error, info};
use url::Url;
const POLLING_INTERVAL: Duration = Duration::minutes(20);
@ -39,7 +40,7 @@ async fn update_entry_db(
) -> Result<(), Status> {
// Start a transaction for batch update
let mut tx = db.begin().await.map_err(|e| {
eprintln!("Failed to start transaction: {}", e);
error!("Failed to start transaction: {}", e);
Status::InternalServerError
})?;
@ -52,7 +53,7 @@ async fn update_entry_db(
.execute(&mut *tx)
.await
.map_err(|e| {
eprintln!("Failed to update feed last_checked_time: {}", e);
error!("Failed to update feed last_checked_time: {}", e);
Status::InternalServerError
})?;
@ -95,9 +96,9 @@ async fn update_entry_db(
.await;
if let Err(e) = result {
eprintln!("Failed to save feed entry: {}", e);
error!("Failed to save feed entry: {}", e);
tx.rollback().await.map_err(|e| {
eprintln!("Failed to rollback transaction: {}", e);
error!("Failed to rollback transaction: {}", e);
Status::InternalServerError
})?;
return Err(Status::InternalServerError);
@ -106,7 +107,7 @@ async fn update_entry_db(
// Commit the transaction
tx.commit().await.map_err(|e| {
eprintln!("Failed to commit transaction: {}", e);
error!("Failed to commit transaction: {}", e);
Status::InternalServerError
})?;
@ -136,7 +137,7 @@ async fn read_entries(feed_id: &str, db: &mut SqliteConnection) -> Result<Vec<En
.fetch_all(db)
.await
.map_err(|e| {
eprintln!("Failed to read feed entries: {}", e);
error!("Failed to read feed entries: {}", e);
Status::InternalServerError
})?;
@ -214,17 +215,19 @@ pub async fn poll_feed(
let now = Utc::now();
let last_checked = now - feed.last_checked_time;
println!("Feed last checked: {}", last_checked);
info!("Feed {} last checked: {}", feed_id, last_checked);
let entries = if last_checked < POLLING_INTERVAL {
println!("reading entries out of db");
info!("Reading entries from database for feed {}", feed_id);
read_entries(&feed_id, &mut db).await?
} else {
info!("Fetching new entries for feed {}", feed_id);
let entries = fetch_new_entries(&url).await?;
update_entry_db(&entries, &feed_id, &mut db).await?;
entries
};
let count = entries.len();
info!("Returning {} entries for feed {}", count, feed_id);
Ok(Json(FeedPollResponse { count, entries }))
}

View File

@ -5,6 +5,7 @@ use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket::State;
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};
use tracing::error;
use uuid::Uuid;
use crate::session_store::SessionStore;
@ -114,7 +115,7 @@ pub async fn create_user(
match user.write_to_database(&mut **db).await {
Ok(_) => Ok(Json(user)),
Err(e) => {
eprintln!("Database error: {}", e);
error!("Database error: {}", e);
match e {
sqlx::Error::Database(db_err) if db_err.is_unique_violation() => {
Err(Status::Conflict)
@ -360,7 +361,7 @@ pub async fn setup(
match user.write_to_database(&mut **db).await {
Ok(_) => Ok(Status::Created),
Err(e) => {
eprintln!("Database error: {}", e);
error!("Database error: {}", e);
match e {
sqlx::Error::Database(db_err) if db_err.is_unique_violation() => {
Err(Json(SetupError {