anyproxy/lib/log.ts

124 lines
2.6 KiB
TypeScript

'use strict';
/*tslint:disable:no-console */
import * as color from 'colorful';
import util from './util';
let ifPrint = true;
let logLevel = 0;
enum LogLevelMap {
tip = 0,
system_error = 1,
error = 1,
rule_error = 2,
warn = 3,
debug = 4,
}
function setPrintStatus(status: boolean): void {
ifPrint = !!status;
}
function setLogLevel(level: string): void {
logLevel = parseInt(level, 10);
}
function printLog(content: string, type?: LogLevelMap): void {
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: {
console.log(color.cyan(`[AnyProxy Log][${timeString}]: ` + content));
return;
}
default: {
console.log(color.cyan(`[AnyProxy Log][${timeString}]: ` + content));
break;
}
}
}
function debug(content: string): void {
printLog(content, LogLevelMap.debug);
}
function info(content: string): void {
printLog(content, LogLevelMap.tip);
}
function warn(content: string): void {
printLog(content, LogLevelMap.warn);
}
function error(content: string): void {
printLog(content, LogLevelMap.system_error);
}
function ruleError(content: string): void {
printLog(content, LogLevelMap.rule_error);
}
// 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;
// module.exports.printLog = printLog;
const LogUtil = {
setPrintStatus,
setLogLevel,
printLog,
debug,
info,
warn,
error,
ruleError,
T_TIP: LogLevelMap.tip,
T_ERR: LogLevelMap.error,
T_RULE_ERROR: LogLevelMap.rule_error,
T_WARN: LogLevelMap.warn,
T_DEBUG: LogLevelMap.debug,
};
export default LogUtil;