feat: add SLS toggle and sanitize logs
This commit is contained in:
20
server.js
20
server.js
@@ -5,6 +5,7 @@ import router from './routes.js';
|
||||
import { initializeAuth } from './auth.js';
|
||||
import { initializeUserAgentUpdater } from './user-agent-updater.js';
|
||||
import './sls-logger.js'; // 初始化阿里云日志服务
|
||||
import { sanitizeForLog } from './log-sanitizer.js';
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -58,6 +59,11 @@ app.use((req, res, next) => {
|
||||
ip: req.ip || req.connection.remoteAddress
|
||||
};
|
||||
|
||||
const safeQuery = sanitizeForLog(errorInfo.query);
|
||||
const safeBody = sanitizeForLog(errorInfo.body);
|
||||
const safeHeaders = sanitizeForLog(errorInfo.headers);
|
||||
const safeIp = sanitizeForLog(errorInfo.ip);
|
||||
|
||||
console.error('\n' + '='.repeat(80));
|
||||
console.error('❌ 非法请求地址');
|
||||
console.error('='.repeat(80));
|
||||
@@ -67,23 +73,23 @@ app.use((req, res, next) => {
|
||||
console.error(`路径: ${errorInfo.path}`);
|
||||
|
||||
if (Object.keys(errorInfo.query).length > 0) {
|
||||
console.error(`查询参数: ${JSON.stringify(errorInfo.query, null, 2)}`);
|
||||
console.error(`查询参数: ${JSON.stringify(safeQuery, null, 2)}`);
|
||||
}
|
||||
|
||||
if (errorInfo.body && Object.keys(errorInfo.body).length > 0) {
|
||||
console.error(`请求体: ${JSON.stringify(errorInfo.body, null, 2)}`);
|
||||
console.error(`请求体: ${JSON.stringify(safeBody, null, 2)}`);
|
||||
}
|
||||
|
||||
console.error(`客户端IP: ${errorInfo.ip}`);
|
||||
console.error(`User-Agent: ${errorInfo.headers['user-agent'] || 'N/A'}`);
|
||||
console.error(`客户端IP: ${safeIp}`);
|
||||
console.error(`User-Agent: ${safeHeaders['user-agent'] || 'N/A'}`);
|
||||
|
||||
if (errorInfo.headers.referer) {
|
||||
console.error(`来源: ${errorInfo.headers.referer}`);
|
||||
if (safeHeaders.referer) {
|
||||
console.error(`来源: ${safeHeaders.referer}`);
|
||||
}
|
||||
|
||||
console.error('='.repeat(80) + '\n');
|
||||
|
||||
logError('Invalid request path', errorInfo);
|
||||
logError('Invalid request path', sanitizeForLog(errorInfo));
|
||||
|
||||
res.status(404).json({
|
||||
error: 'Not Found',
|
||||
|
||||
Reference in New Issue
Block a user