Skip to content

Commit

Permalink
proper logging (#37)
Browse files Browse the repository at this point in the history
* add env logger
  • Loading branch information
Lukas-Heiligenbrunner committed Aug 3, 2024
1 parent 7d2df5e commit 276aba3
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Environment Variables
| DB_HOST | String | POSTGRES Host (ignored if sqlite) | null |
| VERSION_CHECK_INTERVAL | Integer | Interval in seconds for checking package versions | 3600 |
| BUILD_ARTIFACT_DIR | String | pkg share directory between aurcache container and build containers | null |
| LOG_LEVEL | String | Log level | INFO |

## Build Info

Expand All @@ -123,7 +124,6 @@ cargo build --release
## Things still missing

* proper error return from api
* proper logging
* auto update packages
* implement repo-add in rust
* keep older pkg versions in repo (repo-add limitation)
Expand Down
31 changes: 31 additions & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ rust-embed = "8.4.0"
rocket_seek_stream = "0.2.6"
shiplift = "0.7.0"
bigdecimal = "0.4.5"
env_logger = "0.11.5"
log = "0.4.22"

[[bin]]
name = "aurcache"
Expand Down
11 changes: 7 additions & 4 deletions backend/src/api/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::api::cusom_file_server::CustomFileServer;
#[cfg(feature = "static")]
use crate::api::embed::CustomHandler;
use crate::builder::types::Action;
use log::{error, info};
use rocket::Config;
use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig};
use sea_orm::DatabaseConnection;
Expand All @@ -14,6 +15,7 @@ pub fn init_api(db: DatabaseConnection, tx: Sender<Action>) -> JoinHandle<()> {
let config = Config {
address: "0.0.0.0".parse().unwrap(),
port: 8081,
//log_level: LogLevel::Off,
..Default::default()
};

Expand All @@ -33,8 +35,8 @@ pub fn init_api(db: DatabaseConnection, tx: Sender<Action>) -> JoinHandle<()> {

let rock = rock.launch().await;
match rock {
Ok(_) => println!("Rocket shut down gracefully."),
Err(err) => println!("Rocket had an error: {}", err),
Ok(_) => info!("Rocket shut down gracefully."),
Err(err) => error!("Rocket had an error: {}", err),
};
})
}
Expand All @@ -44,6 +46,7 @@ pub fn init_repo() -> JoinHandle<()> {
let config = Config {
address: "0.0.0.0".parse().unwrap(),
port: 8080,
//log_level: LogLevel::Off,
..Default::default()
};

Expand All @@ -52,8 +55,8 @@ pub fn init_repo() -> JoinHandle<()> {
.launch()
.await;
match launch_result {
Ok(_) => println!("Rocket shut down gracefully."),
Err(err) => println!("Rocket had an error: {}", err),
Ok(_) => info!("Rocket shut down gracefully."),
Err(err) => error!("Rocket had an error: {}", err),
};
})
}
3 changes: 2 additions & 1 deletion backend/src/builder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::db::prelude::{Builds, Files, PackagesFiles};
use crate::db::{builds, files, packages, packages_files};
use crate::repo::utils::{repo_add, try_remove_archive_file};
use anyhow::anyhow;
use log::info;
use rocket::futures::StreamExt;
use sea_orm::{
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, ModelTrait, QueryFilter,
Expand Down Expand Up @@ -316,7 +317,7 @@ async fn move_and_add_pkgs(
package_file.save(&txn).await?;

repo_add(archive_name.clone())?;
println!("Successfully added '{}' to the repo archive", archive_name);
info!("Successfully added '{}' to the repo archive", archive_name);
}
txn.commit().await?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions backend/src/builder/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::db::builds;
use crate::db::prelude::Builds;
use anyhow::anyhow;
use log::debug;
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set, TransactionTrait};
use std::ops::Add;

Expand All @@ -27,8 +28,7 @@ impl BuildLogger {
text = text.add("\n");
}

// todo replace this with debug log
print!("{}", text);
debug!("{}", text);

match build.output.unwrap() {
None => {
Expand Down
6 changes: 3 additions & 3 deletions backend/src/db/init.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::db::helpers::dbtype::{database_type, DbType};
use crate::db::migration::Migrator;
use anyhow::anyhow;
use sea_orm::{ConnectionTrait, ConnectOptions, Database, DatabaseConnection};
use rocket::log::private::LevelFilter;
use sea_orm::{ConnectOptions, ConnectionTrait, Database, DatabaseConnection};
use sea_orm_migration::MigratorTrait;
use std::{env, fs};
use rocket::log::private::LevelFilter;

pub async fn init_db() -> anyhow::Result<DatabaseConnection> {
let db: DatabaseConnection = match database_type() {
Expand All @@ -17,7 +17,7 @@ pub async fn init_db() -> anyhow::Result<DatabaseConnection> {
let mut conn_opts = ConnectOptions::new("sqlite://db/db.sqlite?mode=rwc");
conn_opts
.max_connections(100)
.sqlx_logging_level(LevelFilter::Info);
.sqlx_logging_level(LevelFilter::Trace);
let db = Database::connect(conn_opts).await?;
db.execute_unprepared("
PRAGMA journal_mode = WAL; -- better write-concurrency
Expand Down
22 changes: 18 additions & 4 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ use crate::builder::types::Action;
use crate::db::init::init_db;
use crate::repo::init::init_repo_files;
use crate::scheduler::aur_version_update::start_aur_version_checking;
use crate::utils::logger::init_logger;
use log::{info, warn};
use tokio::sync::broadcast;

static START_BANNER: &str = r"
_ _ _____ _____ _
/\ | | | | __ \ / ____| | |
/ \ | | | | |__) | | __ _ ___| |__ ___
/ /\ \| | | | _ /| | / _` |/ __| '_ \ / _ \
/ ____ \ |__| | | \ \| |___| (_| | (__| | | | __/
/_/ \_\____/|_| \_\\_____\__,_|\___|_| |_|\___|
";

#[tokio::main]
async fn main() {
init_logger();
info!("{}", START_BANNER);

let (tx, _) = broadcast::channel::<Action>(32);
let db = init_db()
.await
Expand All @@ -32,16 +46,16 @@ async fn main() {

tokio::select! {
_ = version_check_handle => {
println!("Version check handle exited");
warn!("Version check handle exited");
}
_ = build_queue_handle => {
println!("Build queue handle exited");
warn!("Build queue handle exited");
}
_ = repo_handle => {
println!("Repo web server handle exited");
warn!("Repo web server handle exited");
}
_ = api_handle => {
println!("API web server handle exited");
warn!("API web server handle exited");
}
}
}
7 changes: 4 additions & 3 deletions backend/src/repo/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::db::prelude::PackagesFiles;
use crate::db::{files, packages_files};
use anyhow::anyhow;
use log::info;
use sea_orm::ColumnTrait;
use sea_orm::QueryFilter;
use sea_orm::{DatabaseTransaction, EntityTrait, ModelTrait};
Expand All @@ -25,7 +26,7 @@ pub fn repo_add(pkg_file_name: String) -> anyhow::Result<()> {
));
}

println!("{db_file} updated successfully");
info!("{db_file} updated successfully");
Ok(())
}

Expand All @@ -45,7 +46,7 @@ pub fn repo_remove(pkg_file_name: String) -> anyhow::Result<()> {
));
}

println!("{db_file} updated successfully");
info!("{db_file} updated successfully");
Ok(())
}

Expand All @@ -63,7 +64,7 @@ pub async fn try_remove_archive_file(
let _ = repo_remove(filename.clone());
fs::remove_file(format!("./repo/{}", filename))?;

println!("Removed old file: {}", filename);
info!("Removed old file: {}", filename);
}

Ok(())
Expand Down
9 changes: 5 additions & 4 deletions backend/src/scheduler/aur_version_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::db::packages;
use crate::db::prelude::Packages;
use anyhow::anyhow;
use aur_rs::{Package, Request};
use log::{error, info, warn};
use sea_orm::ActiveValue::Set;
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait};
use std::env;
Expand All @@ -18,11 +19,11 @@ pub fn start_aur_version_checking(db: DatabaseConnection) -> JoinHandle<()> {
tokio::spawn(async move {
sleep(Duration::from_secs(10)).await;
loop {
println!("performing aur version checks");
info!("performing aur version checks");
match aur_check_versions(db.clone()).await {
Ok(_) => {}
Err(e) => {
println!("Failed to perform aur version check: {e}")
error!("Failed to perform aur version check: {e}")
}
}
sleep(Duration::from_secs(check_interval)).await;
Expand All @@ -42,13 +43,13 @@ async fn aur_check_versions(db: DatabaseConnection) -> anyhow::Result<()> {
.results;

if results.len() != packages.len() {
println!("Package nr in repo and aur api response has different size");
warn!("Package nr in repo and aur api response has different size");
}

for package in packages {
match results.iter().find(|x1| x1.name == package.name) {
None => {
println!("Couldn't find {} in AUR response", package.name)
warn!("Couldn't find {} in AUR response", package.name)
}
Some(result) => {
let mut package: packages::ActiveModel = package.into();
Expand Down
21 changes: 21 additions & 0 deletions backend/src/utils/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use env_logger::Env;
use log::LevelFilter;
use std::env;
use std::str::FromStr;

pub fn init_logger() {
let env_name = "LOG_LEVEL";
let env = Env::default()
.filter_or(env_name, "info")
.write_style_or("LOG_STYLE", "always");

env_logger::builder()
.parse_env(env)
// increase default rocket logging to warn
.filter_module(
"rocket",
LevelFilter::from_str(env::var(env_name).unwrap_or("warn".to_string()).as_str())
.unwrap_or(LevelFilter::Warn),
)
.init();
}
1 change: 1 addition & 0 deletions backend/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod dir_size;
pub mod logger;

0 comments on commit 276aba3

Please sign in to comment.