* 格式化日志消息 * @param {string} level - 日志级别 * @param {string} message - 日志消息 * @param {string} module - 模块名 * @param {string} emoji - Emoji标签 * @returns {Object} 格式化后的消息对象
(level, message, module = '', emoji = '')
| 129 | const year = now.getFullYear() |
| 130 | const month = String(now.getMonth() + 1).padStart(2, '0') |
| 131 | const day = String(now.getDate()).padStart(2, '0') |
| 132 | const hours = String(now.getHours()).padStart(2, '0') |
| 133 | const minutes = String(now.getMinutes()).padStart(2, '0') |
| 134 | const seconds = String(now.getSeconds()).padStart(2, '0') |
| 135 | |
| 136 | return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * 格式化日志消息 |
| 141 | * @param {string} level - 日志级别 |
| 142 | * @param {string} message - 日志消息 |
| 143 | * @param {string} module - 模块名 |
| 144 | * @param {string} emoji - Emoji标签 |
| 145 | * @returns {Object} 格式化后的消息对象 |
| 146 | */ |
| 147 | formatMessage(level, message, module = '', emoji = '') { |
| 148 | const timestamp = this.options.showTimestamp ? this.formatTimestamp() : '' |
| 149 | const levelStr = this.options.showLevel ? `[${level}]` : '' |
| 150 | const moduleStr = this.options.showModule && module ? `[${module}]` : '' |
| 151 | const emojiStr = emoji || this.emojis[level] || '' |
| 152 | |
| 153 | // 控制台输出格式(带颜色) |
| 154 | const consoleMessage = [ |
| 155 | this.colors.DIM + timestamp + this.colors.RESET, |
| 156 | this.colors[level] + levelStr + this.colors.RESET, |
| 157 | this.colors.BRIGHT + moduleStr + this.colors.RESET, |
| 158 | emojiStr, |
| 159 | message |