prepare(" INSERT INTO audit_logs (id, tenant_id, user_id, action, entity_type, entity_id, old_data, new_data, ip_address, user_agent) VALUES (UUID(), ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $tenantId, $userId, $action, $entityType, $entityId, $oldData ? json_encode($oldData, JSON_UNESCAPED_UNICODE) : null, $newData ? json_encode($newData, JSON_UNESCAPED_UNICODE) : null, $ipAddress, $userAgent, ]); } catch (\Exception $e) { // Audit logging should NEVER crash the main request error_log("[AuditLogger] Failed to log action '{$action}': " . $e->getMessage()); } } }