Skip to content

Commit

Permalink
feat(backend): allow to set log level via config file (#182)
Browse files Browse the repository at this point in the history
Co-authored-by: ravenclaw900 <50060110+ravenclaw900@users.noreply.github.com>
  • Loading branch information
MichaIng and ravenclaw900 committed Mar 12, 2022
1 parent 779a1c9 commit 5643d51
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Log level
# - Options: "off", "error", "warn", "info", "debug", "trace"
# - Default: "info"
#log_level = "info"

# TCP network port
# - Default: 5252
#port = 5252

# TLS for HTTPS
# - Default: false
#tls = false
#cert = "/path/to/cert"
#key = "/path/to/key"

# Password protection
# - Default: false
#pass = false
Expand All @@ -16,6 +23,7 @@
# Token expiry time in seconds
# - Default: 3600
#expiry = 3600

# Other nodes viewable on frontend page
# - Default: []
# Please insert the IP addresses/domains and ports of these nodes
Expand Down
12 changes: 12 additions & 0 deletions src/backend/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::str::FromStr;
use toml::Value;

pub struct Config {
pub log_level: log::LevelFilter,

pub port: u16,

pub tls: bool,
Expand Down Expand Up @@ -32,6 +35,14 @@ pub fn config() -> Config {
.parse::<Value>()
.expect("Invalid config file");

let log_level = log::LevelFilter::from_str(
cfg.get("log_level")
.unwrap_or(&Value::String("info".to_string()))
.as_str()
.unwrap(),
)
.unwrap();

#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
let port: u16 = cfg
Expand Down Expand Up @@ -106,6 +117,7 @@ pub fn config() -> Config {
}

Config {
log_level,
port,
tls,
cert,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
const DIR: include_dir::Dir = include_dir::include_dir!("dist");

SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.with_level(CONFIG.log_level)
.env()
.init()
.unwrap();
Expand Down

0 comments on commit 5643d51

Please sign in to comment.