-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use log::{error, info}; | ||
use log4rs; | ||
use serde_yaml; | ||
use std::env; | ||
|
||
fn main() { | ||
let config_str = include_str!("sample_config.yml"); | ||
let config = serde_yaml::from_str(config_str).unwrap(); | ||
log4rs::init_raw_config(config).unwrap(); | ||
|
||
let no_color = match env::var("NO_COLOR") { | ||
Ok(no_color) => no_color, | ||
Err(_) => "0".to_string(), | ||
}; | ||
let clicolor_force = match env::var("CLICOLOR_FORCE") { | ||
Ok(clicolor_force) => clicolor_force, | ||
Err(_) => "0".to_string(), | ||
}; | ||
let cli_color = match env::var("CLICOLOR") { | ||
Ok(cli_color) => cli_color, | ||
Err(_) => "0".to_string(), | ||
}; | ||
info!( | ||
"NO_COLOR: {}, CLICOLOR_FORCE: {}, CLICOLOR: {}", | ||
no_color, clicolor_force, cli_color | ||
); | ||
error!( | ||
"NO_COLOR: {}, CLICOLOR_FORCE: {}, CLICOLOR: {}", | ||
no_color, clicolor_force, cli_color | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
refresh_rate: 5 seconds | ||
|
||
appenders: | ||
file: | ||
kind: file | ||
pah: "log/file.log" | ||
encoder: | ||
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
|
||
root: | ||
level: info | ||
appenders: | ||
- file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"refresh_rate": "5 seconds", | ||
"appenders": { | ||
"stdout": { | ||
"kind": "console", | ||
"encoder": { | ||
"pattern": "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" | ||
}, | ||
"filters": [ | ||
{ | ||
"kind": "threshold", | ||
"level": "info" | ||
} | ||
] | ||
}, | ||
"file": { | ||
"kind": "file", | ||
"path": "log/file.log", | ||
"encoder": { | ||
"pattern": "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
} | ||
}, | ||
"rollingfile": { | ||
"kind": "rolling_file", | ||
"path": "log/rolling_file.log", | ||
"encoder": { | ||
"pattern": "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
}, | ||
"policy": { | ||
"trigger": { | ||
"kind": "time", | ||
"interval": "1 minute" | ||
}, | ||
"roller": { | ||
"kind": "fixed_window", | ||
"pattern": "log/old-rolling_file-{}.log", | ||
"base": 0, | ||
"count": 2 | ||
} | ||
} | ||
} | ||
}, | ||
"root": { | ||
"level": "info", | ||
"appenders": [ | ||
"stdout", | ||
"file", | ||
"rollingfile" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
refresh_rate = "5 seconds" | ||
|
||
[appenders.stdout] | ||
kind = "console" | ||
|
||
[appenders.stdout.encoder] | ||
pattern = "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" | ||
|
||
[[appenders.stdout.filters]] | ||
kind = "threshold" | ||
level = "info" | ||
|
||
[appenders.file] | ||
kind = "file" | ||
path = "log/file.log" | ||
|
||
[appenders.file.encoder] | ||
pattern = "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
|
||
[appenders.rollingfile] | ||
kind = "rolling_file" | ||
path = "log/rolling_file.log" | ||
|
||
[appenders.rollingfile.encoder] | ||
pattern = "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
|
||
[appenders.rollingfile.policy.trigger] | ||
kind = "time" | ||
interval = "1 minute" | ||
|
||
[appenders.rollingfile.policy.roller] | ||
kind = "fixed_window" | ||
pattern = "log/old-rolling_file-{}.log" | ||
base = 0 | ||
count = 2 | ||
|
||
[root] | ||
level = "info" | ||
appenders = [ "stdout", "file", "rollingfile" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
refresh_rate: 5 seconds | ||
|
||
appenders: | ||
stdout: | ||
kind: console | ||
encoder: | ||
pattern: "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" | ||
filters: | ||
- kind: threshold | ||
level: info | ||
file: | ||
kind: file | ||
path: "log/file.log" | ||
encoder: | ||
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
rollingfile: | ||
kind: rolling_file | ||
path: "log/rolling_file.log" | ||
encoder: | ||
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" | ||
policy: | ||
trigger: | ||
kind: time | ||
interval: 1 minute | ||
roller: | ||
kind: fixed_window | ||
pattern: "log/old-rolling_file-{}.log" | ||
base: 0 | ||
count: 2 | ||
|
||
root: | ||
level: info | ||
appenders: | ||
- stdout | ||
- file | ||
- rollingfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#[test] | ||
#[cfg(all(feature = "config_parsing", feature = "yaml_format"))] | ||
fn test_cfg_with_error_handler() { | ||
use std::{ | ||
io::{self, Write}, | ||
path::Path, | ||
}; | ||
|
||
let cfg = log4rs::config::load_config_file( | ||
Path::new("./test_cfgs/test.yml"), | ||
log4rs::config::Deserializers::default(), | ||
); | ||
assert!(cfg.is_ok()); | ||
let cfg = cfg.unwrap(); | ||
|
||
let res = log4rs::config::init_config_with_err_handler( | ||
cfg, | ||
Box::new(|e: &anyhow::Error| { | ||
let _ = writeln!(io::stderr(), "log4rs: {}", e); | ||
}), | ||
); | ||
assert!(res.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[test] | ||
#[cfg(all(feature = "config_parsing", feature = "json_format"))] | ||
fn test_init_json_cfg() { | ||
use log4rs; | ||
use std::path::Path; | ||
|
||
assert!(log4rs::init_file( | ||
Path::new("./test_cfgs/test.json"), | ||
log4rs::config::Deserializers::default() | ||
) | ||
.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[test] | ||
#[cfg(all(feature = "config_parsing", feature = "yaml_format"))] | ||
fn test_malformed_appenders() { | ||
use std::fs; | ||
|
||
let config_str = fs::read_to_string("test_cfgs/malformed_appender.yml").unwrap(); | ||
let cfg = ::serde_yaml::from_str::<log4rs::config::RawConfig>(&config_str); | ||
|
||
assert!(cfg.is_ok()); | ||
let cfg = cfg.unwrap(); | ||
|
||
let res = log4rs::config::create_raw_config(cfg); | ||
assert!(res.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[test] | ||
#[cfg(all(feature = "config_parsing", feature = "toml_format"))] | ||
fn test_init_toml_cfg() { | ||
use log4rs; | ||
use std::path::Path; | ||
|
||
assert!(log4rs::init_file( | ||
Path::new("./test_cfgs/test.toml"), | ||
log4rs::config::Deserializers::default() | ||
) | ||
.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[test] | ||
#[cfg(all(feature = "config_parsing", feature = "yaml_format"))] | ||
fn test_init_yaml_cfg() { | ||
use log4rs; | ||
use std::path::Path; | ||
|
||
assert!(log4rs::init_file( | ||
Path::new("./test_cfgs/test.yml"), | ||
log4rs::config::Deserializers::default() | ||
) | ||
.is_ok()); | ||
} |