Skip to content

Commit

Permalink
add debug flag in logging
Browse files Browse the repository at this point in the history
Signed-off-by: Arshdeep54 <[email protected]>
  • Loading branch information
Arshdeep54 committed Feb 26, 2025
1 parent f46758a commit d30e0b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions sample.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ token = 'secret_token'
# app(https://slack.com/apps/A0F7XDUAZ-incoming-webhooks)
# and paste the hook URL here. You can customize the icon and name as you like.
slack = 'https://hooks.slack.com/services/ABCDEFGHI/ABCDEFGHI/abcdefghijklmnopqrstuvwx'

# Logging
[logging]
debug=true
10 changes: 10 additions & 0 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ pub struct NotifiersConf {
pub slack: String,
}

#[derive(Deserialize, Clone)]
pub struct LoggingConf {
pub debug: bool,
}

#[derive(Deserialize, Clone)]
pub struct Config {
pub hostname: String,
pub keyhouse: KeyhouseConf,
pub notifiers: NotifiersConf,
pub logging: LoggingConf,
}

pub fn read_config() -> Result<Config> {
Expand All @@ -47,6 +53,9 @@ pub fn set_config_value(key: &str, val: &str) -> Result<()> {
"notifiers.slack" => {
doc["notifiers"]["slack"] = value(val);
}
"logging.debug" => {
doc["logging"]["debug"] = value(val);
}
_ => {
return Err("Invalid Key passed".into());
}
Expand All @@ -65,6 +74,7 @@ pub fn get_config_value(key: &str) -> Result<String> {
"keyhouse.base_url" => doc["keyhouse"]["base_url"].as_str(),
"keyhouse.token" => doc["keyhouse"]["token"].as_str(),
"notifiers.slack" => doc["notifiers"]["slack"].as_str(),
"logging.debug" => doc["logging"]["debug"].as_str(),
_ => {
return Err("Invalid Key passed".into());
}
Expand Down
20 changes: 18 additions & 2 deletions src/lib/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::Write;
use std::time::{SystemTime, UNIX_EPOCH};
use std::io::Result;
use chrono::{DateTime, Utc};
use crate::config::{read_config, Config};

pub fn log(filepath: &str, status: &str, message: &str) -> Result<()> {
let start = SystemTime::now();
Expand All @@ -22,6 +23,18 @@ pub fn log(filepath: &str, status: &str, message: &str) -> Result<()> {
}

pub fn logln(message: &str) {
let config = match read_config(){
Ok(config) => config,
Err(_) => {
log("/opt/watchdog/custom-logs/watchdog.logs", "FAILURE", "Failed to read config").expect("Failed to log");
return;
},
};
let debug=get_debug(&config);
if debug==false {
log("/opt/watchdog/custom-logs/watchdog.logs", "FAILURE", "debug false in logln").expect("Failed to log");
return;
}
let start = SystemTime::now();
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
let timestamp = since_the_epoch.as_secs();
Expand All @@ -35,6 +48,9 @@ pub fn logln(message: &str) {
.create(true)
.open(filepath).expect("Failed to open log file");


file.write_all(log_message.as_bytes()).expect("Failed to write to log file");
}
}

pub fn get_debug(config: &Config) -> bool {
config.logging.debug
}

0 comments on commit d30e0b2

Please sign in to comment.