fix(log):replace all println! with log

This commit is contained in:
dolzhuying 2025-04-15 10:10:04 +08:00 committed by Samuel Dai
parent a671480b6f
commit 8c4107a8b4
12 changed files with 45 additions and 40 deletions

2
Cargo.lock generated
View File

@ -848,6 +848,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"dotenv", "dotenv",
"env_logger",
"log",
"my-workspace-hack", "my-workspace-hack",
"provider", "provider",
"serde 1.0.217", "serde 1.0.217",

View File

@ -12,3 +12,5 @@ serde_json = "1.0"
my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" } my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" }
provider = { path = "../provider" } provider = { path = "../provider" }
dotenv = "0.15" dotenv = "0.15"
env_logger = "0.10"
log = "0.4.27"

View File

@ -1,16 +1,17 @@
use std::sync::Arc; use std::sync::Arc;
use actix_web::{App, HttpServer, web}; use actix_web::{App, HttpServer, web};
use service::Service;
use provider::{ use provider::{
handlers::{delete::delete_handler, deploy::deploy_handler, invoke_resolver::InvokeResolver}, handlers::{delete::delete_handler, deploy::deploy_handler, invoke_resolver::InvokeResolver},
proxy::proxy_handler::proxy_handler, proxy::proxy_handler::proxy_handler,
types::config::FaaSConfig, types::config::FaaSConfig,
}; };
use service::Service;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let service = Arc::new( let service = Arc::new(
Service::new("/run/containerd/containerd.sock") Service::new("/run/containerd/containerd.sock")
.await .await
@ -20,7 +21,7 @@ async fn main() -> std::io::Result<()> {
let resolver = Some(InvokeResolver::new(service.clone()).await); let resolver = Some(InvokeResolver::new(service.clone()).await);
let faas_config = FaaSConfig::new(); let faas_config = FaaSConfig::new();
println!("I'm running!"); log::info!("I'm running!");
let server = HttpServer::new(move || { let server = HttpServer::new(move || {
App::new() App::new()
@ -34,7 +35,7 @@ async fn main() -> std::io::Result<()> {
}) })
.bind("0.0.0.0:8090")?; .bind("0.0.0.0:8090")?;
println!("0.0.0.0:8090"); log::info!("Running on 0.0.0.0:8090...");
server.run().await server.run().await
} }
@ -49,9 +50,9 @@ mod tests {
let bin = std::env::var("CNI_BIN_DIR").unwrap_or_else(|_| "Not set".to_string()); let bin = std::env::var("CNI_BIN_DIR").unwrap_or_else(|_| "Not set".to_string());
let conf = std::env::var("CNI_CONF_DIR").unwrap_or_else(|_| "Not set".to_string()); let conf = std::env::var("CNI_CONF_DIR").unwrap_or_else(|_| "Not set".to_string());
let tool = std::env::var("CNI_TOOL").unwrap_or_else(|_| "Not set".to_string()); let tool = std::env::var("CNI_TOOL").unwrap_or_else(|_| "Not set".to_string());
println!("CNI_BIN_DIR: {bin}"); log::debug!("CNI_BIN_DIR: {bin}");
println!("CNI_CONF_DIR: {conf}"); log::debug!("CNI_CONF_DIR: {conf}");
println!("CNI_TOOL: {tool}"); log::debug!("CNI_TOOL: {tool}");
// for (key, value) in &result { // for (key, value) in &result {
// println!("{}={}", key, value); // println!("{}={}", key, value);
// } // }

View File

@ -28,7 +28,7 @@ service = { path = "../service" }
cni = { path = "../cni" } cni = { path = "../cni" }
async-trait = "0.1" async-trait = "0.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4" log = "0.4.27"
my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" } my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" }
url = "2.4" url = "2.4"
derive_more = { version = "2", features = ["full"] } derive_more = { version = "2", features = ["full"] }

View File

@ -42,10 +42,11 @@ async fn delete(
} }
let function = get_function(service, function_name, namespace).await?; let function = get_function(service, function_name, namespace).await?;
if function.replicas != 0 { if function.replicas != 0 {
println!(" delete_cni_network ing {:?}", function.replicas); log::info!("function.replicas: {:?}", function.replicas);
cni::delete_cni_network(namespace, function_name); cni::delete_cni_network(namespace, function_name);
log::info!("delete_cni_network ok");
} else { } else {
println!(" function.replicas {:?}", function.replicas); log::info!("function.replicas: {:?}", function.replicas);
} }
service service
.remove_container(function_name, namespace) .remove_container(function_name, namespace)

View File

@ -50,7 +50,7 @@ async fn deploy(service: &Arc<Service>, config: &FunctionDeployment) -> Result<(
// namespace // namespace
// )))); // ))));
// } // }
println!( log::info!(
"Namespace '{}' validated.", "Namespace '{}' validated.",
config.namespace.clone().unwrap() config.namespace.clone().unwrap()
); );
@ -71,16 +71,18 @@ async fn deploy(service: &Arc<Service>, config: &FunctionDeployment) -> Result<(
ImageManager::prepare_image(client, &config.image, &namespace, true) ImageManager::prepare_image(client, &config.image, &namespace, true)
.await .await
.map_err(CustomError::from)?; .map_err(CustomError::from)?;
println!("Image '{}' validated", &config.image); log::info!("Image '{}' validated ,", &config.image);
service service
.create_container(&config.image, &config.service, &namespace) .create_container(&config.image, &config.service, &namespace)
.await .await
.map_err(|e| CustomError::OtherError(format!("failed to create container:{}", e)))?; .map_err(|e| CustomError::OtherError(format!("failed to create container:{}", e)))?;
println!( log::info!(
"Container {} created using image {} in namespace {}", "Container {} created using image {} in namespace {}",
&config.service, &config.image, namespace &config.service,
&config.image,
namespace
); );
service service
@ -92,7 +94,7 @@ async fn deploy(service: &Arc<Service>, config: &FunctionDeployment) -> Result<(
&config.service, e &config.service, e
)) ))
})?; })?;
println!( log::info!(
"Task for container {} was created successfully", "Task for container {} was created successfully",
&config.service &config.service
); );

View File

@ -64,7 +64,7 @@ pub async fn get_function(
} }
} }
Err(e) => { Err(e) => {
eprintln!("Failed to get task: {}", e); log::error!("Failed to get task: {}", e);
replicas = 0; replicas = 0;
} }
} }

View File

@ -33,11 +33,9 @@ impl InvokeResolver {
return Err(ErrorInternalServerError("Failed to get function")); return Err(ErrorInternalServerError("Failed to get function"));
} }
}; };
log::info!("Function:{:?}", function);
//容器启动后的port?
let address = function.address.clone(); let address = function.address.clone();
println!("function: {:?}", function);
let urlstr = format!("http://{}", address); let urlstr = format!("http://{}", address);
match Url::parse(&urlstr) { match Url::parse(&urlstr) {
Ok(url) => Ok(url), Ok(url) => Ok(url),

View File

@ -4,7 +4,7 @@ use derive_more::Display;
use service::image_manager::ImageError; use service::image_manager::ImageError;
pub fn map_service_error(e: Box<dyn std::error::Error>) -> Error { pub fn map_service_error(e: Box<dyn std::error::Error>) -> Error {
eprintln!("Service error: {}", e); log::error!("Service error: {}", e);
actix_web::error::ErrorInternalServerError(format!("Operationfailed: {}", e)) actix_web::error::ErrorInternalServerError(format!("Operationfailed: {}", e))
} }

View File

@ -19,7 +19,7 @@ pub async fn proxy_handler(
.expect("empty proxy handler resolver, cannot be nil"); .expect("empty proxy handler resolver, cannot be nil");
let proxy_client = new_proxy_client_from_config(config.as_ref()).await; let proxy_client = new_proxy_client_from_config(config.as_ref()).await;
println!("proxy_client : {:?}", proxy_client); log::info!("proxy_client : {:?}", proxy_client);
match *req.method() { match *req.method() {
Method::POST Method::POST

View File

@ -1,3 +1,2 @@
pub mod config; pub mod config;
pub mod function_deployment; pub mod function_deployment;

View File

@ -146,11 +146,11 @@ impl Service {
}; };
let mut cc = self.client.containers(); let mut cc = self.client.containers();
let responce = cc let response = cc
.list(with_namespace!(request, namespace)) .list(with_namespace!(request, namespace))
.await? .await?
.into_inner(); .into_inner();
let container = responce let container = response
.containers .containers
.iter() .iter()
.find(|container| container.id == cid); .find(|container| container.id == cid);
@ -161,15 +161,15 @@ impl Service {
let request = ListTasksRequest { let request = ListTasksRequest {
filter: format!("container=={}", cid), filter: format!("container=={}", cid),
}; };
let responce = tc let response = tc
.list(with_namespace!(request, namespace)) .list(with_namespace!(request, namespace))
.await? .await?
.into_inner(); .into_inner();
println!("Tasks: {:?}", responce.tasks); log::info!("Tasks: {:?}", response.tasks);
drop(tc); drop(tc);
if let Some(task) = responce.tasks.iter().find(|task| task.id == container.id) { if let Some(task) = response.tasks.iter().find(|task| task.id == container.id) {
println!("Task found: {}, Status: {}", task.id, task.status); log::info!("Task found: {}, Status: {}", task.id, task.status);
// TASK_UNKNOWN (0) — 未知状态 // TASK_UNKNOWN (0) — 未知状态
// TASK_CREATED (1) — 任务已创建 // TASK_CREATED (1) — 任务已创建
// TASK_RUNNING (2) — 任务正在运行 // TASK_RUNNING (2) — 任务正在运行
@ -192,7 +192,7 @@ impl Service {
//todo 这里删除cni? //todo 这里删除cni?
self.remove_netns_ip(cid).await; self.remove_netns_ip(cid).await;
println!("Container: {:?} deleted", cc); log::info!("Container: {:?} deleted", cc);
} else { } else {
todo!("Container not found"); todo!("Container not found");
} }
@ -227,17 +227,17 @@ impl Service {
.into_inner() .into_inner()
.mounts; .mounts;
println!("mounts ok"); log::info!("mounts ok");
drop(sc); drop(sc);
println!("drop sc ok"); log::info!("drop sc ok");
let _ = cni::init_net_work(); let _ = cni::init_net_work();
println!("init_net_work ok"); log::info!("init_net_work ok");
let (ip, path) = cni::create_cni_network(cid.to_string(), ns.to_string())?; let (ip, path) = cni::create_cni_network(cid.to_string(), ns.to_string())?;
let ports = ImageManager::get_runtime_config(img_name).unwrap().ports; let ports = ImageManager::get_runtime_config(img_name).unwrap().ports;
let network_config = NetworkConfig::new(path, ip, ports); let network_config = NetworkConfig::new(path, ip, ports);
println!("create_cni_network ok"); log::info!("create_cni_network ok");
self.save_network_config(cid, network_config.clone()).await; self.save_network_config(cid, network_config.clone()).await;
println!("save_netns_ip ok, netconfig: {:?}", network_config); log::info!("save_netns_ip ok, netconfig: {:?}", network_config);
let mut tc = self.client.tasks(); let mut tc = self.client.tasks();
let req = CreateTaskRequest { let req = CreateTaskRequest {
container_id: cid.to_string(), container_id: cid.to_string(),
@ -255,7 +255,7 @@ impl Service {
..Default::default() ..Default::default()
}; };
let _resp = self.client.tasks().start(with_namespace!(req, ns)).await?; let _resp = self.client.tasks().start(with_namespace!(req, ns)).await?;
println!("Task: {:?} started", cid); log::info!("Task: {:?} started", cid);
Ok(()) Ok(())
} }
@ -299,7 +299,7 @@ impl Service {
Ok::<(), Err>(()) Ok::<(), Err>(())
}) })
.await; .await;
println!(" after wait"); log::info!("after wait");
let kill_request = KillRequest { let kill_request = KillRequest {
container_id: cid.to_string(), container_id: cid.to_string(),
@ -324,17 +324,17 @@ impl Service {
// println!("Task: {:?} deleted", cid); // println!("Task: {:?} deleted", cid);
match c.delete(with_namespace!(req, namespace)).await { match c.delete(with_namespace!(req, namespace)).await {
Ok(_) => { Ok(_) => {
println!("Task: {:?} deleted", cid); log::info!("Task: {:?} deleted", cid);
Ok(()) Ok(())
} }
Err(e) => { Err(e) => {
eprintln!("Failed to delete task: {}", e); log::error!("Failed to delete task: {}", e);
Err(e.into()) Err(e.into())
} }
} }
} }
Ok(Err(e)) => { Ok(Err(e)) => {
eprintln!("Wait task failed: {}", e); log::error!("Wait task failed: {}", e);
Err(e) Err(e)
} }
Err(_) => { Err(_) => {
@ -346,11 +346,11 @@ impl Service {
}; };
match c.kill(with_namespace!(kill_request, namespace)).await { match c.kill(with_namespace!(kill_request, namespace)).await {
Ok(_) => { Ok(_) => {
println!("Task: {:?} force killed", cid); log::info!("Task: {:?} force killed", cid);
Ok(()) Ok(())
} }
Err(e) => { Err(e) => {
eprintln!("Failed to force kill task: {}", e); log::error!("Failed to force kill task: {}", e);
Err(e.into()) Err(e.into())
} }
} }