log to stdout or to a file
This commit is contained in:
parent
908a6895a3
commit
026b0285a9
@ -5,7 +5,9 @@ bind_port = 5063
|
||||
|
||||
[log]
|
||||
# log file location
|
||||
log_file = "sip-tarpit.log"
|
||||
log_file = "CONSOLE" # to log to stdout
|
||||
# log_file = "sip-tarpit.log" # to log to a file
|
||||
|
||||
# level of logging (DEBUG, INFO, WARN, ERROR)
|
||||
level = "INFO"
|
||||
|
||||
|
@ -74,7 +74,7 @@ impl Config {
|
||||
log_file.unwrap_or("sip-tarpit.log".to_owned()),
|
||||
level.unwrap_or("INFO".to_owned())
|
||||
),
|
||||
None => ("sip-tarpit.log".to_owned(), "INFO".to_owned())
|
||||
None => ("CONSOLE".to_owned(), "INFO".to_owned())
|
||||
};
|
||||
|
||||
let delay = match config_toml.tarpit {
|
||||
|
37
src/main.rs
37
src/main.rs
@ -19,6 +19,7 @@ use tokio::net::UdpSocket;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use log::LevelFilter;
|
||||
use log4rs::append::file::FileAppender;
|
||||
use log4rs::append::console::ConsoleAppender;
|
||||
use log4rs::config::{Appender, Config, Root};
|
||||
use log4rs::encode::pattern::PatternEncoder;
|
||||
|
||||
@ -164,7 +165,7 @@ impl Server {
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let config: config::Config = config::Config::new();
|
||||
|
||||
let log_level = match config.log_level.to_lowercase().as_str() {
|
||||
let log_level_filter = match config.log_level.to_lowercase().as_str() {
|
||||
"trace" => LevelFilter::Trace,
|
||||
"debug" => LevelFilter::Debug,
|
||||
"info" => LevelFilter::Info,
|
||||
@ -173,20 +174,36 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
_ => LevelFilter::Info,
|
||||
};
|
||||
|
||||
if log_level == LevelFilter::Debug {
|
||||
if log_level_filter == LevelFilter::Debug {
|
||||
log::info!("Debug mode enabled");
|
||||
println!("{:#?}", config);
|
||||
}
|
||||
|
||||
let logfile = FileAppender::builder()
|
||||
.encoder(Box::new(PatternEncoder::new("{d} - {l} - {m}\n")))
|
||||
.build("sip-tarpit.log")
|
||||
.unwrap();
|
||||
let logconfig: log4rs::Config;
|
||||
|
||||
// write log to stdout or to file
|
||||
if config.log_file.to_lowercase().as_str() == "console" {
|
||||
let stdout = ConsoleAppender::builder()
|
||||
.encoder(Box::new(PatternEncoder::new("{d} - {l} - {m}\n")))
|
||||
.build();
|
||||
|
||||
logconfig = Config::builder()
|
||||
.appender(Appender::builder().build("stdout", Box::new(stdout)))
|
||||
.build(Root::builder().appender("stdout").build(log_level_filter))
|
||||
.unwrap();
|
||||
} else {
|
||||
|
||||
let logfile = FileAppender::builder()
|
||||
.encoder(Box::new(PatternEncoder::new("{d} - {l} - {m}\n")))
|
||||
.build(config.log_file.as_str())
|
||||
.unwrap();
|
||||
|
||||
logconfig = Config::builder()
|
||||
.appender(Appender::builder().build("logfile", Box::new(logfile)))
|
||||
.build(Root::builder().appender("logfile").build(log_level_filter))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let logconfig = Config::builder()
|
||||
.appender(Appender::builder().build("logfile", Box::new(logfile)))
|
||||
.build(Root::builder().appender("logfile").build(log_level))
|
||||
.unwrap();
|
||||
log4rs::init_config(logconfig).unwrap();
|
||||
|
||||
let addr = format!("{}:{}", config.bind_addr, config.bind_port);
|
||||
|
Loading…
Reference in New Issue
Block a user