Use --verbose instead of RUST_LOG

This commit is contained in:
Joscha 2023-08-04 16:10:40 +02:00
parent de767121be
commit e76c94a57c
4 changed files with 20 additions and 75 deletions

View file

@ -22,14 +22,23 @@ struct Args {
db: PathBuf,
/// Path to the git repo.
repo: PathBuf,
/// Enable more verbose output
#[arg(long, short)]
verbose: bool,
}
fn set_up_logging() {
let filter = tracing_subscriber::filter::Builder::default()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();
tracing_subscriber::fmt().with_env_filter(filter).init();
fn set_up_logging(verbose: bool) {
if verbose {
tracing_subscriber::fmt()
.with_max_level(LevelFilter::DEBUG)
.init();
} else {
tracing_subscriber::fmt()
.with_max_level(LevelFilter::INFO)
.without_time()
.with_target(false)
.init();
}
}
#[derive(Template)]
@ -49,10 +58,9 @@ async fn index(State(db): State<SqlitePool>) -> Result<Response, Response> {
}
async fn run() -> anyhow::Result<()> {
// Parse args before any logging starts
let args = Args::parse();
set_up_logging();
set_up_logging(args.verbose);
info!("You are running {NAME} {VERSION}");
let state = AppState::new(&args.db).await?;

View file

@ -7,6 +7,7 @@ use sqlx::{
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
SqlitePool,
};
use tracing::{info, debug};
// TODO Occasionally run PRAGMA optimize
async fn pool(db_path: &Path) -> sqlx::Result<SqlitePool> {
@ -25,8 +26,10 @@ async fn pool(db_path: &Path) -> sqlx::Result<SqlitePool> {
.create_if_missing(true)
.optimize_on_close(true, None);
info!("Opening db at {}", db_path.display());
let pool = SqlitePoolOptions::new().connect_with(options).await?;
debug!("Applying outstanding db migrations");
sqlx::migrate!().run(&pool).await?;
Ok(pool)