Spaces:
Running
Running
File size: 10,140 Bytes
b5e1f18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
// logger.js - 统一的日志系统模块
const fs = require('fs');
const path = require('path');
// 避免循环依赖
let config = null;
// 延迟加载配置
function getConfig() {
if (!config) {
try {
config = require('../config/config');
} catch (err) {
console.error('加载配置文件失败:', err.message);
config = { log: { level: 'INFO', format: 'colored' } };
}
}
return config;
}
const LOG_LEVELS = {
ERROR: 0,
WARN: 1,
INFO: 2,
DEBUG: 3,
TRACE: 4,
HTTP: 2 // HTTP日志级别与INFO相同
};
// 默认日志级别
let currentLogLevel = LOG_LEVELS.INFO;
// 日志格式
let logFormat = 'colored'; // colored, json, text
// 带颜色的控制台输出
const COLORS = {
RESET: '\x1b[0m',
RED: '\x1b[31m',
YELLOW: '\x1b[33m',
GREEN: '\x1b[32m',
BLUE: '\x1b[34m',
CYAN: '\x1b[36m'
};
// 日志文件配置
const LOG_DIR = path.join(__dirname, '../../logs');
const LOG_FILE = path.join(LOG_DIR, 'app.log');
const MAX_LOG_SIZE = 10 * 1024 * 1024; // 10MB
let logToFile = false;
// 内存中存储的日志(用于网页显示)
const memoryLogs = [];
const MAX_MEMORY_LOGS = 1000; // 内存中最多保存的日志条数
// 确保日志目录存在
function ensureLogDirExists() {
try {
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR, { recursive: true });
}
return true;
} catch (err) {
console.error(`创建日志目录失败: ${err.message}`);
return false;
}
}
// 初始化文件日志
function initFileLogging() {
const conf = getConfig();
if (process.env.LOG_TO_FILE === 'true' || (conf.log && conf.log.toFile)) {
if (ensureLogDirExists()) {
logToFile = true;
// 检查日志文件大小,如果超过最大值则进行轮转
if (fs.existsSync(LOG_FILE)) {
const stats = fs.statSync(LOG_FILE);
if (stats.size > MAX_LOG_SIZE) {
rotateLogFile();
}
}
return true;
}
}
return false;
}
// 日志文件轮转
function rotateLogFile() {
try {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const newLogFile = path.join(LOG_DIR, `app-${timestamp}.log`);
if (fs.existsSync(LOG_FILE)) {
fs.renameSync(LOG_FILE, newLogFile);
}
// 清理旧日志文件,保留最近10个
const logFiles = fs.readdirSync(LOG_DIR)
.filter(file => file.startsWith('app-') && file.endsWith('.log'))
.sort()
.reverse();
if (logFiles.length > 10) {
logFiles.slice(10).forEach(file => {
try {
fs.unlinkSync(path.join(LOG_DIR, file));
} catch (err) {
console.error(`删除旧日志文件失败: ${err.message}`);
}
});
}
} catch (err) {
console.error(`日志文件轮转失败: ${err.message}`);
logToFile = false;
}
}
// 添加日志到内存
function addLogToMemory(level, timestamp, ...args) {
// 将日志对象添加到内存数组
const logEntry = {
level,
timestamp,
message: args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg)).join(' ')
};
memoryLogs.unshift(logEntry); // 新日志添加到数组开头
// 保持数组在最大长度以内
if (memoryLogs.length > MAX_MEMORY_LOGS) {
memoryLogs.pop(); // 移除最旧的日志
}
}
// 将日志写入文件
function writeLogToFile(level, timestamp, ...args) {
if (!logToFile) return;
try {
let logEntry;
if (logFormat === 'json') {
// JSON格式
const data = args.map(arg => typeof arg === 'object' ? arg : String(arg));
const logObject = {
level,
timestamp,
message: data.length === 1 ? data[0] : data
};
logEntry = JSON.stringify(logObject) + '\n';
} else {
// 文本格式
logEntry = `[${level}] ${timestamp} ${args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : arg
).join(' ')}\n`;
}
fs.appendFileSync(LOG_FILE, logEntry);
// 检查文件大小,必要时进行轮转
const stats = fs.statSync(LOG_FILE);
if (stats.size > MAX_LOG_SIZE) {
rotateLogFile();
}
} catch (err) {
console.error(`写入日志文件失败: ${err.message}`);
logToFile = false;
}
}
// 获取时间戳
function getTimestamp() {
return new Date().toISOString();
}
// 设置日志级别
function setLogLevel(level) {
if (typeof level === 'string') {
level = level.toUpperCase();
if (LOG_LEVELS[level] !== undefined) {
currentLogLevel = LOG_LEVELS[level];
} else {
error(`无效的日志级别: ${level}`);
}
} else if (typeof level === 'number' && level >= 0 && level <= 4) {
currentLogLevel = level;
} else {
error(`无效的日志级别: ${level}`);
}
}
// 设置日志格式
function setLogFormat(format) {
const validFormats = ['colored', 'json', 'text'];
if (validFormats.includes(format)) {
logFormat = format;
return true;
} else {
error(`无效的日志格式: ${format}`);
return false;
}
}
// 格式化控制台日志
function formatConsoleLog(level, timestamp, color, ...args) {
if (logFormat === 'json') {
// JSON格式
const data = args.map(arg => typeof arg === 'object' ? arg : String(arg));
return JSON.stringify({
level,
timestamp,
message: data.length === 1 ? data[0] : data
});
} else if (logFormat === 'text') {
// 纯文本格式(无颜色)
return `[${level}] ${timestamp} ${args.join(' ')}`;
} else {
// 默认:带颜色格式
return `${color}[${level}] ${timestamp}${COLORS.RESET} ${args.join(' ')}`;
}
}
// 错误日志
function error(...args) {
if (currentLogLevel >= LOG_LEVELS.ERROR) {
const timestamp = getTimestamp();
const formattedLog = formatConsoleLog('ERROR', timestamp, COLORS.RED, ...args);
console.error(formattedLog);
writeLogToFile('ERROR', timestamp, ...args);
addLogToMemory('ERROR', timestamp, ...args);
}
}
// 警告日志
function warn(...args) {
if (currentLogLevel >= LOG_LEVELS.WARN) {
const timestamp = getTimestamp();
const formattedLog = formatConsoleLog('WARN', timestamp, COLORS.YELLOW, ...args);
console.warn(formattedLog);
writeLogToFile('WARN', timestamp, ...args);
addLogToMemory('WARN', timestamp, ...args);
}
}
// 信息日志
function info(...args) {
if (currentLogLevel >= LOG_LEVELS.INFO) {
const timestamp = getTimestamp();
const formattedLog = formatConsoleLog('INFO', timestamp, COLORS.GREEN, ...args);
console.log(formattedLog);
writeLogToFile('INFO', timestamp, ...args);
addLogToMemory('INFO', timestamp, ...args);
}
}
// 调试日志
function debug(...args) {
if (currentLogLevel >= LOG_LEVELS.DEBUG) {
const timestamp = getTimestamp();
const formattedLog = formatConsoleLog('DEBUG', timestamp, COLORS.BLUE, ...args);
console.log(formattedLog);
writeLogToFile('DEBUG', timestamp, ...args);
addLogToMemory('DEBUG', timestamp, ...args);
}
}
// 跟踪日志
function trace(...args) {
if (currentLogLevel >= LOG_LEVELS.TRACE) {
const timestamp = getTimestamp();
const formattedLog = formatConsoleLog('TRACE', timestamp, COLORS.CYAN, ...args);
console.log(formattedLog);
writeLogToFile('TRACE', timestamp, ...args);
addLogToMemory('TRACE', timestamp, ...args);
}
}
// HTTP请求日志 (特殊处理,方便筛选)
function http(...args) {
if (currentLogLevel >= LOG_LEVELS.INFO) {
const timestamp = getTimestamp();
const formattedLog = formatConsoleLog('HTTP', timestamp, COLORS.CYAN, ...args);
console.log(formattedLog);
writeLogToFile('HTTP', timestamp, ...args);
addLogToMemory('HTTP', timestamp, ...args);
}
}
// 获取内存中的日志
function getLogs(filter = {}) {
let filteredLogs = [...memoryLogs];
// 按日志级别筛选
if (filter.level) {
filteredLogs = filteredLogs.filter(log => log.level === filter.level);
}
// 按时间范围筛选
if (filter.startTime) {
filteredLogs = filteredLogs.filter(log => new Date(log.timestamp) >= new Date(filter.startTime));
}
if (filter.endTime) {
filteredLogs = filteredLogs.filter(log => new Date(log.timestamp) <= new Date(filter.endTime));
}
// 按关键词搜索
if (filter.search) {
const searchTerm = filter.search.toLowerCase();
filteredLogs = filteredLogs.filter(log =>
log.message.toLowerCase().includes(searchTerm) ||
log.level.toLowerCase().includes(searchTerm)
);
}
// 分页
const page = filter.page || 1;
const pageSize = filter.pageSize || 100;
const start = (page - 1) * pageSize;
const end = start + pageSize;
return {
logs: filteredLogs.slice(start, end),
total: filteredLogs.length,
page,
pageSize
};
}
// 清除内存日志
function clearMemoryLogs() {
memoryLogs.length = 0;
info('内存日志已清除');
}
// 初始化配置
function initialize() {
try {
const conf = getConfig();
// 初始化日志级别
const envLevel = process.env.LOG_LEVEL;
if (envLevel) {
setLogLevel(envLevel);
} else if (conf && conf.log && conf.log.level) {
setLogLevel(conf.log.level);
}
// 初始化日志格式
const envFormat = process.env.LOG_FORMAT;
if (envFormat) {
setLogFormat(envFormat);
} else if (conf && conf.log && conf.log.format) {
setLogFormat(conf.log.format);
}
// 初始化文件日志
initFileLogging();
} catch (err) {
console.error(`初始化日志系统出错: ${err.message}`);
}
}
// 初始化
initialize();
module.exports = {
LOG_LEVELS,
setLogLevel,
setLogFormat,
error,
warn,
info,
debug,
trace,
http,
// 暴露文件日志相关方法
enableFileLogging: () => {
if (ensureLogDirExists()) {
logToFile = true;
info('文件日志已启用');
return true;
}
return false;
},
disableFileLogging: () => {
logToFile = false;
info('文件日志已禁用');
},
rotateLogFile,
// 添加内存日志相关方法
getLogs,
clearMemoryLogs
}; |