Spaces:
Running
Running
File size: 10,080 Bytes
f120063 f91fed0 f120063 f91fed0 f120063 f91fed0 f120063 f91fed0 f120063 f91fed0 f120063 f91fed0 f120063 | 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 | const fs = require('fs')
const path = require('path')
const paths = require('./paths')
/**
* 日志管理器
* 统一管理项目中的日志输出,支持分级打印、时间戳、Emoji标签等功能
*/
class Logger {
constructor(options = {}) {
this.options = {
// 日志级别: DEBUG < INFO < WARN < ERROR
level: options.level || 'INFO',
// 是否启用文件日志
enableFileLog: options.enableFileLog || false,
// 是否启用运行日志(控制台 + 内存日志)
enableRuntimeLog: options.enableRuntimeLog !== false,
// 日志文件路径
logDir: options.logDir || paths.logDir,
// 日志文件名格式
logFileName: options.logFileName || 'app.log',
// 是否显示时间戳
showTimestamp: options.showTimestamp !== false,
// 是否显示日志级别
showLevel: options.showLevel !== false,
// 是否显示模块名
showModule: options.showModule !== false,
// 时间格式
timeFormat: options.timeFormat || 'YYYY-MM-DD HH:mm:ss',
// 最大日志文件大小 (MB)
maxFileSize: options.maxFileSize || 10,
// 保留的日志文件数量
maxFiles: options.maxFiles || 5
}
// 日志级别映射
this.levels = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3
}
// Emoji 标签映射
this.emojis = {
DEBUG: '🔍',
INFO: '📝',
WARN: '⚠️',
ERROR: '❌',
SUCCESS: '✅',
NETWORK: '🌐',
DATABASE: '🗄️',
AUTH: '🔐',
UPLOAD: '📤',
DOWNLOAD: '📥',
CACHE: '💾',
CONFIG: '⚙️',
SERVER: '🚀',
CLIENT: '👤',
REDIS: '🔴',
TOKEN: '🎫',
SEARCH: '🔍',
CHAT: '💬',
MODEL: '🤖',
FILE: '📁',
TIME: '⏰',
MEMORY: '🧠',
PROCESS: '⚡'
}
// 颜色代码
this.colors = {
DEBUG: '\x1b[36m', // 青色
INFO: '\x1b[32m', // 绿色
WARN: '\x1b[33m', // 黄色
ERROR: '\x1b[31m', // 红色
RESET: '\x1b[0m', // 重置
BRIGHT: '\x1b[1m', // 加粗
DIM: '\x1b[2m' // 暗淡
}
// 初始化日志目录
if (this.options.enableFileLog) {
this.initLogDirectory()
}
this.memoryLogs = []
this.maxMemoryLogs = options.maxMemoryLogs || 500
}
/**
* 初始化日志目录
*/
initLogDirectory() {
try {
if (!fs.existsSync(this.options.logDir)) {
fs.mkdirSync(this.options.logDir, { recursive: true })
}
} catch (error) {
console.error('创建日志目录失败:', error.message)
}
}
/**
* 检查日志级别是否应该输出
* @param {string} level - 日志级别
* @returns {boolean}
*/
shouldLog(level) {
return this.options.enableRuntimeLog && this.levels[level] >= this.levels[this.options.level]
}
/**
* 格式化时间戳
* @returns {string}
*/
formatTimestamp() {
const now = new Date()
const year = now.getFullYear()
const month = String(now.getMonth() + 1).padStart(2, '0')
const day = String(now.getDate()).padStart(2, '0')
const hours = String(now.getHours()).padStart(2, '0')
const minutes = String(now.getMinutes()).padStart(2, '0')
const seconds = String(now.getSeconds()).padStart(2, '0')
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
/**
* 格式化日志消息
* @param {string} level - 日志级别
* @param {string} message - 日志消息
* @param {string} module - 模块名
* @param {string} emoji - Emoji标签
* @returns {Object} 格式化后的消息对象
*/
formatMessage(level, message, module = '', emoji = '') {
const timestamp = this.options.showTimestamp ? this.formatTimestamp() : ''
const levelStr = this.options.showLevel ? `[${level}]` : ''
const moduleStr = this.options.showModule && module ? `[${module}]` : ''
const emojiStr = emoji || this.emojis[level] || ''
// 控制台输出格式(带颜色)
const consoleMessage = [
this.colors.DIM + timestamp + this.colors.RESET,
this.colors[level] + levelStr + this.colors.RESET,
this.colors.BRIGHT + moduleStr + this.colors.RESET,
emojiStr,
message
].filter(Boolean).join(' ')
// 文件输出格式(无颜色)
const fileMessage = [
timestamp,
levelStr,
moduleStr,
emojiStr,
message
].filter(Boolean).join(' ')
return { consoleMessage, fileMessage }
}
/**
* 写入日志文件
* @param {string} message - 日志消息
*/
writeToFile(message) {
if (!this.options.enableFileLog) return
try {
const logFile = path.join(this.options.logDir, this.options.logFileName)
const logEntry = `${message}\n`
// 检查文件大小并轮转
this.rotateLogFile(logFile)
fs.appendFileSync(logFile, logEntry, 'utf8')
} catch (error) {
console.error('写入日志文件失败:', error.message)
}
}
pushMemoryLog(entry) {
this.memoryLogs.push({
id: Date.now() + Math.random(),
timestamp: new Date().toISOString(),
text: entry
})
if (this.memoryLogs.length > this.maxMemoryLogs) {
this.memoryLogs.splice(0, this.memoryLogs.length - this.maxMemoryLogs)
}
}
getMemoryLogs() {
return [...this.memoryLogs]
}
clearMemoryLogs() {
this.memoryLogs = []
}
setFileLogEnabled(enabled) {
this.options.enableFileLog = !!enabled
if (this.options.enableFileLog) {
this.initLogDirectory()
}
}
isFileLogEnabled() {
return !!this.options.enableFileLog
}
setRuntimeLogEnabled(enabled) {
this.options.enableRuntimeLog = !!enabled
}
isRuntimeLogEnabled() {
return !!this.options.enableRuntimeLog
}
/**
* 日志文件轮转
* @param {string} logFile - 日志文件路径
*/
rotateLogFile(logFile) {
try {
if (!fs.existsSync(logFile)) return
const stats = fs.statSync(logFile)
const fileSizeMB = stats.size / (1024 * 1024)
if (fileSizeMB > this.options.maxFileSize) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
const backupFile = logFile.replace('.log', `_${timestamp}.log`)
fs.renameSync(logFile, backupFile)
// 清理旧的日志文件
this.cleanOldLogFiles()
}
} catch (error) {
console.error('日志文件轮转失败:', error.message)
}
}
/**
* 清理旧的日志文件
*/
cleanOldLogFiles() {
try {
const files = fs.readdirSync(this.options.logDir)
const logFiles = files
.filter(file => file.endsWith('.log') && file !== this.options.logFileName)
.map(file => ({
name: file,
path: path.join(this.options.logDir, file),
mtime: fs.statSync(path.join(this.options.logDir, file)).mtime
}))
.sort((a, b) => b.mtime - a.mtime)
// 保留最新的几个文件,删除其余的
if (logFiles.length > this.options.maxFiles) {
const filesToDelete = logFiles.slice(this.options.maxFiles)
filesToDelete.forEach(file => {
fs.unlinkSync(file.path)
})
}
} catch (error) {
console.error('清理旧日志文件失败:', error.message)
}
}
/**
* 通用日志方法
* @param {string} level - 日志级别
* @param {string} message - 日志消息
* @param {string} module - 模块名
* @param {string} emoji - Emoji标签
* @param {any} data - 附加数据
*/
log(level, message, module = '', emoji = '', data = null) {
if (!this.shouldLog(level)) return
const { consoleMessage, fileMessage } = this.formatMessage(level, message, module, emoji)
// 控制台输出
if (level === 'ERROR') {
console.error(consoleMessage)
} else if (level === 'WARN') {
console.warn(consoleMessage)
} else {
console.log(consoleMessage)
}
// 输出附加数据
if (data !== null) {
console.log(data)
}
// 文件输出
this.writeToFile(fileMessage + (data ? `\n${JSON.stringify(data, null, 2)}` : ''))
// 内存缓冲,用于前端日志查看
this.pushMemoryLog(fileMessage + (data ? `\n${JSON.stringify(data, null, 2)}` : ''))
}
// 便捷方法
debug(message, module = '', emoji = '', data = null) {
this.log('DEBUG', message, module, emoji || this.emojis.DEBUG, data)
}
info(message, module = '', emoji = '', data = null) {
this.log('INFO', message, module, emoji || this.emojis.INFO, data)
}
warn(message, module = '', emoji = '', data = null) {
this.log('WARN', message, module, emoji || this.emojis.WARN, data)
}
error(message, module = '', emoji = '', data = null) {
this.log('ERROR', message, module, emoji || this.emojis.ERROR, data)
}
// 特定场景的便捷方法
success(message, module = '', data = null) {
this.info(message, module, this.emojis.SUCCESS, data)
}
network(message, module = '', data = null) {
this.info(message, module, this.emojis.NETWORK, data)
}
database(message, module = '', data = null) {
this.info(message, module, this.emojis.DATABASE, data)
}
auth(message, module = '', data = null) {
this.info(message, module, this.emojis.AUTH, data)
}
redis(message, module = '', data = null) {
this.info(message, module, this.emojis.REDIS, data)
}
chat(message, module = '', data = null) {
this.info(message, module, this.emojis.CHAT, data)
}
server(message, module = '', data = null) {
this.info(message, module, this.emojis.SERVER, data)
}
}
// 创建默认实例
const defaultLogger = new Logger({
level: process.env.LOG_LEVEL || 'INFO',
enableFileLog: process.env.ENABLE_FILE_LOG === 'true',
enableRuntimeLog: process.env.ENABLE_RUNTIME_LOG !== 'false',
showModule: true,
showTimestamp: true,
showLevel: true
})
module.exports = {
Logger,
logger: defaultLogger
}
|