✨ Adds logging
This commit is contained in:
25
src/main.rs
25
src/main.rs
@@ -6,8 +6,10 @@ mod schema;
|
||||
use crate::api::hit;
|
||||
use crate::api::image;
|
||||
use crate::api::tracker;
|
||||
use chrono::Local;
|
||||
use diesel::{Connection, PgConnection};
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
@@ -17,12 +19,33 @@ fn index() -> &'static str {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
// TODO: add logging
|
||||
// TODO: add auth
|
||||
|
||||
fn setup_logging() -> Result<(), fern::InitError> {
|
||||
let level_raw = env::var("LOG_LEVEL").unwrap_or("INFO".to_string());
|
||||
let level = log::LevelFilter::from_str(&level_raw).expect("LOG_LEVEL invalid");
|
||||
|
||||
fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"{} {} [{}] {}",
|
||||
Local::now().format("%Y-%m-%d %H:%M:%S"),
|
||||
record.level(),
|
||||
record.target(),
|
||||
message
|
||||
))
|
||||
})
|
||||
.level(level)
|
||||
.chain(std::io::stdout())
|
||||
.apply()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
dotenv::dotenv().ok();
|
||||
setup_logging().expect("Failed to setup logging");
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
let image_path = env::var("IMAGE_PATH").expect("IMAGE_PATH must be set");
|
||||
|
||||
Reference in New Issue
Block a user