log.js 660 B

12345678910111213141516171819202122232425262728293031
  1. var util = require("util")
  2. global.logLevel = 0
  3. var n0 = function(n) {
  4. if(n >= 0 && n < 10)
  5. return "0"+n
  6. return n
  7. }
  8. global.log0 = function(s, l) {
  9. if(s instanceof Object)
  10. s = util.inspect(s)
  11. if(l === undefined)
  12. l = 0
  13. if(l <= logLevel) {
  14. var d = new Date()
  15. var t = d.getFullYear()+"-"+n0(d.getMonth()+1)+"-"+n0(d.getDate())+"_"+n0(d.getHours())+":"+n0(d.getMinutes())+":"+n0(d.getSeconds())
  16. process.stdout.write(t+" "+s+"\n");
  17. }
  18. }
  19. global.log1 = function(s) { log0(s, 1) }
  20. global.log2 = function(s) { log0(s, 2) }
  21. global.log3 = function(s) { log0(s, 3) }
  22. global.log4 = function(s) { log0(s, 4) }
  23. global.log5 = function(s) { log0(s, 5) }