Skip to content

Commit 87a02ae

Browse files
authored
add log4rs support (paritytech#124)
1 parent 22f0182 commit 87a02ae

File tree

8 files changed

+340
-9
lines changed

8 files changed

+340
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ chainx/
2020
!rpc/src/chainx
2121
bak/
2222
.chainx/
23-
.sub/
23+
.sub/
24+
log/

Cargo.lock

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jsonrpc-core = "14.2.0"
1010
log = "0.4.8"
1111
parking_lot = "0.10.0"
1212
structopt = "0.3.8"
13+
log4rs = { version = "0.12", features = ["rolling_file_appender", "compound_policy", "size_trigger", "fixed_window_roller"] }
1314

1415
# Substrate client
1516
sc-basic-authorship = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc3" }

cli/src/cli.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,28 @@ pub struct Cli {
88

99
#[structopt(flatten)]
1010
pub run: RunCmd,
11+
12+
#[structopt(long = "log4rs")]
13+
/// Use `env_logger`, not `log4rs`. notice `env_logger` can't print into file directly
14+
pub log4rs: bool,
15+
16+
#[structopt(long = "log-dir", default_value = "./log")]
17+
/// When use `log4rs`, assign the path of log dir, notice this would create the dir directly. Default dir is `./log`
18+
pub log_dir: String,
19+
#[structopt(long = "log-name", default_value = "chainx.log")]
20+
/// When use `log4rs`, assign the log file name. Default dir is `chainx.log`, thus when use default config, the log would create in ./log/chainx.log
21+
pub log_name: String,
22+
#[structopt(long = "log-size", default_value = "300")]
23+
/// When use `log4rs`, the default log size in log rotation. The unit is MB, default is 300 MB
24+
pub log_size: u64,
25+
#[structopt(long = "log-roll-count", default_value = "10")]
26+
/// When use `log4rs`, the max log rotation. Default is 10. If the log is more then the number, would delete old file.
27+
/// The log would like `chainx.log.0`, `chainx.log.1` ... `chainx.log.10`
28+
pub log_roll_count: u32,
29+
#[structopt(long = "log-console")]
30+
/// When use `log4rs`, print log into console. Default is false
31+
pub log_console: bool,
32+
#[structopt(long = "log-compression")]
33+
/// When use `log4rs`, compress the old log to save space. the log would like `chainx.log.gz.0`
34+
pub log_compression: bool,
1135
}

cli/src/command.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use crate::chain_spec;
1919
use crate::cli::Cli;
2020
use crate::service;
21-
use sc_cli::SubstrateCli;
21+
use sc_cli::{CliConfiguration, SubstrateCli};
2222

2323
impl SubstrateCli for Cli {
2424
fn impl_name() -> &'static str {
@@ -63,17 +63,24 @@ fn load_spec(id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
6363
let p = std::path::PathBuf::from(path);
6464
if !p.exists() {
6565
// TODO more better hint
66-
return Err(format!("not a valid path or just allow [\"dev\", \"local\"]"))
66+
return Err(format!(
67+
"not a valid path or just allow [\"dev\", \"local\"]"
68+
));
6769
}
6870
Box::new(chain_spec::ChainSpec::from_json_file(p)?)
69-
},
71+
}
7072
})
7173
}
7274

7375
/// Parse and run command line arguments
7476
pub fn run() -> sc_cli::Result<()> {
7577
let cli = Cli::from_args();
7678

79+
if cli.log4rs {
80+
let s = cli.run.log_filters()?;
81+
crate::logger::init_logger_log4rs(&s, &cli)?;
82+
}
83+
7784
match &cli.subcommand {
7885
Some(subcommand) => {
7986
let runner = cli.create_runner(subcommand)?;

cli/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod chain_spec;
88
mod service;
99
mod cli;
1010
mod command;
11+
mod logger;
1112

1213
pub use command::*;
1314
pub use sc_cli::Result;

0 commit comments

Comments
 (0)