// 日志模块: 邮件日志 + 系统日志, 写入数据库供后台查看 // 所有写库操作 try/catch 包裹, 日志失败绝不影响主流程 const { query } = require('./db'); // 系统日志: level = info | warn | error async function logSystem(level, source, message, details) { try { await query( 'INSERT INTO system_logs(level, source, message, details) VALUES (?,?,?,?)', [level || 'info', String(source || '').slice(0, 50), String(message || '').slice(0, 2000), details ? JSON.stringify(details).slice(0, 4000) : null] ); } catch { /* 日志失败忽略 */ } } // 邮件日志: status = sent | failed async function logEmail(to, templateCode, subject, status, error) { try { await query( 'INSERT INTO email_logs(to_email, template_code, subject, status, error) VALUES (?,?,?,?,?)', [String(to || '').slice(0, 100), String(templateCode || '').slice(0, 50), String(subject || '').slice(0, 255), status === 'sent' ? 'sent' : 'failed', error ? String(error).slice(0, 2000) : null] ); } catch { /* 日志失败忽略 */ } } module.exports = { logSystem, logEmail };