diff --git a/forest/Cargo.toml b/forest/Cargo.toml index ed32b7b9cc4d..c9b190ef708e 100644 --- a/forest/Cargo.toml +++ b/forest/Cargo.toml @@ -12,8 +12,7 @@ libp2p = "0.15.0" futures = "0.3.1" clap = "2.33.0" log = "0.4.8" -slog = "2.5.2" -slog-async = "2.3.0" -slog-term = "2.4.2" +async-log = "2.0.0" async-std = { version = "1.4.0", features = ["attributes"] } serde = { version = "1.0", features = ["derive"] } +pretty_env_logger = "0.3" diff --git a/forest/src/cli/mod.rs b/forest/src/cli/mod.rs index 4c525b8fef36..2d801156aa7c 100644 --- a/forest/src/cli/mod.rs +++ b/forest/src/cli/mod.rs @@ -6,11 +6,10 @@ mod config; pub use config::Config; use clap::{App, Arg}; -use slog::Logger; use std::io; use utils::{read_file_to_string, read_toml}; -pub(super) fn cli(_log: &Logger) -> Result { +pub(super) fn cli() -> Result { let app = App::new("Forest") .version("0.0.1") .author("ChainSafe Systems ") diff --git a/forest/src/log/mod.rs b/forest/src/log/mod.rs deleted file mode 100644 index cedbb660d070..000000000000 --- a/forest/src/log/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2020 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use slog::Drain; -use slog::*; -use slog_async; -use slog_term; - -pub fn setup_logging() -> slog::Logger { - let decorator = slog_term::TermDecorator::new().build(); - let drain = slog_term::FullFormat::new(decorator).build().fuse(); - let drain = slog_async::Async::new(drain).build().fuse(); - - slog::Logger::root(drain, o!()) -} diff --git a/forest/src/logger/mod.rs b/forest/src/logger/mod.rs new file mode 100644 index 000000000000..2820edb98bd2 --- /dev/null +++ b/forest/src/logger/mod.rs @@ -0,0 +1,11 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +pub(crate) fn setup_logger() { + let logger = pretty_env_logger::formatted_timed_builder() + .filter(None, log::LevelFilter::Info) + .build(); + async_log::Logger::wrap(logger, || 0) + .start(log::LevelFilter::Info) + .unwrap(); +} diff --git a/forest/src/main.rs b/forest/src/main.rs index d37b6730fb60..55f22b2121d5 100644 --- a/forest/src/main.rs +++ b/forest/src/main.rs @@ -2,26 +2,23 @@ // SPDX-License-Identifier: Apache-2.0, MIT mod cli; -mod log; - +mod logger; use self::cli::cli; use async_std::task; use forest_libp2p::{get_keypair, Libp2pService}; use libp2p::identity::{ed25519, Keypair}; -use slog::{info, trace}; +use log::{info, trace}; use utils::write_to_file; #[async_std::main] async fn main() { - let log = log::setup_logging(); - info!(log, "Starting Forest"); + logger::setup_logger(); + info!("Starting Forest"); // Capture CLI inputs - let config = cli(&log).expect("CLI error"); - - let logger = log.clone(); + let config = cli().expect("CLI error"); - let net_keypair = match get_keypair(&log, &"/.forest/libp2p/keypair") { + let net_keypair = match get_keypair(&"/.forest/libp2p/keypair") { Some(kp) => kp, None => { // Keypair not found, generate and save generated keypair @@ -29,18 +26,18 @@ async fn main() { // Save Ed25519 keypair to file // TODO rename old file to keypair.old(?) if let Err(e) = write_to_file(&gen_keypair.encode(), &"/.forest/libp2p/", "keypair") { - info!(log, "Could not write keystore to disk!"); - trace!(log, "Error {:?}", e); + info!("Could not write keystore to disk!"); + trace!("Error {:?}", e); }; Keypair::Ed25519(gen_keypair) } }; - let lp2p_service = Libp2pService::new(logger, &config.network, net_keypair); + let lp2p_service = Libp2pService::new(&config.network, net_keypair); task::block_on(async move { lp2p_service.run().await; }); - info!(log, "Forest finish shutdown"); + info!("Forest finish shutdown"); } diff --git a/node/forest_libp2p/Cargo.toml b/node/forest_libp2p/Cargo.toml index eed236b48d46..d6f8c9236d38 100644 --- a/node/forest_libp2p/Cargo.toml +++ b/node/forest_libp2p/Cargo.toml @@ -10,6 +10,5 @@ libp2p = "0.15.0" futures = "0.3.1" futures-util = "0.3.1" log = "0.4.8" -slog = "2.5.2" async-std = { version = "1.4.0", features = ["unstable"] } serde = { version = "1.0", features = ["derive"] } diff --git a/node/forest_libp2p/src/behaviour.rs b/node/forest_libp2p/src/behaviour.rs index 5459fa0e1ef3..f26d17e38ebc 100644 --- a/node/forest_libp2p/src/behaviour.rs +++ b/node/forest_libp2p/src/behaviour.rs @@ -13,7 +13,7 @@ use libp2p::ping::{ }; use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess}; use libp2p::NetworkBehaviour; -use slog::{debug, Logger}; +use log::debug; use std::{task::Context, task::Poll}; #[derive(NetworkBehaviour)] @@ -25,8 +25,6 @@ pub struct ForestBehaviour, #[behaviour(ignore)] events: Vec, - #[behaviour(ignore)] - log: Logger, } #[derive(Debug)] @@ -82,29 +80,19 @@ impl match event.result { Result::Ok(PingSuccess::Ping { rtt }) => { debug!( - self.log, "PingSuccess::Ping rtt to {} is {} ms", event.peer.to_base58(), rtt.as_millis() ); } Result::Ok(PingSuccess::Pong) => { - debug!( - self.log, - "PingSuccess::Pong from {}", - event.peer.to_base58() - ); + debug!("PingSuccess::Pong from {}", event.peer.to_base58()); } Result::Err(PingFailure::Timeout) => { - debug!(self.log, "PingFailure::Timeout {}", event.peer.to_base58()); + debug!("PingFailure::Timeout {}", event.peer.to_base58()); } Result::Err(PingFailure::Other { error }) => { - debug!( - self.log, - "PingFailure::Other {}: {}", - event.peer.to_base58(), - error - ); + debug!("PingFailure::Other {}: {}", event.peer.to_base58(), error); } } } @@ -120,12 +108,12 @@ impl info, observed_addr, } => { - debug!(self.log, "Identified Peer {:?}", peer_id); - debug!(self.log, "protocol_version {:}?", info.protocol_version); - debug!(self.log, "agent_version {:?}", info.agent_version); - debug!(self.log, "listening_ addresses {:?}", info.listen_addrs); - debug!(self.log, "observed_address {:?}", observed_addr); - debug!(self.log, "protocols {:?}", info.protocols); + debug!("Identified Peer {:?}", peer_id); + debug!("protocol_version {:}?", info.protocol_version); + debug!("agent_version {:?}", info.agent_version); + debug!("listening_ addresses {:?}", info.listen_addrs); + debug!("observed_address {:?}", observed_addr); + debug!("protocols {:?}", info.protocols); } IdentifyEvent::Sent { .. } => (), IdentifyEvent::Error { .. } => (), @@ -147,7 +135,7 @@ impl ForestBehaviou } impl ForestBehaviour { - pub fn new(log: Logger, local_key: &Keypair) -> Self { + pub fn new(local_key: &Keypair) -> Self { let local_peer_id = local_key.public().into_peer_id(); let gossipsub_config = GossipsubConfig::default(); ForestBehaviour { @@ -155,7 +143,6 @@ impl ForestBehaviou mdns: Mdns::new().expect("Could not start mDNS"), ping: Ping::default(), identify: Identify::new("forest/libp2p".into(), "0.0.1".into(), local_key.public()), - log, events: vec![], } } diff --git a/node/forest_libp2p/src/service.rs b/node/forest_libp2p/src/service.rs index cf2473f0ccff..ab200f79c066 100644 --- a/node/forest_libp2p/src/service.rs +++ b/node/forest_libp2p/src/service.rs @@ -15,7 +15,7 @@ use libp2p::{ identity::{ed25519, Keypair}, mplex, secio, yamux, PeerId, Swarm, Transport, }; -use slog::{debug, error, info, trace, Logger}; +use log::{debug, error, info, trace}; use std::io::{Error, ErrorKind}; use std::time::Duration; use utils::{get_home_dir, read_file_to_vec}; @@ -47,31 +47,29 @@ pub struct Libp2pService { pubsub_receiver_out: Receiver, pubsub_sender_out: Sender, - - log: Logger, } impl Libp2pService { /// Constructs a Libp2pService - pub fn new(log: Logger, config: &Libp2pConfig, net_keypair: Keypair) -> Self { + pub fn new(config: &Libp2pConfig, net_keypair: Keypair) -> Self { let peer_id = PeerId::from(net_keypair.public()); - info!(log, "Local peer id: {:?}", peer_id); + info!("Local peer id: {:?}", peer_id); let transport = build_transport(net_keypair.clone()); let mut swarm = { - let be = ForestBehaviour::new(log.clone(), &net_keypair); + let be = ForestBehaviour::new(&net_keypair); Swarm::new(transport, be, peer_id) }; for node in config.bootstrap_peers.clone() { match node.parse() { Ok(to_dial) => match Swarm::dial_addr(&mut swarm, to_dial) { - Ok(_) => debug!(log, "Dialed {:?}", node), - Err(e) => debug!(log, "Dial {:?} failed: {:?}", node, e), + Ok(_) => debug!("Dialed {:?}", node), + Err(e) => debug!("Dial {:?} failed: {:?}", node, e), }, - Err(err) => error!(log, "Failed to parse address to dial: {:?}", err), + Err(err) => error!("Failed to parse address to dial: {:?}", err), } } @@ -96,7 +94,6 @@ impl Libp2pService { pubsub_sender_in, pubsub_receiver_out, pubsub_sender_out, - log, } } @@ -117,7 +114,7 @@ impl Libp2pService { topics, message, } => { - info!(self.log, "Got a Gossip Message from {:?}", source); + info!( "Got a Gossip Message from {:?}", source); self.pubsub_sender_out.send(NetworkEvent::PubsubMessage { source, topics, @@ -168,22 +165,22 @@ pub fn build_transport(local_key: Keypair) -> Boxed<(PeerId, StreamMuxerBox), Er } /// Fetch keypair from disk, returning none if it cannot be decoded -pub fn get_keypair(log: &Logger, path: &str) -> Option { +pub fn get_keypair(path: &str) -> Option { let path_to_keystore = get_home_dir() + path; match read_file_to_vec(&path_to_keystore) { Err(e) => { - info!(log, "Networking keystore not found!"); - trace!(log, "Error {:?}", e); + info!("Networking keystore not found!"); + trace!("Error {:?}", e); None } Ok(mut vec) => match ed25519::Keypair::decode(&mut vec) { Ok(kp) => { - info!(log, "Recovered keystore from {:?}", &path_to_keystore); + info!("Recovered keystore from {:?}", &path_to_keystore); Some(Keypair::Ed25519(kp)) } Err(e) => { - info!(log, "Could not decode networking keystore!"); - trace!(log, "Error {:?}", e); + info!("Could not decode networking keystore!"); + trace!("Error {:?}", e); None } },