Skip to content

Commit 6f2d8c3

Browse files
author
chenjunliang
committed
log
1 parent 4b8d4dd commit 6f2d8c3

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ rcron = "1.1.3"
1313
clap = { version = "3.2.16", features = ["derive"] }
1414
anyhow = "1.0.60"
1515
chrono = "0.4.21"
16-
rust_decimal = "1.25.0"
16+
rust_decimal = "1.25.0"
17+
log = "0.4.14"
18+
simple_logger = "2.1.0"

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ fn parse_path(s: &str) -> Result<String> {
2929
Ok(s.into())
3030
}
3131

32+
fn init_log() {
33+
simple_logger::init_with_level(log::Level::Info).unwrap();
34+
}
35+
3236
fn main() -> Result<()> {
3337
let options = Options::parse();
34-
println!("Received parameters:{:?}", options);
38+
log::info!("App environments:{:?}", options);
39+
40+
init_log();
3541

3642
let username = libc_util::get_current_user();
3743
let pkg_name = env!("CARGO_PKG_NAME");

src/tasks.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ use std::time::{Duration, SystemTime};
1010
fn mysql_audit_log_rotate(sched: &mut JobScheduler, path: String, max_size: u32, max_file: u32) {
1111
// utc time
1212
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), move || {
13-
println!(
14-
"执行日志轮转任务! path:{}, max_size:{}, max_file:{}, time:{:?}",
15-
path,
16-
max_size,
17-
max_file,
18-
SystemTime::now()
19-
);
2013
let r = fs::File::options().write(true).open(path.as_str());
2114
match r {
2215
Ok(file) => {
@@ -26,17 +19,17 @@ fn mysql_audit_log_rotate(sched: &mut JobScheduler, path: String, max_size: u32,
2619
let file_size = (file_len / cf / cf).round_dp(0);
2720
let file_max_size = Decimal::from(max_size);
2821
if file_size >= file_max_size {
29-
println!("The file size reaches the split standard:{:?}M", file_size);
22+
log::info!("The file size reaches the split standard:{:?}M", file_size);
3023
let file_path = Path::new(path.as_str());
3124
let origin_file_name =
3225
file_path.file_name().unwrap().to_str().unwrap().to_string();
3326
let sv: Vec<&str> = origin_file_name.split(".").collect();
3427
let origin_name = sv[0];
3528
let origin_file_type = sv[1];
36-
println!("origin_name:{}", origin_name);
29+
log::info!("origin_name:{}", origin_name);
3730

3831
let parent_path = file_path.parent().unwrap();
39-
println!("parent_path:{}", parent_path.to_str().unwrap());
32+
log::info!("parent_path:{}", parent_path.to_str().unwrap());
4033

4134
let local: DateTime<Local> = Local::now();
4235
let time_str = local.format("%Y%m%d%H%M%S").to_string();
@@ -45,7 +38,7 @@ fn mysql_audit_log_rotate(sched: &mut JobScheduler, path: String, max_size: u32,
4538
let new_file_path =
4639
parent_path.to_str().unwrap().to_owned() + "/" + new_file_name.as_str();
4740
fs::copy(file_path, new_file_path.as_str()).unwrap();
48-
println!("Log file copied to:{}", new_file_path);
41+
log::info!("Log file copied to:{}", new_file_path);
4942

5043
file.set_len(0).unwrap();
5144

@@ -57,20 +50,20 @@ fn mysql_audit_log_rotate(sched: &mut JobScheduler, path: String, max_size: u32,
5750
.collect::<Vec<String>>();
5851
files.sort();
5952
if files.len() > max_file as usize {
60-
println!("The number of files exceeds the limit,start cleaning...");
53+
log::info!("The number of files exceeds the limit,start cleaning...");
6154
for i in 0..(files.len() - max_file as usize) {
6255
let item_file_name = files.get(i).unwrap();
6356
let item_path =
6457
parent_path.to_str().unwrap().to_owned() + "/" + item_file_name;
6558
fs::remove_file(item_path.as_str())
6659
.expect("Failed to clean up redundant files");
67-
println!("Delete file {}", item_path);
60+
log::info!("Delete file {}", item_path);
6861
}
6962
}
7063
}
7164
}
7265
Err(e) => {
73-
eprintln!("Read file failed[{}]:{}", path.as_str(), e.to_string());
66+
log::error!("Read file failed[{}]:{}", path.as_str(), e.to_string());
7467
}
7568
}
7669
}));

0 commit comments

Comments
 (0)