Add support to load log4rs config file

This commit is contained in:
2024-09-21 05:08:24 +00:00
committed by GitHub
parent 1814cf330d
commit 1cc8e67fb1
7 changed files with 66 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ on:
- 'doc/**'
- Dockerfile
- docker-compose.yml
- log-cfg.yml
- README.md
pull_request:
branches: [ master ]

View File

@@ -8,6 +8,7 @@ on:
- '.github/workflows/github-pages.yaml'
- 'doc/**'
- docker-compose.yml
- log-cfg.yml
- README.md
workflow_dispatch:

View File

@@ -13,6 +13,7 @@ on:
- 'doc/**'
- Dockerfile
- docker-compose.yml
- log-cfg.yml
- README.md
# Allows you to run this workflow manually from the Actions tab

39
log-cfg.yml Normal file
View File

@@ -0,0 +1,39 @@
appenders:
stdout:
kind: console
encoder:
pattern: "{m}{n}"
requests:
kind: rolling_file
path: "log/requests.log"
encoder:
pattern: "{d} - {m}{n}"
policy:
kind: compound
trigger:
kind: size
limit: 1mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "logs/requests.{}.log"
root:
level: info
appenders:
- stdout
loggers:
html5ever::tree_builder:
level: warn
html5ever::tokenizer:
level: warn
html5ever::tokenizer::char_ref:
level: warn
reqwest::connect:
level: warn
server:
level: info
appenders:
- requests
additive: false

View File

@@ -32,6 +32,17 @@ pub fn init_with_level(level: LevelFilter) {
}
}
pub fn init_with_file(path: String) {
let config = log4rs::config::load_config_file(path, Default::default()).unwrap();
let mut h = HANDLE.get_mut();
if let Some(h) = h.as_ref() {
h.set_config(config);
} else {
let handle = init_config(config).unwrap();
h.replace(handle);
}
}
pub fn init_default() {
init_with_level(LevelFilter::Info);
}

View File

@@ -384,6 +384,13 @@ impl OptHelper {
pub fn init_log(&self) {
if self.opt.get_ref().verbose {
crate::log_cfg::init_with_level(log::LevelFilter::Debug);
} else {
match self.log_cfg() {
Some(cfg) => {
crate::log_cfg::init_with_file(cfg);
}
None => {}
}
}
}
@@ -644,6 +651,11 @@ impl OptHelper {
return PathBuf::from("/app/temp");
std::env::temp_dir()
}
/// The path to config file of [log4rs]
pub fn log_cfg(&self) -> Option<String> {
return self.settings.get_ref().get_str("log-cfg");
}
}
impl Default for OptHelper {

View File

@@ -75,6 +75,7 @@ pub fn get_settings_list() -> Vec<SettingDes> {
#[cfg(feature = "server")]
SettingDes::new("push-task-max-push-count", gettext("The maximum number of tasks to push to client at the same time."), JsonValueType::Number, Some(check_nozero_usize)).unwrap(),
SettingDes::new("fanbox-http-headers", gettext("Extra http headers for fanbox.cc."), JsonValueType::Object, Some(check_header_map)).unwrap(),
SettingDes::new("log-cfg", gettext("The path to the config file of log4rs."), JsonValueType::Str, None).unwrap(),
]
}