Skip to content

Commit

Permalink
Log to file.
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Mar 28, 2022
1 parent 53e378f commit 0d36d2e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
tracing-subscriber = "0.3.9"
tracing-appender = "0.2.2"
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::{io, path};
use std::collections::HashMap;
use std::convert::Infallible;
use std::net::SocketAddr;
Expand All @@ -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;
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 0d36d2e

Please sign in to comment.