Skip to content

Commit 6523d7b

Browse files
committed
refactor: 优化日志输出
1 parent fc088f6 commit 6523d7b

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/event.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn handle_process_event(sender: Sender<ProcessEvent>, rx: Receiver<ProcessEv
7070
match received.event_type {
7171
EventType::Running => {
7272
info!(
73-
"The {} service started with pid: {}",
73+
"[{}] started with pid: {}",
7474
received.service_name,
7575
received
7676
.pid
@@ -85,7 +85,7 @@ pub fn handle_process_event(sender: Sender<ProcessEvent>, rx: Receiver<ProcessEv
8585
.map_or_else(|| "unknown".to_string(), |pid| pid.to_string());
8686
let msg = received.data.or(Some("unknown".to_string())).unwrap();
8787
warn!(
88-
"The {} service (pid: {}) has exited:{}",
88+
"[{}] (pid: {}) has exited:{}",
8989
received.service_name, pid, msg
9090
);
9191
}
@@ -94,7 +94,7 @@ pub fn handle_process_event(sender: Sender<ProcessEvent>, rx: Receiver<ProcessEv
9494
.pid
9595
.map_or_else(|| "unknown".to_string(), |pid| pid.to_string());
9696
info!(
97-
"The {} service (pid: {}) has be stopped,will stop health watch",
97+
"[{}] (pid: {}) has be stopped,will stop health watch",
9898
received.service_name, pid,
9999
);
100100
health::stop_watch(received.service_name)
@@ -103,7 +103,7 @@ pub fn handle_process_event(sender: Sender<ProcessEvent>, rx: Receiver<ProcessEv
103103
process::status::change_proc_health_status(&received.service_name, false)
104104
.unwrap_or_else(|err| {
105105
warn!(
106-
"change process {} health status failed: {}",
106+
"change [{}] health status failed: {}",
107107
&received.service_name, err
108108
);
109109
});
@@ -112,7 +112,7 @@ pub fn handle_process_event(sender: Sender<ProcessEvent>, rx: Receiver<ProcessEv
112112
process::status::change_proc_health_status(&received.service_name, true)
113113
.unwrap_or_else(|err| {
114114
warn!(
115-
"change process {} health status failed: {}",
115+
"change [{}] health status failed: {}",
116116
&received.service_name, err
117117
);
118118
});

src/health.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
process,
55
};
66
use lazy_static::lazy_static;
7-
use log::{debug, info, warn};
7+
use log::{info, warn};
88
use std::{collections::HashMap, sync::RwLock, thread, time::Duration};
99

1010
lazy_static! {
@@ -14,7 +14,7 @@ lazy_static! {
1414
pub fn start_watch(service_name: String, config: Option<HealthCheckConfig>) {
1515
let health_cfg = config.clone(); // to avoid borrow count
1616
if health_cfg.is_none() || !health_cfg.unwrap().enable {
17-
info!("service {} is not enabled to health check", &service_name);
17+
info!("[{}] is not enabled to health check", &service_name);
1818
return;
1919
}
2020
if is_watching(&service_name) {
@@ -28,10 +28,7 @@ pub fn start_watch(service_name: String, config: Option<HealthCheckConfig>) {
2828
pub fn stop_watch(service_name: String) {
2929
let mut status = SERVICES_HEALTH_STATUS.write().unwrap();
3030
if !status.contains_key(&service_name) {
31-
warn!(
32-
"The service {} is not being watched, ignore stop",
33-
&service_name
34-
);
31+
warn!("[{}] is not being watched, ignore stop", &service_name);
3532
return;
3633
}
3734
status.remove(&service_name);
@@ -54,11 +51,11 @@ fn do_watch_health(service_name: String, config: HealthCheckConfig) {
5451
if !is_watching(&service_name) {
5552
return;
5653
}
57-
info!("service {} has enabled health checks", &service_name);
54+
info!("[{}] has enabled health checks", &service_name);
5855
loop {
5956
if !is_watching(&service_name) {
6057
info!(
61-
"service {} is not being watched, stop health check",
58+
"[{}] is not being watched, stop health check",
6259
&service_name
6360
);
6461
break;
@@ -73,9 +70,9 @@ fn do_watch_health(service_name: String, config: HealthCheckConfig) {
7370
let fail_times = incr_fail_times(&service_name);
7471
let restart: bool = fail_times > config.max_failures;
7572
if restart {
76-
warn!("health check failure count for service {} has exceeded the threshold, preparing to restart it", &service_name);
73+
warn!("health check failure count for [{}] has exceeded the threshold, preparing to restart it", &service_name);
7774
process::manager::restart_service(&service_name).unwrap_or_else(|err| {
78-
warn!("restart service {} failed: {}", &service_name, err);
75+
warn!("restart [{}] failed: {}", &service_name, err);
7976
});
8077
check_interval += config.check_delay.unwrap_or(0);
8178
}

src/process/manager.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn start_services(services: Vec<String>) -> Result<()> {
3030
if dep_ok {
3131
start_service(name)?;
3232
} else {
33-
info!("service [{}] has dependencies, add to pending list", name);
33+
info!("[{}] has dependencies, add to pending list", name);
3434
let deps = service_info.unwrap().config.depends_on.clone().unwrap();
3535
pending::add_pending_service(name, deps)
3636
}
@@ -47,7 +47,7 @@ pub fn start_service(service_name: &str) -> Result<()> {
4747
let pid_val = pid.unwrap();
4848
if status::is_running_by_pid(pid_val) {
4949
info!(
50-
"service [{}] is already running with pid {}!",
50+
"[{}] is already running with pid {}!",
5151
service_name, pid_val
5252
);
5353
event::send_process_event(service_name, EventType::Running, None, pid);
@@ -56,7 +56,7 @@ pub fn start_service(service_name: &str) -> Result<()> {
5656
}
5757
thread::spawn(move || {
5858
if let Err(err) = spawn_proc(Arc::clone(&conf)) {
59-
error!("service [{}] exited with error: {}", svc_name, err);
59+
error!("[{}] exited with error: {}", svc_name, err);
6060
}
6161
});
6262
Ok(())
@@ -76,7 +76,7 @@ pub fn stop_service(service_name: &str) -> Result<()> {
7676
let proc_runtime = status::find_readonly_proc_runtime(service_name)?;
7777
let pid = proc_runtime.pid;
7878
if pid.is_none() {
79-
info!("service [{}] is not running!", service_name);
79+
info!("[{}] is not running!", service_name);
8080
return Ok(());
8181
}
8282
let pid_val = pid.unwrap();
@@ -87,12 +87,12 @@ pub fn stop_service(service_name: &str) -> Result<()> {
8787
})?;
8888
if !is_running {
8989
info!(
90-
"ignore stop command, service [{}] is not running, pid: {}!",
90+
"ignore stop command, [{}] is not running, pid: {}!",
9191
service_name, pid_val
9292
);
9393
return Ok(());
9494
}
95-
info!("service [{}] (pid: {}) is stopping", service_name, pid_val);
95+
info!("[{}] (pid: {}) is stopping", service_name, pid_val);
9696
//首先尝试通过信号量的方式让进程自己退出
9797
if let Err(err) = terminate_process(pid_val) {
9898
warn!("signal {} (pid: {}) failed: {}", service_name, pid_val, err);
@@ -105,7 +105,7 @@ pub fn stop_service(service_name: &str) -> Result<()> {
105105
}
106106
//如果超过规定时间进程没有退出,则强制杀掉进程
107107
if is_running {
108-
info!("service [{}] (pid: {}) is still running within the specified time after sending the interrupt signal, and is ready to be killed", service_name, pid_val);
108+
info!("[{}] (pid: {}) is still running within the specified time after sending the interrupt signal, and is ready to be killed", service_name, pid_val);
109109
kill_process(pid_val)?;
110110
}
111111
Ok(())
@@ -151,7 +151,7 @@ fn spawn_proc(conf: Arc<ServiceConfig>) -> Result<()> {
151151
}
152152
if command.starts_with(".") {}
153153
before_exec(&mut cmd)?;
154-
debug!("execute service [{}] start command:{}", svc_name, command);
154+
debug!("execute [{}] start command:{}", svc_name, command);
155155
let child = cmd.spawn().map_err(|e| format!("{}", e));
156156
match child {
157157
Ok(mut child_proc) => {

0 commit comments

Comments
 (0)