Skip to content

Commit eaea781

Browse files
author
chenjunliang
committed
日志使用本地时间
1 parent e50a270 commit eaea781

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ anyhow = "1.0.61"
1717
chrono = "0.4.21"
1818
rust_decimal = "1.25.0"
1919
log = "0.4.17"
20-
simple_logger = "2.2.0"
20+
simple_logger = { version = "2.2.0", features = ["timestamps"] }
2121

2222
[target.'cfg(unix)'.dependencies]
2323
daemonize = "0.4.1"

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ mod daemon_util;
44
mod libc_util;
55
mod tasks;
66

7+
use crate::tasks::start_backstage_task;
78
use anyhow::Result;
89
use clap::Parser;
10+
use log::LevelFilter;
11+
use simple_logger::SimpleLogger;
912
use std::fs;
10-
use crate::tasks::start_backstage_task;
1113

1214
/// An extension tool of mysql-audit, which provides functions such as log rotation and log cleaning.
1315
#[derive(Parser, Debug)]
@@ -34,7 +36,11 @@ fn parse_path(s: &str) -> Result<String> {
3436

3537
/// Initialize the log component
3638
fn init_log() {
37-
simple_logger::init_with_level(log::Level::Info).unwrap();
39+
SimpleLogger::new()
40+
.with_level(LevelFilter::Info)
41+
.with_local_timestamps()
42+
.init()
43+
.unwrap();
3844
}
3945

4046
/// main function

src/tasks.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fs;
66
use std::path::Path;
77
use std::sync::{Arc, Mutex};
88
use std::thread::sleep;
9-
use std::time::{Duration};
9+
use std::time::Duration;
1010

1111
/// Handling mysql audit logs
1212
fn mysql_audit_log_handle(sched: &mut JobScheduler, path: String, max_size: u32, max_file: u32) {
@@ -36,8 +36,11 @@ fn mysql_audit_log_handle(sched: &mut JobScheduler, path: String, max_size: u32,
3636
let parent_path = file_path.parent().unwrap();
3737
let local: DateTime<Local> = Local::now();
3838
let time_str = local.format("%Y%m%d%H%M%S").to_string();
39-
let new_file_name =
40-
origin_name.to_owned() + "-" + time_str.as_str() + "." + origin_file_type;
39+
let new_file_name = origin_name.to_owned()
40+
+ "-"
41+
+ time_str.as_str()
42+
+ "."
43+
+ origin_file_type;
4144
let new_file_path =
4245
parent_path.to_str().unwrap().to_owned() + "/" + new_file_name.as_str();
4346
fs::copy(file_path, new_file_path.as_str()).unwrap();

0 commit comments

Comments
 (0)