1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /**
- * @Author HonorLee (dev@honorlee.me)
- * @Version 1.0 (2018-05-04)
- * @License MIT
- */
- require('colors');
- import Tracer = require('tracer');
- const TracerInstance = Tracer.dailyfile({root:SYSTEM.PATH.Log,format : `{{timestamp}} | {{file}}:{{line}} {{message}}`, dateformat : "HH:MM:ss.L"});
- enum Level{
- LOG,
- INFO,
- DEBUG,
- WARN,
- ERROR
- }
- class Logger{
- public print(level:Level,msg:any,color:string|null):void{
- // console.log(new Error('test').stack)
- const str = `[${SYSTEM.ID}][${Level[level]}][${Moment().format('HH:mm:ss')}] ${msg}`;
- if(color){
- console.log(str[color as any]);
- }else{
- console.log(str);
- }
- const WriteLevel = SYSTEM.CONFIG.Log_Level.toUpperCase();
- if(Level[WriteLevel]){
- const wlevel = <any>Level[WriteLevel] as number;
- if(level >= wlevel){
- this.write(level,msg)
- }
- }
- }
- public write(level:Level,msg:any):void{
- const fun:string = Level[level].toLowerCase();
- TracerInstance[fun](msg);
- }
- }
- const LoggerInstance = new Logger();
- const LoggerBridge = new Proxy(LoggerInstance,{
- get(target:Logger,key:string){
- let level:Level = -1;
- let color:string|null = null;
- switch(key){
- case 'log':
- level = Level.LOG;
- break;
- case 'info':
- level = Level.INFO;
- color = 'green';
- break;
- case 'debug':
- level = Level.DEBUG;
- color = 'magenta';
- break;
- case 'warn':
- level = Level.WARN;
- color = 'yellow';
- break;
- case 'error':
- level = Level.ERROR;
- color = 'red';
- break;
- default:
- return null;
- }
- return function(msg:string):void{
- LoggerInstance.print(level,msg,color);
- }
- }
- })
- module.exports = LoggerBridge;
-
|