nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

commit 6633f8b47254b9da16023d2761e36ce391e3a268
parent 93dfed0a87cb02d8a75a8fe4e4f27e4f1bab6324
Author: Greg Heartsfield <scsibug@imap.cc>
Date:   Wed, 28 Sep 2022 07:19:59 -0500

feat: replace logging with tracing

Diffstat:
MCargo.lock | 145++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
MCargo.toml | 6+++---
MDockerfile | 2+-
Msrc/config.rs | 2+-
Msrc/conn.rs | 2+-
Msrc/db.rs | 2+-
Msrc/event.rs | 2+-
Msrc/main.rs | 8++++----
Msrc/nip05.rs | 2+-
Msrc/schema.rs | 2+-
Msrc/server.rs | 2+-
Mtests/common/mod.rs | 7++++---
12 files changed, 118 insertions(+), 64 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -35,6 +35,24 @@ dependencies = [ ] [[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] name = "anyhow" version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -73,17 +91,6 @@ dependencies = [ ] [[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - -[[package]] name = "autocfg" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -204,6 +211,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] +name = "chrono" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" +dependencies = [ + "iana-time-zone", + "num-integer", + "num-traits", + "winapi", +] + +[[package]] name = "cloudabi" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -264,7 +283,7 @@ dependencies = [ "tonic", "tracing", "tracing-core", - "tracing-subscriber", + "tracing-subscriber 0.3.15", ] [[package]] @@ -370,19 +389,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" [[package]] -name = "env_logger" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - -[[package]] name = "fallible-iterator" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -758,6 +764,19 @@ dependencies = [ ] [[package]] +name = "iana-time-zone" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd911b35d940d2bd0bea0f9100068e5b97b51a1cbe13d13382f132e0365257a0" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "js-sys", + "wasm-bindgen", + "winapi", +] + +[[package]] name = "idna" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -880,6 +899,15 @@ dependencies = [ [[package]] name = "matchers" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchers" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" @@ -980,7 +1008,6 @@ dependencies = [ "bitcoin_hashes", "config", "console-subscriber", - "env_logger", "futures", "futures-util", "governor", @@ -989,7 +1016,6 @@ dependencies = [ "hyper", "hyper-tls", "lazy_static", - "log", "nonzero_ext", "parse_duration", "r2d2", @@ -1002,6 +1028,8 @@ dependencies = [ "thiserror", "tokio", "tokio-tungstenite", + "tracing", + "tracing-subscriber 0.2.25", "tungstenite", "uuid", ] @@ -1803,15 +1831,6 @@ dependencies = [ ] [[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - -[[package]] name = "thiserror" version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2082,12 +2101,55 @@ dependencies = [ ] [[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-serde" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" +dependencies = [ + "serde", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" +dependencies = [ + "ansi_term", + "chrono", + "lazy_static", + "matchers 0.0.1", + "regex", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", + "tracing-serde", +] + +[[package]] name = "tracing-subscriber" version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b" dependencies = [ - "matchers", + "matchers 0.1.0", "once_cell", "regex", "sharded-slab", @@ -2301,15 +2363,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -4,9 +4,9 @@ version = "0.6.2" edition = "2021" [dependencies] -log = "0.4" -env_logger = "0.9" -tokio = { version = "1", features = ["full", "tracing"] } +tracing = "0.1.36" +tracing-subscriber = "0.2.0" +tokio = { version = "1", features = ["full", "tracing", "signal"] } console-subscriber = "0.1.8" futures = "0.3" futures-util = "0.3" diff --git a/Dockerfile b/Dockerfile @@ -36,7 +36,7 @@ RUN chown -R $APP_USER:$APP_USER ${APP} USER $APP_USER WORKDIR ${APP} -ENV RUST_LOG=info +ENV RUST_LOG=info,nostr_rs_relay=info ENV APP_DATA=${APP_DATA} CMD ./nostr-rs-relay --db ${APP_DATA} diff --git a/src/config.rs b/src/config.rs @@ -1,8 +1,8 @@ //! Configuration file and settings management use config::{Config, ConfigError, File}; -use log::warn; use serde::{Deserialize, Serialize}; use std::time::Duration; +use tracing::warn; #[derive(Debug, Serialize, Deserialize, Clone)] #[allow(unused)] diff --git a/src/conn.rs b/src/conn.rs @@ -5,8 +5,8 @@ use crate::error::Result; use crate::event::Event; use crate::subscription::Subscription; -use log::{debug, info}; use std::collections::HashMap; +use tracing::{debug, info}; use uuid::Uuid; /// A subscription identifier has a maximum length diff --git a/src/db.rs b/src/db.rs @@ -13,7 +13,6 @@ use crate::utils::{is_hex, is_lower_hex}; use governor::clock::Clock; use governor::{Quota, RateLimiter}; use hex; -use log::{debug, info, trace, warn}; use r2d2; use r2d2_sqlite::SqliteConnectionManager; use rusqlite::params; @@ -25,6 +24,7 @@ use std::thread; use std::time::Duration; use std::time::Instant; use tokio::task; +use tracing::{debug, info, trace, warn}; pub type SqlitePool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>; pub type PooledConnection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>; diff --git a/src/event.rs b/src/event.rs @@ -5,7 +5,6 @@ use crate::nip05; use crate::utils::unix_time; use bitcoin_hashes::{sha256, Hash}; use lazy_static::lazy_static; -use log::*; use secp256k1::{schnorr, Secp256k1, VerifyOnly, XOnlyPublicKey}; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::value::Value; @@ -13,6 +12,7 @@ use serde_json::Number; use std::collections::HashMap; use std::collections::HashSet; use std::str::FromStr; +use tracing::{debug, info}; lazy_static! { /// Secp256k1 verification instance. diff --git a/src/main.rs b/src/main.rs @@ -1,11 +1,12 @@ //! Server process -use log::info; + use nostr_rs_relay::config; use nostr_rs_relay::server::start_server; use std::env; use std::sync::mpsc as syncmpsc; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; +use tracing::info; use console_subscriber::ConsoleLayer; @@ -19,10 +20,9 @@ fn db_from_args(args: &[String]) -> Option<String> { /// Start running a Nostr relay server. fn main() { - // setup logger - let _ = env_logger::try_init(); + // setup tracing + let _trace_sub = tracing_subscriber::fmt::try_init(); info!("Starting up from main"); - // get database directory from args let args: Vec<String> = env::args().collect(); let db_dir: Option<String> = db_from_args(&args); diff --git a/src/nip05.rs b/src/nip05.rs @@ -13,13 +13,13 @@ use hyper::body::HttpBody; use hyper::client::connect::HttpConnector; use hyper::Client; use hyper_tls::HttpsConnector; -use log::*; use rand::Rng; use rusqlite::params; use std::time::Duration; use std::time::Instant; use std::time::SystemTime; use tokio::time::Interval; +use tracing::{debug, info, warn}; /// NIP-05 verifier state pub struct Verifier { diff --git a/src/schema.rs b/src/schema.rs @@ -3,11 +3,11 @@ use crate::db::PooledConnection; use crate::error::Result; use crate::event::{single_char_tagname, Event}; use crate::utils::is_lower_hex; -use log::*; use rusqlite::limits::Limit; use rusqlite::params; use rusqlite::Connection; use std::time::Instant; +use tracing::{debug, error, info}; /// Startup DB Pragmas pub const STARTUP_SQL: &str = r##" diff --git a/src/server.rs b/src/server.rs @@ -19,7 +19,6 @@ use hyper::upgrade::Upgraded; use hyper::{ header, server::conn::AddrStream, upgrade, Body, Request, Response, Server, StatusCode, }; -use log::*; use serde::{Deserialize, Serialize}; use serde_json::json; use std::collections::HashMap; @@ -34,6 +33,7 @@ use tokio::sync::broadcast::{self, Receiver, Sender}; use tokio::sync::mpsc; use tokio::sync::oneshot; use tokio_tungstenite::WebSocketStream; +use tracing::*; use tungstenite::error::CapacityError::MessageTooLong; use tungstenite::error::Error as WsError; use tungstenite::handshake; diff --git a/tests/common/mod.rs b/tests/common/mod.rs @@ -1,5 +1,4 @@ use anyhow::{anyhow, Result}; -use log::{debug, info}; use nostr_rs_relay::config; use nostr_rs_relay::server::start_server; //use http::{Request, Response}; @@ -10,6 +9,7 @@ use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; use std::thread::JoinHandle; use std::time::Duration; +use tracing::{debug, info}; pub struct Relay { pub port: u16, @@ -18,7 +18,8 @@ pub struct Relay { } pub fn start_relay() -> Result<Relay> { - let _ = env_logger::try_init(); + // setup tracing + let _trace_sub = tracing_subscriber::fmt::try_init(); info!("Starting up from main"); // replace default settings let mut settings = config::Settings::default(); @@ -72,7 +73,7 @@ pub async fn wait_for_healthy_relay(relay: &Relay) -> Result<()> { } Err(_) => { // server is not yet ready, probably connection refused... - debug!("Got ERR from Relay!"); + debug!("Relay not ready, will try again..."); tokio::time::sleep(Duration::from_millis(10)).await; } }