Skip to content

Commit

Permalink
made sure that logging in dev is pleasantly debuggable
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed May 5, 2024
1 parent d2d2bdb commit 336bd57
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 8 deletions.
97 changes: 97 additions & 0 deletions server/Cargo.lock

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

3 changes: 2 additions & 1 deletion server/main-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ path = "src/main.rs"
[dependencies]
# logging/obeservability
log = "0.4.21"
structured-logger = "1.0.3"
actix-web-prom = { version = "0.8.0", default-features = false, features = [] }
env_logger = "0.11.3" # used in dev
structured-logger = "1.0.3" # used in prod

#serialisation
serde = { version = "1.0.200", features = ["derive"] }
Expand Down
26 changes: 19 additions & 7 deletions server/main-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ use std::collections::HashMap;
use std::error::Error;

use actix_cors::Cors;
use actix_web::{get, middleware, web, App, HttpResponse, HttpServer};
use actix_web::{App, get, HttpResponse, HttpServer, middleware, web};
use actix_web_prom::PrometheusMetricsBuilder;
use log::{debug, error, info};
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
use sqlx::prelude::*;
use sqlx::PgPool;
use structured_logger::async_json::new_writer;
use structured_logger::Builder;

mod calendar;
mod details;
Expand Down Expand Up @@ -56,11 +54,25 @@ fn connection_string() -> String {
format!("postgres://{username}:{password}@{url}/{db}")
}

fn setup_logging() {
#[cfg(debug_assertions)]
{
let env = env_logger::Env::default()
.default_filter_or("trace");
env_logger::Builder::from_env(env).init();
}
#[cfg(not(debug_assertions))]
structured_logger::Builder::with_level("info")
.with_target_writer(
"*",
structured_logger::async_json::new_writer(tokio::io::stdout()),
)
.init();
}

#[tokio::main]
async fn main() -> Result<(), BoxedError> {
Builder::with_level("info")
.with_target_writer("*", new_writer(tokio::io::stdout()))
.init();
setup_logging();
let uri = connection_string();
let pool = PgPoolOptions::new().connect(&uri).await?;
#[cfg(not(feature = "skip_db_setup"))]
Expand Down

0 comments on commit 336bd57

Please sign in to comment.