ql-publish
Version:
82 lines (69 loc) • 2.11 kB
JavaScript
const { formatDate } = require('./date');
// 定义 ANSI 颜色代码
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
underscore: '\x1b[4m',
blink: '\x1b[5m',
reverse: '\x1b[7m',
hidden: '\x1b[8m',
// 文本颜色
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
// 背景颜色
bgBlack: '\x1b[40m',
bgRed: '\x1b[41m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m',
bgBlue: '\x1b[44m',
bgMagenta: '\x1b[45m',
bgCyan: '\x1b[46m',
bgWhite: '\x1b[47m'
};
// 格式化时间
function getFormattedTime() {
return formatDate()
}
// 日志工具
const logger = {
// 普通日志 - 白色
log: (...args) => {
console.log(`${colors.white}[${getFormattedTime()}]`, ...args, colors.reset);
},
// 信息日志 - 蓝色
info: (...args) => {
console.log(`${colors.blue}[${getFormattedTime()}] [INFO]`, ...args, colors.reset);
},
// 成功日志 - 绿色
success: (...args) => {
console.log(`${colors.green}[${getFormattedTime()}] [SUCCESS]`, ...args, colors.reset);
},
// 警告日志 - 黄色
warn: (...args) => {
console.log(`${colors.yellow}[${getFormattedTime()}] [WARN]`, ...args, colors.reset);
},
// 错误日志 - 红色
error: (...args) => {
console.error(`${colors.red}[${getFormattedTime()}] [ERROR]`, ...args, colors.reset);
},
// 调试日志 - 品红
debug: (...args) => {
console.log(`${colors.magenta}[${getFormattedTime()}] [DEBUG]`, ...args, colors.reset);
},
// 自定义颜色日志
custom: (color, ...args) => {
if (colors[color]) {
console.log(`${colors[color]}[${getFormattedTime()}]`, ...args, colors.reset);
} else {
console.log(`[${getFormattedTime()}]`, ...args);
}
}
};
module.exports = logger;