57 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-02-13 19:32:10 +04:00
import DEBUG from "../config/debug";
2020-06-21 15:25:17 +03:00
export enum LogLevels {
log = 1,
warn = 2,
error = 4,
debug = 8
};
const _logTimer = Date.now();
2020-06-21 15:25:17 +03:00
function dT() {
return '[' + ((Date.now() - _logTimer) / 1000).toFixed(3) + ']';
2020-06-21 15:25:17 +03:00
}
export function logger(prefix: string, level = LogLevels.log | LogLevels.warn | LogLevels.error) {
if(!DEBUG/* || true */) {
2020-06-21 15:25:17 +03:00
level = LogLevels.error;
}
2020-08-26 22:25:43 +03:00
//level = LogLevels.log | LogLevels.warn | LogLevels.error | LogLevels.debug
2020-08-26 19:14:23 +03:00
2020-06-21 15:25:17 +03:00
function Log(...args: any[]) {
return level & LogLevels.log && console.log(dT(), prefix, ...args);
2020-06-21 15:25:17 +03:00
}
Log.warn = function(...args: any[]) {
return level & LogLevels.warn && console.warn(dT(), prefix, ...args);
2020-06-21 15:25:17 +03:00
};
Log.info = function(...args: any[]) {
return level & LogLevels.log && console.info(dT(), prefix, ...args);
2020-06-21 15:25:17 +03:00
};
Log.error = function(...args: any[]) {
return level & LogLevels.error && console.error(dT(), prefix, ...args);
2020-06-21 15:25:17 +03:00
};
Log.trace = function(...args: any[]) {
return level & LogLevels.log && console.trace(dT(), prefix, ...args);
2020-06-21 15:25:17 +03:00
};
2020-08-26 22:25:43 +03:00
/* Log.debug = function(...args: any[]) {
return level & LogLevels.debug && console.log(dT(), prefix, ...args);
2020-08-26 22:25:43 +03:00
}; */
2020-08-26 19:14:23 +03:00
2020-08-26 22:25:43 +03:00
Log.debug = function(...args: any[]) {
return level & LogLevels.debug && console.debug(dT(), prefix, ...args);
2020-08-26 22:25:43 +03:00
};
Log.setPrefix = function(_prefix: string) {
prefix = '[' + _prefix + ']:';
};
Log.setPrefix(prefix);
2020-06-21 15:25:17 +03:00
return Log;
};