mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-20 16:04:20 +00:00
83 lines
1.7 KiB
JavaScript
83 lines
1.7 KiB
JavaScript
'use strict'
|
|
|
|
const color = require('colorful');
|
|
const util = require('./util');
|
|
|
|
let ifPrint = true;
|
|
let logLevel = 0;
|
|
const LogLevelMap = {
|
|
tip: 0,
|
|
system_error: 1,
|
|
rule_error: 2,
|
|
warn: 3,
|
|
debug: 4,
|
|
};
|
|
|
|
function setPrintStatus(status) {
|
|
ifPrint = !!status;
|
|
}
|
|
|
|
function setLogLevel(level) {
|
|
logLevel = parseInt(level, 10);
|
|
}
|
|
|
|
function printLog(content, type) {
|
|
if (!ifPrint) {
|
|
return;
|
|
}
|
|
const timeString = util.formatDate(new Date(), 'YYYY-MM-DD hh:mm:ss');
|
|
switch (type) {
|
|
case LogLevelMap.tip: {
|
|
if (logLevel > 0) {
|
|
return;
|
|
}
|
|
console.log(color.cyan(`[AnyProxy Log][${timeString}]: ` + content));
|
|
break;
|
|
}
|
|
|
|
case LogLevelMap.system_error: {
|
|
if (logLevel > 1) {
|
|
return;
|
|
}
|
|
console.error(color.red(`[AnyProxy ERROR][${timeString}]: ` + content));
|
|
break;
|
|
}
|
|
|
|
case LogLevelMap.rule_error: {
|
|
if (logLevel > 2) {
|
|
return;
|
|
}
|
|
|
|
console.error(color.red(`[AnyProxy RULE_ERROR][${timeString}]: ` + content));
|
|
break;
|
|
}
|
|
|
|
case LogLevelMap.warn: {
|
|
if (logLevel > 3) {
|
|
return;
|
|
}
|
|
|
|
console.error(color.magenta(`[AnyProxy WARN][${timeString}]: ` + content));
|
|
break;
|
|
}
|
|
|
|
case LogLevelMap.debug: {
|
|
return;
|
|
}
|
|
|
|
default : {
|
|
console.log(color.cyan(`[AnyProxy Log][${timeString}]: ` + content));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports.printLog = printLog;
|
|
module.exports.setPrintStatus = setPrintStatus;
|
|
module.exports.setLogLevel = setLogLevel;
|
|
module.exports.T_TIP = LogLevelMap.tip;
|
|
module.exports.T_ERR = LogLevelMap.system_error;
|
|
module.exports.T_RULE_ERROR = LogLevelMap.rule_error;
|
|
module.exports.T_WARN = LogLevelMap.warn;
|
|
module.exports.T_DEBUG = LogLevelMap.debug;
|