summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-03-28 15:01:03 +0800
committerKeuin <[email protected]>2022-03-28 15:01:03 +0800
commit0d36d2e4c4b7c0dc24c531d80081fb7aa1e6d6aa (patch)
treefa5da7fcef36bdf55e49ba99df76c5f7f8a4be06 /src/main.rs
parent53e378f8b30ec884a0ddfada945a78404ebd4a85 (diff)
Log to file.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs19
1 files changed, 16 insertions, 3 deletions
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");