Skip to content

Commit

Permalink
Merge pull request #126 from xsnippet/tracing
Browse files Browse the repository at this point in the history
Add basic logging
  • Loading branch information
malor committed Mar 20, 2021
2 parents 83be571 + 0e45734 commit d21f967
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 22 deletions.
180 changes: 168 additions & 12 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ rocket = "0.4.5"
rocket_contrib = {version = "0.4.5", features = ["json"]}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tracing = "0.1.25"
tracing-subscriber = "0.2.16"
uuid = { version = "0.8.2", features = ["v4"] }
3 changes: 2 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;

use super::routes;
use super::storage::{SqlStorage, Storage};
use super::web::RequestIdHeader;
use super::web::{RequestIdHeader, RequestSpan};

#[derive(Debug)]
pub struct Config {
Expand Down Expand Up @@ -51,5 +51,6 @@ pub fn create_app() -> Result<rocket::Rocket, Box<dyn Error>> {
.manage(Config { syntaxes })
.manage(storage)
.attach(RequestIdHeader)
.attach(RequestSpan)
.mount("/v1", routes))
}
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ impl<'r> Responder<'r> for ApiError {
fn respond_to(self, request: &Request) -> response::Result<'r> {
if let ApiError::InternalError(_) = self {
// do not give away any details for internal server errors
// TODO: integrate with Rocket contextual logging when 0.5 is released
eprintln!("Internal server error: {}", self.reason());
error!("Internal server error: {}", self.reason());
Response::build()
.status(http::Status::InternalServerError)
.ok()
} else {
// otherwise, present the error in the requested data format
let http_status = self.status();
debug!("ApiError: {:?}", self);
Response::build_from(Output(self).respond_to(request)?)
.status(http_status)
.ok()
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,34 @@
extern crate diesel;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate tracing;

mod application;
mod errors;
mod routes;
mod storage;
mod web;

use tracing_subscriber::{fmt, EnvFilter};

fn main() {
// set up logging of application events to stderr
let tracing_config = rocket::config::RocketConfig::active_default()
.expect("failed to read Rocket config")
.active()
.get_string("tracing")
.unwrap_or_else(|_| String::from("info"));
let subscriber = fmt()
.with_env_filter(EnvFilter::new(tracing_config))
.with_writer(std::io::stderr)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

let app = match application::create_app() {
Ok(app) => app,
Err(err) => {
eprintln!("error: {}", err);
error!("error: {}", err);
std::process::exit(1);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/storage/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ mod tests {

test_function(Box::new(SqlStorage { pool }));
} else {
eprintln!("ROCKET_DATABASE_URL is not set, skipping the test");
error!("ROCKET_DATABASE_URL is not set, skipping the test");
}
}

Expand Down

0 comments on commit d21f967

Please sign in to comment.