refactor(core): migrate session_manager, email, rpc from slog to tracing

- session_manager/manager.rs: remove slog::Logger field, update new()
  and with_config() to remove log parameter
- email/lib.rs: remove slog::Logger from AppEmail::init()
- rpc/admin/server.rs: remove slog::Logger from serve() and spawn(),
  replace with tracing::info!/error!
This commit is contained in:
ZhenYi 2026-04-21 22:29:43 +08:00
parent 0c1a9ddf98
commit e99feb236b
4 changed files with 22 additions and 33 deletions

View File

@ -21,6 +21,6 @@ tokio = { workspace = true, features = ["rt-multi-thread", "rt", "sync", "macros
serde = { workspace = true, features = ["derive"] }
anyhow = { workspace = true }
regex = { workspace = true }
slog = { workspace = true }
tracing = { workspace = true }
[lints]
workspace = true

View File

@ -24,7 +24,7 @@ pub struct AppEmail {
}
impl AppEmail {
pub async fn init(cfg: &AppConfig, logs: slog::Logger) -> anyhow::Result<Self> {
pub async fn init(cfg: &AppConfig) -> anyhow::Result<Self> {
let smtp_host = cfg.smtp_host()?;
let smtp_port = cfg.smtp_port()?;
let smtp_username = cfg.smtp_username()?;
@ -75,7 +75,7 @@ impl AppEmail {
{
Ok(e) => e,
Err(_) => {
slog::warn!(logs, "Email build error: to={}", msg.to);
tracing::warn!(to = %msg.to, "Email build error");
continue;
}
};
@ -92,24 +92,19 @@ impl AppEmail {
}
Ok(Err(e)) => {
if i == 2 {
slog::error!(
logs,
"Email send failed after retries: to={}, error={}",
msg.to,
e
);
tracing::error!(to = %msg.to, error = %e, "Email send failed after retries");
}
tokio::time::sleep(Duration::from_secs((1 << i) as u64)).await;
}
Err(e) => {
slog::error!(logs, "Email spawn error: to={}, err={}", msg.to, e);
tracing::error!(to = %msg.to, error = %e, "Email spawn error");
break;
}
}
}
if !success {
slog::warn!(logs, "Email send permanently failed: to={}", msg.to);
tracing::warn!(to = %msg.to, "Email send permanently failed");
}
}
});

View File

@ -1,7 +1,6 @@
//! Tonic gRPC server implementation for SessionAdmin service.
use session_manager::SessionManager;
use slog::Logger;
use std::net::SocketAddr;
use tokio::sync::broadcast;
use tonic::{transport::Server, Request, Response, Status};
@ -181,13 +180,12 @@ pub const DEFAULT_GRPC_PORT: u16 = 9090;
pub async fn serve(
addr: SocketAddr,
session_manager: SessionManager,
log: Logger,
) -> anyhow::Result<()> {
let service = SessionAdminService::new(session_manager);
let incoming = tonic::transport::server::TcpIncoming::bind(addr)
.map_err(|e| anyhow::anyhow!("failed to bind TcpIncoming: {}", e))?;
slog::info!(log, "Admin gRPC server listening on {}", addr);
tracing::info!(addr = %addr, "Admin gRPC server listening");
Server::builder()
.add_service(SessionAdminServer::new(service))
@ -201,13 +199,12 @@ pub async fn serve(
pub fn spawn(
addr: SocketAddr,
session_manager: SessionManager,
log: Logger,
mut shutdown_rx: broadcast::Receiver<()>,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let result = serve(addr, session_manager, log).await;
let result = serve(addr, session_manager).await;
if let Err(e) = result {
eprintln!("Admin gRPC server error: {}", e);
tracing::error!(error = %e, "Admin gRPC server error");
}
let _ = shutdown_rx.recv().await;

View File

@ -3,7 +3,6 @@ use uuid::Uuid;
use crate::storage::{SessionStorage, SessionStorageError};
use crate::types::{OnlineStatus, SessionInfo, UserSession};
use slog::info;
#[derive(Debug, Clone)]
pub struct SessionManagerConfig {
@ -25,27 +24,23 @@ pub struct SessionManager {
storage: SessionStorage,
#[allow(dead_code)]
config: SessionManagerConfig,
logger: slog::Logger,
}
impl SessionManager {
pub fn new(storage: SessionStorage, logger: slog::Logger) -> Self {
pub fn new(storage: SessionStorage) -> Self {
Self {
storage,
config: SessionManagerConfig::default(),
logger,
}
}
pub fn with_config(
storage: SessionStorage,
config: SessionManagerConfig,
logger: slog::Logger,
) -> Self {
Self {
storage,
config,
logger,
}
}
@ -69,10 +64,11 @@ impl SessionManager {
};
self.storage.save_session(&session).await?;
info!(self.logger, "session_registered";
"session_id" => %session.session_id,
"user_id" => %session.user_id,
"workspace_id" => %session.workspace_id
tracing::info!(
session_id = %session.session_id,
user_id = %session.user_id,
workspace_id = %session.workspace_id,
"session_registered"
);
Ok(session)
}
@ -85,7 +81,7 @@ impl SessionManager {
/// Remove a single session (logout from one device/tab).
pub async fn remove_session(&self, session_id: &Uuid) -> Result<(), SessionStorageError> {
self.storage.delete_session(session_id).await?;
info!(self.logger, "session_removed"; "session_id" => %session_id);
tracing::info!(session_id = %session_id, "session_removed");
Ok(())
}
@ -100,10 +96,11 @@ impl SessionManager {
.delete_user_workspace_sessions(user_id, workspace_id)
.await?;
let count = deleted.len();
info!(self.logger, "user_kicked_from_workspace";
"user_id" => %user_id,
"workspace_id" => %workspace_id,
"sessions_removed" => count
tracing::info!(
user_id = %user_id,
workspace_id = %workspace_id,
sessions_removed = count,
"user_kicked_from_workspace"
);
Ok(count)
}
@ -112,7 +109,7 @@ impl SessionManager {
pub async fn kick_user(&self, user_id: &Uuid) -> Result<usize, SessionStorageError> {
let deleted = self.storage.delete_user_sessions(user_id).await?;
let count = deleted.len();
info!(self.logger, "user_kicked"; "user_id" => %user_id, "sessions_removed" => count);
tracing::info!(user_id = %user_id, sessions_removed = count, "user_kicked");
Ok(count)
}