Add log4rs config and fix main to use exit codes

This commit is contained in:
Victor Mignot 2022-10-21 19:59:52 -04:00
parent f08ab5bbe0
commit 6df9437531
No known key found for this signature in database
GPG key ID: FFE4EF056FB5E0D0
2 changed files with 99 additions and 10 deletions

59
log4rs_config.yaml Normal file
View file

@ -0,0 +1,59 @@
refresh_rate: 30 seconds
appenders:
stdout:
kind: console
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"
rolling_logs:
kind: rolling_file
path: logs/yorokobot-latest.log
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"
policy:
trigger: # Specify when to switch to another log file
kind: size
limit: 30 mb
roller: # Specify what to do with older logs files
kind: fixed_window
pattern: logs/yorokobot_{}.gz
count: 10
rolling_debug:
kind: rolling_file
path: logs/traces-latest.log
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"
policy:
trigger:
kind: size
limit: 30 mb
roller:
kind: fixed_window
pattern: logs/yorokobot_traces_{}.gz
count: 20
root:
level: warn
appenders:
- stdout
loggers:
bot_infos:
level: info
appenders:
- rolling_debug
bot_warn_errors:
level: warn
appenders:
- rolling_logs
bot_traces:
level: debug
appenders:
- rolling_debug

View file

@ -1,3 +1,4 @@
use log::error;
use std::env; use std::env;
use yorokobot::{ use yorokobot::{
@ -6,22 +7,51 @@ use yorokobot::{
}; };
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> std::process::ExitCode {
let discord_token = env::var("DISCORD_TOKEN").expect("Cannot fetch Discord token"); // Start the logger
log4rs::init_file("log4rs_config.yaml", Default::default()).unwrap();
let mongodb_uri = env::var("MONGODB_URI").expect("Cannot fetch Mongo URI"); let discord_token = match env::var("DISCORD_TOKEN") {
Ok(t) => t,
Err(_) => {
error!(target: "bot_warn_errors", "Could not find DISCORD_TOKEN environment variable.");
return std::process::ExitCode::FAILURE;
}
};
let mongodb_uri = match env::var("MONGODB_URI") {
Ok(u) => u,
Err(_) => {
error!(target: "bot_warn_errors", "Could not find MONGODB_URI environment variable.");
return std::process::ExitCode::FAILURE;
}
};
let credentials = ClientCredentials { let credentials = ClientCredentials {
discord_token: &discord_token, discord_token: &discord_token,
mongo_uri: &mongodb_uri, mongo_uri: &mongodb_uri,
}; };
let mut client = Client::new(credentials) let mut client = match Client::new(credentials).await {
.await Ok(c) => c,
.expect("Could not create client"); Err(_) => {
error!(target: "bot_warn_errors", "Could not instantiate the bot client.");
client.connect().await.unwrap_or_else(|error| match error { return std::process::ExitCode::FAILURE;
ClientsError::Database(e) => panic!("Could not connect to database: {:?}", e), }
ClientsError::Discord(e) => panic!("Could not connect to Discord: {:?}", e), };
});
if let Err(error) = client.connect().await {
match error {
ClientsError::Database(e) => {
error!(target: "bot_warn_errors", "Could not connect to database: {:?}", e)
}
ClientsError::Discord(e) => {
error!(target: "bot_warn_errors", "Could not connect to Discord: {:?}", e)
}
};
return std::process::ExitCode::FAILURE;
}
std::process::ExitCode::SUCCESS
} }