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"] } serde = { workspace = true, features = ["derive"] }
anyhow = { workspace = true } anyhow = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
slog = { workspace = true } tracing = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View File

@ -24,7 +24,7 @@ pub struct AppEmail {
} }
impl 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_host = cfg.smtp_host()?;
let smtp_port = cfg.smtp_port()?; let smtp_port = cfg.smtp_port()?;
let smtp_username = cfg.smtp_username()?; let smtp_username = cfg.smtp_username()?;
@ -75,7 +75,7 @@ impl AppEmail {
{ {
Ok(e) => e, Ok(e) => e,
Err(_) => { Err(_) => {
slog::warn!(logs, "Email build error: to={}", msg.to); tracing::warn!(to = %msg.to, "Email build error");
continue; continue;
} }
}; };
@ -92,24 +92,19 @@ impl AppEmail {
} }
Ok(Err(e)) => { Ok(Err(e)) => {
if i == 2 { if i == 2 {
slog::error!( tracing::error!(to = %msg.to, error = %e, "Email send failed after retries");
logs,
"Email send failed after retries: to={}, error={}",
msg.to,
e
);
} }
tokio::time::sleep(Duration::from_secs((1 << i) as u64)).await; tokio::time::sleep(Duration::from_secs((1 << i) as u64)).await;
} }
Err(e) => { Err(e) => {
slog::error!(logs, "Email spawn error: to={}, err={}", msg.to, e); tracing::error!(to = %msg.to, error = %e, "Email spawn error");
break; break;
} }
} }
} }
if !success { 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. //! Tonic gRPC server implementation for SessionAdmin service.
use session_manager::SessionManager; use session_manager::SessionManager;
use slog::Logger;
use std::net::SocketAddr; use std::net::SocketAddr;
use tokio::sync::broadcast; use tokio::sync::broadcast;
use tonic::{transport::Server, Request, Response, Status}; use tonic::{transport::Server, Request, Response, Status};
@ -181,13 +180,12 @@ pub const DEFAULT_GRPC_PORT: u16 = 9090;
pub async fn serve( pub async fn serve(
addr: SocketAddr, addr: SocketAddr,
session_manager: SessionManager, session_manager: SessionManager,
log: Logger,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let service = SessionAdminService::new(session_manager); let service = SessionAdminService::new(session_manager);
let incoming = tonic::transport::server::TcpIncoming::bind(addr) let incoming = tonic::transport::server::TcpIncoming::bind(addr)
.map_err(|e| anyhow::anyhow!("failed to bind TcpIncoming: {}", e))?; .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() Server::builder()
.add_service(SessionAdminServer::new(service)) .add_service(SessionAdminServer::new(service))
@ -201,13 +199,12 @@ pub async fn serve(
pub fn spawn( pub fn spawn(
addr: SocketAddr, addr: SocketAddr,
session_manager: SessionManager, session_manager: SessionManager,
log: Logger,
mut shutdown_rx: broadcast::Receiver<()>, mut shutdown_rx: broadcast::Receiver<()>,
) -> tokio::task::JoinHandle<()> { ) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move { tokio::spawn(async move {
let result = serve(addr, session_manager, log).await; let result = serve(addr, session_manager).await;
if let Err(e) = result { if let Err(e) = result {
eprintln!("Admin gRPC server error: {}", e); tracing::error!(error = %e, "Admin gRPC server error");
} }
let _ = shutdown_rx.recv().await; let _ = shutdown_rx.recv().await;

View File

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