From 3e0d4c242bd8bccac89eab220c82fa97adccbd0a Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 21 Apr 2026 20:11:59 +0800 Subject: [PATCH] Emit tracing inside record_event and drop duplicate feeder match MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit record_event now logs at the matching tracing level (error/warn/info) using the persisted message — giving every app uniform event logs for free. Feeder's handle_control_event collapses from a 60-line match (which just duplicated the persisted message with less-readable UUIDs) to a single if-let for UnitStateChanged, which is the only AppEvent that is intentionally not persisted. Co-Authored-By: Claude Opus 4.7 --- crates/app_feeder_distributor/src/event.rs | 76 ++++------------------ crates/plc_platform_core/src/event.rs | 7 ++ 2 files changed, 19 insertions(+), 64 deletions(-) diff --git a/crates/app_feeder_distributor/src/event.rs b/crates/app_feeder_distributor/src/event.rs index 60c36b2..d957f37 100644 --- a/crates/app_feeder_distributor/src/event.rs +++ b/crates/app_feeder_distributor/src/event.rs @@ -92,71 +92,19 @@ async fn handle_control_event( pool: &sqlx::PgPool, ws_manager: Option<&std::sync::Arc>, ) { - persist_event_if_needed(&event, pool, ws_manager).await; - - match event { - AppEvent::EquipmentStartCommandSent { - equipment_id, - unit_id, - point_id, - } => { - tracing::info!( - "Equipment start command sent: equipment={}, unit={:?}, point={}", - equipment_id, - unit_id, - point_id - ); - } - AppEvent::EquipmentStopCommandSent { - equipment_id, - unit_id, - point_id, - } => { - tracing::info!( - "Equipment stop command sent: equipment={}, unit={:?}, point={}", - equipment_id, - unit_id, - point_id - ); - } - AppEvent::AutoControlStarted { unit_id } => { - tracing::info!("Auto control started for unit {}", unit_id); - } - AppEvent::AutoControlStopped { unit_id } => { - tracing::info!("Auto control stopped for unit {}", unit_id); - } - AppEvent::FaultLocked { - unit_id, - equipment_id, - } => { - tracing::warn!("Fault locked: unit={}, equipment={}", unit_id, equipment_id); - } - AppEvent::FaultAcked { unit_id } => { - tracing::info!("Fault acked for unit {}", unit_id); - } - AppEvent::CommLocked { unit_id } => { - tracing::warn!("Comm locked for unit {}", unit_id); - } - AppEvent::CommRecovered { unit_id } => { - tracing::info!("Comm recovered for unit {}", unit_id); - } - AppEvent::RemLocal { - unit_id, - equipment_id, - } => { - tracing::warn!("REM local: unit={}, equipment={}", unit_id, equipment_id); - } - AppEvent::RemRecovered { unit_id } => { - tracing::info!("REM recovered for unit {}", unit_id); - } - AppEvent::UnitStateChanged { - unit_id, - from_state, - to_state, - } => { - tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state); - } + // UnitStateChanged is high-frequency and intentionally not persisted; + // it still needs tracing for local observability. All other events are + // persisted via record_event, which emits tracing automatically. + if let AppEvent::UnitStateChanged { + unit_id, + from_state, + to_state, + } = &event + { + tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state); } + + persist_event_if_needed(&event, pool, ws_manager).await; } async fn fetch_unit_code(pool: &sqlx::PgPool, id: Uuid) -> String { diff --git a/crates/plc_platform_core/src/event.rs b/crates/plc_platform_core/src/event.rs index a26ceca..344b1f2 100644 --- a/crates/plc_platform_core/src/event.rs +++ b/crates/plc_platform_core/src/event.rs @@ -64,6 +64,13 @@ pub async fn record_event( event: EventInsert, ) { let event_type = event.event_type; + + match event.level { + "error" => tracing::error!(event_type, "{}", event.message), + "warn" => tracing::warn!(event_type, "{}", event.message), + _ => tracing::info!(event_type, "{}", event.message), + } + let envelope = EventEnvelope::new(event_type, event.payload); let inserted = sqlx::query_as::<_, EventRecord>(