github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/sample-apps/node/node_modules/logfmt/lib/logger.js (about) 1 var _ = require('lodash'); 2 3 exports.log = function(data, stream) { 4 this.stream = this.stream || process.stdout; 5 if(stream == undefined) stream = this.stream; 6 7 var logData = _.extend({}, this.defaultData, data); 8 9 if(this.timers){ 10 for(var key in this.timers){ 11 var now = (new Date()).getTime() 12 logData[key] = (now - this.timers[key]).toString() + 'ms' ; 13 } 14 } 15 16 stream.write(this.stringify(logData) + "\n"); 17 } 18 19 exports.time = function(label) { 20 var logfmt = require('../logfmt'); 21 var startTime = (new Date()).getTime(); 22 var label = label || 'elapsed'; 23 var timer = new logfmt(); 24 timer.stream = this.stream; 25 timer.defaultData = this.defaultData; 26 timer.timers = _.extend({}, this.timers) 27 timer.timers[label] = startTime; 28 return timer; 29 } 30 31 exports.namespace = function(object) { 32 var logfmt = require('../logfmt'); 33 var namespaced = new logfmt() 34 var namespace = _.extend({}, this.defaultData, object); 35 namespaced.stream = this.stream; 36 namespaced.defaultData = namespace 37 namespaced.timers = this.timers; 38 return namespaced; 39 } 40 41 exports.error = function(err, id) { 42 this.maxErrorLines = this.maxErrorLines || 10; 43 if (id === undefined) { 44 id = Math.random().toString().slice(2, 12); 45 } 46 this.log({ error:true, id:id, message:err.message }); 47 var stack = err.stack.split('\n'); 48 for (var line in stack) { 49 if (line >= this.maxErrorLines) break; 50 this.log({ error:true, id:id, line:line, trace:stack[line] }); 51 } 52 }