diff options
author | Keuin <[email protected]> | 2022-03-28 15:01:03 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-03-28 15:01:03 +0800 |
commit | 0d36d2e4c4b7c0dc24c531d80081fb7aa1e6d6aa (patch) | |
tree | fa5da7fcef36bdf55e49ba99df76c5f7f8a4be06 | |
parent | 53e378f8b30ec884a0ddfada945a78404ebd4a85 (diff) |
Log to file.
-rw-r--r-- | Cargo.lock | 42 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/main.rs | 19 |
3 files changed, 60 insertions, 4 deletions
@@ -215,6 +215,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403" [[package]] +name = "crossbeam-channel" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaa7bd5fb665c6864b5f963dd9097905c54125909c7aa94c9e18507cdbe6c53" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils 0.8.8", +] + +[[package]] name = "crossbeam-epoch" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -830,6 +840,7 @@ dependencies = [ "teloxide", "tokio", "tracing", + "tracing-appender", "tracing-subscriber", "urandom", "warp", @@ -1048,6 +1059,15 @@ dependencies = [ ] [[package]] +name = "num_threads" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aba1801fb138d8e85e11d0fc70baf4fe1cdfffda7c6cd34a854905df588e5ed0" +dependencies = [ + "libc", +] + +[[package]] name = "once_cell" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1874,6 +1894,17 @@ dependencies = [ ] [[package]] +name = "time" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd" +dependencies = [ + "itoa", + "libc", + "num_threads", +] + +[[package]] name = "tinyvec" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1998,6 +2029,17 @@ dependencies = [ ] [[package]] +name = "tracing-appender" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" +dependencies = [ + "crossbeam-channel", + "time", + "tracing-subscriber", +] + +[[package]] name = "tracing-attributes" version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -17,4 +17,5 @@ sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite" ] } urandom = "0.1.0" bs58 = "0.4.0" tracing = "0.1.32" -tracing-subscriber = "0.3.9"
\ No newline at end of file +tracing-subscriber = "0.3.9" +tracing-appender = "0.2.2"
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1c1bd48..2c4263d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::{io, path}; use std::collections::HashMap; use std::convert::Infallible; use std::net::SocketAddr; @@ -6,7 +7,8 @@ use std::str::FromStr; use teloxide::prelude2::*; use tracing::{debug, info, Level}; use tracing::instrument; -use tracing_subscriber::FmtSubscriber; +use tracing_subscriber::fmt; +use tracing_subscriber::layer::SubscriberExt; use warp::Filter; use config::Config; @@ -50,9 +52,20 @@ async fn main() { } }; eprintln!("Configuration is loaded. Set log level to {:?}.", log_level); - let subscriber = FmtSubscriber::builder() + let log_file_path = path::Path::new(&config.log_file); + let parent = log_file_path.parent().expect("Cannot extract parent."); + let filename = log_file_path.file_name().expect("Cannot extract file name."); + let (nb_file_appender, _guard) = + tracing_appender::non_blocking( + tracing_appender::rolling::never(parent, filename)); + let subscriber = fmt::Subscriber::builder() .with_max_level(log_level) - .finish(); + .with_writer(io::stderr) // log to stderr + .finish() + .with(fmt::Layer::default() + .with_writer(nb_file_appender) // log to file + .with_ansi(false)); // remove color control characters from log file + tracing::subscriber::set_global_default(subscriber) .expect("Failed to set default subscriber"); |