consolie.logカスタムタイムスタンプを追加する完璧なソリューションです.

1778 ワード

  log4js   ,   pm2    。        console.log



function getDate(extra){
 var dat = new Date;//    
 var year = dat.getFullYear();//   
 var month = dat.getMonth()+1;    //   ,js 0   ,  +1
 var date1 = dat.getDate(); //   
 var hour = dat.getHours();//    
 var minutes = dat.getMinutes();//    
 var second = dat.getSeconds();//   
 var haomiao = dat.getMilliseconds();
 dat = undefined;
 return year+"-"+month+"-"+date1+" "+hour+":"+minutes +":"+second+" "+haomiao + extra ;
}

console.oldlog = console.log;
console.oldtrace = console.trace;
console.olddebug = console.debug;
console.oldinfo = console.info;
console.oldwarn = console.warn;
console.olderror = console.error;


function log(){
 process.stdout.write(getDate('-log-: '));
 console.oldlog.apply(console, arguments);
}

function trace(){
 process.stdout.write(getDate('-trace-: '));
 console.oldtrace.apply(console, arguments);
}

function info(){
 process.stdout.write(getDate('-info-: '));
 console.oldinfo.apply(console, arguments);
}

function warn(){
 process.stdout.write(getDate('-warn-: '));
 console.oldwarn.apply(console, arguments);
}

function error(){
 process.stderr.write(getDate('-error-: '));
 console.olderror.apply(console, arguments);
}

function debug(){
 process.stdout.write(getDate('-debug-: '));
 console.olddebug.apply(console, arguments);
}

console.log = log;
console.debug = debug;
console.trace = trace;
console.info = info;
console.warn = warn;
console.error = error;

global.log = log;
global.debug = debug;
global.trace = trace;
global.info = info;
global.warn = warn;
global.error = error;

module.exports = global;