Skip to content

Commit

Permalink
chore: Convert logging to the tracing crate
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jun 8, 2022
1 parent e193558 commit 5dab424
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 73 deletions.
140 changes: 83 additions & 57 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ serde_json = "1.0.81"
pact_matching = "0.12.8"
pact_verifier = "0.13.7"
pact_models = "0.4.1"
simplelog = "0.12.0"
log = "0.4.17"
maplit = "1.0.2"
itertools = "0.10.3"
hyper = { version = "0.14.19", features = ["full"] }
Expand All @@ -33,8 +31,12 @@ regex = "1.5.6"
reqwest = { version = "0.11.10", default-features = false, features = ["json", "rustls-tls-native-roots"] }
tower-service = "0.3.1"
anyhow = "1.0.57"
tracing = "0.1.34"
tracing-core = "0.1.26"
tracing-subscriber = "0.3.11"

[dev-dependencies]
quickcheck = "1.0.3"
expectest = "0.12.0"
rand = "0.8.5"
pretty_assertions = "1.2.1"
22 changes: 13 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ use base64::encode;
use clap::{App, AppSettings, Arg, ArgMatches, ErrorKind};
use clap::crate_version;
use futures::stream::*;
use log::*;
use log::LevelFilter;
use maplit::*;
use pact_models::pact::{load_pact_from_json, read_pact};
use pact_models::prelude::*;
use pact_verifier::pact_broker::HALClient;
use regex::Regex;
use serde_json::Value;
use simplelog::{ColorChoice, Config, SimpleLogger, TerminalMode, TermLogger};
use tracing::{debug, error, warn};
use tracing_core::LevelFilter;
use tracing_subscriber::FmtSubscriber;

use crate::server::ServerHandler;

Expand Down Expand Up @@ -148,7 +148,7 @@ async fn main() -> Result<(), ExitCode> {

fn print_version() {
println!("\npact stub server version : v{}", crate_version!());
println!("pact specification version: v{}", PactSpecification::V3.version_str());
println!("pact specification version: v{}", PactSpecification::V4.version_str());
}

fn integer_value(v: String) -> Result<(), String> {
Expand Down Expand Up @@ -497,12 +497,16 @@ async fn handle_command_args() -> Result<(), u8> {

fn setup_logger(level: &str) {
let log_level = match level {
"none" => LevelFilter::Off,
_ => LevelFilter::from_str(level).unwrap()
"none" => LevelFilter::OFF,
_ => LevelFilter::from_str(level).unwrap_or(LevelFilter::INFO)
};
let subscriber = FmtSubscriber::builder()
.with_max_level(log_level)
.with_thread_names(true)
.finish();
if let Err(err) = tracing::subscriber::set_global_default(subscriber) {
eprintln!("ERROR: Failed to initialise global tracing subscriber - {err}");
};
if TermLogger::init(log_level, Config::default(), TerminalMode::Mixed, ColorChoice::Auto).is_err() {
SimpleLogger::init(log_level, Config::default()).unwrap_or_default();
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/pact_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use http::header::{ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE};
use http::header::HeaderValue;
use http::request::Parts;
use hyper::{Body, Response as HyperResponse};
use log::*;
use pact_models::content_types::TEXT;
use pact_models::http_parts::HttpPart;
use pact_models::prelude::*;
use pact_models::query_strings::parse_query_string;
use pact_models::v4::http_parts::{HttpRequest, HttpResponse};
use tracing::{debug, info, warn};

fn extract_query_string(uri: &Uri) -> Option<HashMap<String, Vec<String>>> {
match uri.query() {
Expand Down
12 changes: 8 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use futures::task::{Context, Poll};
use http::{Error, StatusCode};
use hyper::{Body, Request as HyperRequest, Response as HyperResponse, Server};
use itertools::Itertools;
use log::*;
use maplit::*;
use pact_matching::{CoreMatchingContext, DiffConfig, Mismatch};
use pact_models::generators::GeneratorTestMode;
Expand All @@ -18,6 +17,7 @@ use pact_models::v4::http_parts::{HttpRequest, HttpResponse};
use pact_models::v4::V4InteractionType;
use regex::Regex;
use tower_service::Service;
use tracing::{debug, error, info, warn};

use crate::pact_support;

Expand Down Expand Up @@ -397,16 +397,20 @@ mod test {
response: HttpResponse { status: 201, .. HttpResponse::default() },
.. SynchronousHttp::default() };

let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
let pact1 = V4Pact {
interactions: vec![ interaction1.boxed_v4() ],
.. V4Pact::default()
};
let pact2 = V4Pact {
interactions: vec![ interaction2.boxed_v4() ],
.. V4Pact::default()
};

let request1 = HttpRequest {
body: OptionalBody::Present("{\"a\": 1, \"b\": 4, \"c\": 6}".as_bytes().into(), None, None),
.. HttpRequest::default() };

expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
expect!(super::find_matching_request(&request1, false, false, vec![pact1, pact2], None, false).await)
.to(be_ok().value(interaction2.response));
}

Expand Down

0 comments on commit 5dab424

Please sign in to comment.