depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit 92fcf0505bc718f4a3a2225c64f4161d0dd5745c
parent 05720fcf2dd2829009adf0f145f1341d6e8260a2
Author: Antoine A <>
Date:   Wed,  8 Dec 2021 18:17:17 +0100

Add proper logging

Diffstat:
MCargo.lock | 47+++++++++++++++++++++++++++++++++++++++++++++++
MCargo.toml | 2+-
Ataler-log/Cargo.toml | 9+++++++++
Ataler-log/src/lib.rs | 30++++++++++++++++++++++++++++++
Mwire-gateway/Cargo.toml | 3+++
Mwire-gateway/src/main.rs | 29++++++++++++++++++++++++-----
6 files changed, 114 insertions(+), 6 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -473,6 +473,20 @@ dependencies = [ ] [[package]] +name = "flexi_logger" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27ea44b8d87613dd188e316a6120dfe879f0c01489f55927b30ca068a67f4ae5" +dependencies = [ + "glob", + "lazy_static", + "log", + "rustversion", + "thiserror", + "time", +] + +[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -599,6 +613,12 @@ dependencies = [ ] [[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + +[[package]] name = "half" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1378,6 +1398,15 @@ dependencies = [ ] [[package]] +name = "taler-log" +version = "0.1.0" +dependencies = [ + "flexi_logger", + "log", + "time", +] + +[[package]] name = "textwrap" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1407,6 +1436,23 @@ dependencies = [ ] [[package]] +name = "time" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41effe7cfa8af36f439fac33861b66b049edc6f9a32331e2312660529c1c24ad" +dependencies = [ + "itoa", + "libc", + "time-macros", +] + +[[package]] +name = "time-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25eb0ca3468fc0acc11828786797f6ef9aa1555e4a211a60d64cc8e4d1be47d6" + +[[package]] name = "tinytemplate" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1734,6 +1780,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "serde_with", + "taler-log", "thiserror", "tokio", "tokio-postgres", diff --git a/Cargo.toml b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["wire-gateway", "btc-wire", "uri-pack"] +members = ["wire-gateway", "btc-wire", "uri-pack", "taler-log"] diff --git a/taler-log/Cargo.toml b/taler-log/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "taler-log" +version = "0.1.0" +edition = "2021" + +[dependencies] +log = "0.4.14" +flexi_logger = { version = "0.20.1", default-features = false } +time = { version = "0.3.5", features = ["formatting", "macros"] } diff --git a/taler-log/src/lib.rs b/taler-log/src/lib.rs @@ -0,0 +1,30 @@ +use flexi_logger::{DeferredNow, LogSpecification, Record}; +pub use log; +use time::{format_description::FormatItem, macros::format_description}; + +const TIME_FORMAT: &[FormatItem<'static>] = format_description!( + "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:6][offset_hour sign:mandatory][offset_minute]" +); + +fn custom_format( + w: &mut dyn std::io::Write, + now: &mut DeferredNow, + record: &Record, +) -> Result<(), std::io::Error> { + write!( + w, + "{} {} {} {}", + now.format(TIME_FORMAT), + record.module_path().unwrap_or("<unnamed>"), + record.level(), + &record.args() + ) +} + +pub fn init() { + flexi_logger::Logger::with(LogSpecification::info()) + .log_to_stderr() + .format(custom_format) + .start() + .unwrap(); +} diff --git a/wire-gateway/Cargo.toml b/wire-gateway/Cargo.toml @@ -38,3 +38,5 @@ rand = { version = "0.8.4", features = ["getrandom"] } url = { version = "2.2.2", features = ["serde"] } # Async postgres client tokio-postgres = { version = "0.7.5" } +# Logging +taler-log = {path = "../taler-log"} +\ No newline at end of file diff --git a/wire-gateway/src/main.rs b/wire-gateway/src/main.rs @@ -1,4 +1,4 @@ -use std::{process::exit, str::FromStr}; +use std::{process::exit, str::FromStr, time::Instant}; use api_common::{Amount, SafeUint64, ShortHashCode, Timestamp}; use api_wire::{OutgoingBankTransaction, OutgoingHistory}; @@ -10,7 +10,7 @@ use hyper::{ service::{make_service_fn, service_fn}, Body, Error, Method, Response, Server, StatusCode, }; - +use taler_log::log::{error, info, log, Level}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio_postgres::{Client, NoTls}; use url::Url; @@ -41,11 +41,13 @@ const DB_URL: &str = "postgres://localhost?user=postgres&password=password"; #[tokio::main] async fn main() { + taler_log::init(); + let (client, connection) = tokio_postgres::connect(DB_URL, NoTls).await.unwrap(); tokio::spawn(async move { if let Err(e) = connection.await { - eprintln!("wire-gateway: DB {}", e); + error!("DB: {}", e); exit(1); } }); @@ -54,6 +56,7 @@ async fn main() { let addr = ([0, 0, 0, 0], 8080).into(); let make_service = make_service_fn(move |_| async move { Ok::<_, Error>(service_fn(move |req| async move { + let start = Instant::now(); let (parts, body) = req.into_parts(); let response = match router(&parts, body, state).await { Ok(resp) => resp, @@ -82,16 +85,32 @@ async fn main() { .unwrap(), ), }; + let status = response.status().as_u16(); + let level = if status >= 500 { + Level::Error + } else if status >= 400 { + Level::Warn + } else { + Level::Info + }; + log!( + level, + "{} {} {:>2}ms {}", + parts.method, + status, + start.elapsed().as_millis(), + parts.uri.path() + ); Ok::<Response<Body>, Error>(response) })) }); let server = Server::bind(&addr).serve(make_service); - println!("Server listening on http://{}", addr); + info!("Server listening on http://{}", addr); if let Err(e) = server.await { - eprintln!("server error: {}", e); + error!("server: {}", e); } }