const fs = require('fs'); const path = require('path'); // Tạo thư mục logs nếu chưa tồn tại const logDir = path.join(__dirname, '../logs'); if (!fs.existsSync(logDir)) { fs.mkdirSync(logDir, { recursive: true }); } const logFilePath = path.join(logDir, 'activity.log'); /** * Ghi log các hoạt động quan trọng vào hệ thống file * @param {string} action - Tên hành động (vd: DELETE_SCENE, ORPHAN_CLEANUP) * @param {object} details - Thông tin chi tiết (ID, số lượng...) * @param {string} performer - Người thực hiện (Username hoặc 'System') */ const logActivity = async (action, details, performer = 'System') => { const timestamp = new Date().toISOString(); const logEntry = `[${timestamp}] [${action.padEnd(20)}] | Performer: ${performer.padEnd(15)} | Details: ${JSON.stringify(details)}\n`; try { // Sử dụng appendFile bất đồng bộ để không chặn luồng xử lý chính await fs.promises.appendFile(logFilePath, logEntry); } catch (err) { // Chỉ log ra console nếu việc ghi file thất bại để tránh làm sập app console.error('[Logger Error]: Không thể ghi log vào file', err); } }; module.exports = { logActivity };