Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from thebergamo/updates
Update to new interface of Good and es6
- Loading branch information
Showing
6 changed files
with
408 additions
and
875 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ sudo: false | |
language: node_js | ||
|
||
node_js: | ||
- 0.10 | ||
- 4 | ||
- "4" | ||
- "node" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,195 +1,182 @@ | ||
// Load modules | ||
'use strict'; | ||
|
||
var Squeeze = require('good-squeeze').Squeeze; | ||
var Hoek = require('hoek'); | ||
var Moment = require('moment'); | ||
var SafeStringify = require('json-stringify-safe'); | ||
var Through = require('through2'); | ||
// Load Modules | ||
|
||
// Declare internals | ||
const Hoek = require('hoek'); | ||
const Moment = require('moment'); | ||
const Stream = require('stream'); | ||
const SafeStringify = require('json-stringify-safe'); | ||
|
||
var internals = { | ||
const internals = { | ||
defaults: { | ||
format: 'YYMMDD/HHmmss.SSS', | ||
utc: true, | ||
color: true | ||
} | ||
}; | ||
|
||
module.exports = internals.GoodConsole = function (events, config) { | ||
internals.utility = { | ||
formatOutput(event, settings) { | ||
|
||
if (!(this instanceof internals.GoodConsole)) { | ||
return new internals.GoodConsole(events, config); | ||
} | ||
config = config || {}; | ||
this._settings = Hoek.applyToDefaults(internals.defaults, config); | ||
this._filter = new Squeeze(events); | ||
}; | ||
let timestamp = Moment(parseInt(event.timestamp, 10)); | ||
|
||
internals.GoodConsole.prototype.init = function (stream, emitter, callback) { | ||
if (settings.utc) { | ||
timestamp = timestamp.utc(); | ||
} | ||
|
||
var self = this; | ||
timestamp = timestamp.format(settings.format); | ||
|
||
if (!stream._readableState.objectMode) { | ||
return callback(new Error('stream must be in object mode')); | ||
} | ||
event.tags = event.tags.toString(); | ||
const tags = ` [${event.tags}] `; | ||
|
||
stream.pipe(this._filter).pipe(Through.obj(function goodConsoleTransform (data, enc, next) { | ||
const output = `${timestamp},${tags}${event.data}`; | ||
|
||
var eventName = data.event; | ||
var tags = []; | ||
return output; | ||
}, | ||
|
||
/*eslint-disable */ | ||
if (Array.isArray(data.tags)) { | ||
tags = data.tags.concat([]); | ||
} else if (data.tags != null) { | ||
tags = [data.tags]; | ||
} | ||
/*eslint-enable */ | ||
formatMethod(method, settings) { | ||
|
||
tags.unshift(eventName); | ||
const methodColors = { | ||
get: 32, | ||
delete: 31, | ||
put: 36, | ||
post: 33 | ||
}; | ||
|
||
if (eventName === 'response') { | ||
this.push(self._formatResponse(data, tags)); | ||
return next(); | ||
let formattedMethod = method.toLowerCase(); | ||
if (settings.color) { | ||
const color = methodColors[method.toLowerCase()] || 34; | ||
formattedMethod = `\x1b[1;${color}m${formattedMethod}\x1b[0m`; | ||
} | ||
|
||
if (eventName === 'wreck') { | ||
this.push(self._formatWreck(data, tags)); | ||
return next(); | ||
return formattedMethod; | ||
}, | ||
|
||
formatStatusCode(statusCode, settings) { | ||
|
||
let color; | ||
if (statusCode && settings.color) { | ||
color = 32; | ||
if (statusCode >= 500) { | ||
color = 31; | ||
} | ||
else if (statusCode >= 400) { | ||
color = 33; | ||
} | ||
else if (statusCode >= 300) { | ||
color = 36; | ||
} | ||
|
||
return `\x1b[${color}m${statusCode}\x1b[0m`; | ||
} | ||
|
||
var eventPrintData = { | ||
timestamp: data.timestamp || Date.now(), | ||
tags: tags, | ||
data: undefined | ||
return statusCode; | ||
}, | ||
|
||
formatResponse(event, tags, settings) { | ||
|
||
const query = event.query ? JSON.stringify(event.query) : ''; | ||
const method = internals.utility.formatMethod(event.method, settings); | ||
const statusCode = internals.utility.formatStatusCode(event.statusCode, settings) || ''; | ||
|
||
// event, timestamp, id, instance, labels, method, path, query, responseTime, | ||
// statusCode, pid, httpVersion, source, remoteAddress, userAgent, referer, log | ||
// method, pid, error | ||
const output = `${event.instance}: ${method} ${event.path} ${query} ${statusCode} (${event.responseTime}ms)`; | ||
|
||
const response = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
if (eventName === 'ops') { | ||
eventPrintData.data = Hoek.format('memory: %sMb, uptime (seconds): %s, load: %s', | ||
Math.round(data.proc.mem.rss / (1024 * 1024)), | ||
data.proc.uptime, | ||
data.os.load); | ||
return internals.utility.formatOutput(response, settings); | ||
}, | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
} | ||
formatOps(event, tags, settings) { | ||
|
||
if (eventName === 'error') { | ||
eventPrintData.data = 'message: ' + data.error.message + ' stack: ' + data.error.stack; | ||
const memory = Math.round(event.proc.mem.rss / (1024 * 1024)); | ||
const output = `memory: ${memory}Mb, uptime (seconds): ${event.proc.uptime}, load: [${event.os.load}]`; | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
} | ||
const ops = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
if (eventName === 'request' || eventName === 'log') { | ||
eventPrintData.data = 'data: ' + (typeof data.data === 'object' ? SafeStringify(data.data) : data.data); | ||
return internals.utility.formatOutput(ops, settings); | ||
}, | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
} | ||
formatError(event, tags, settings) { | ||
|
||
// Event that is unknown to good-console, try a defualt. | ||
if (data.data) { | ||
eventPrintData.data = 'data: ' + (typeof data.data === 'object' ? SafeStringify(data.data) : data.data); | ||
} | ||
else { | ||
eventPrintData.data = 'data: (none)'; | ||
} | ||
const output = `message: ${event.error.message} stack: ${event.error.stack}`; | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
})).pipe(process.stdout); | ||
const error = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
callback(); | ||
}; | ||
return internals.utility.formatOutput(error, settings); | ||
}, | ||
|
||
internals.GoodConsole.prototype._printEvent = function (event) { | ||
formatDefault(event, tags, settings) { | ||
|
||
var m = Moment(parseInt(event.timestamp, 10)); | ||
if (!this._settings.utc) { m.local(); } | ||
const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data; | ||
const output = `data: ${data}`; | ||
|
||
var timestring = m.format(this._settings.format); | ||
var data = event.data; | ||
var output = timestring + ', [' + event.tags.toString() + '], ' + data; | ||
const defaults = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
return output + '\n'; | ||
return internals.utility.formatOutput(defaults, settings); | ||
} | ||
}; | ||
|
||
internals.GoodConsole.prototype._formatResponse = function (event, tags) { | ||
class GoodConsole extends Stream.Transform { | ||
constructor(config) { | ||
|
||
var query = event.query ? JSON.stringify(event.query) : ''; | ||
var responsePayload = ''; | ||
super({ objectMode: true }); | ||
|
||
if (typeof event.responsePayload === 'object' && event.responsePayload) { | ||
responsePayload = 'response payload: ' + SafeStringify(event.responsePayload); | ||
config = config || {}; | ||
this._settings = Hoek.applyToDefaults(internals.defaults, config); | ||
} | ||
|
||
var method = this._formatMethod(event.method); | ||
var statusCode = this._formatStatusCode(event.statusCode) || ''; | ||
|
||
return this._printEvent({ | ||
timestamp: event.timestamp, | ||
tags: tags, | ||
//instance, method, path, query, statusCode, responseTime, responsePayload | ||
data: Hoek.format('%s: %s %s %s %s (%sms) %s', event.instance, method, event.path, query, statusCode, event.responseTime, responsePayload) | ||
}); | ||
}; | ||
_transform(data, enc, next) { | ||
|
||
internals.GoodConsole.prototype._formatWreck = function (event, tags) { | ||
const eventName = data.event; | ||
let tags = []; | ||
|
||
var data, method, statusCode; | ||
method = this._formatMethod(event.request.method); | ||
if (Array.isArray(data.tags)) { | ||
tags = data.tags.concat([]); | ||
} | ||
else if (data.tags) { | ||
tags = [data.tags]; | ||
} | ||
|
||
if (event.error) { | ||
data = Hoek.format('%s: %s (%sms) error: %s stack: %s', method, event.request.url, event.timeSpent, event.error.message, event.error.stack); | ||
} else { | ||
statusCode = this._formatStatusCode(event.response.statusCode); | ||
data = Hoek.format('%s: %s %s %s (%sms)', method, event.request.url, statusCode, event.response.statusMessage, event.timeSpent); | ||
} | ||
tags.unshift(eventName); | ||
|
||
if (eventName === 'error') { | ||
return next(null, internals.utility.formatError(data, tags, this._settings)); | ||
} | ||
|
||
return this._printEvent({ | ||
timestamp: event.timestamp, | ||
tags: tags, | ||
data: data | ||
}); | ||
}; | ||
if (eventName === 'ops') { | ||
return next(null, internals.utility.formatOps(data, tags, this._settings)); | ||
} | ||
|
||
internals.GoodConsole.prototype._formatMethod = function (method) { | ||
|
||
var color; | ||
var methodColors = { | ||
get: 32, | ||
delete: 31, | ||
put: 36, | ||
post: 33 | ||
}; | ||
var formattedMethod = method.toLowerCase(); | ||
if (this._settings.color) { | ||
color = methodColors[method.toLowerCase()] || 34; | ||
formattedMethod = '\x1b[1;' + color + 'm' + formattedMethod + '\x1b[0m'; | ||
} | ||
return formattedMethod; | ||
}; | ||
if (eventName === 'response') { | ||
return next(null, internals.utility.formatResponse(data, tags, this._settings)); | ||
} | ||
|
||
internals.GoodConsole.prototype._formatStatusCode = function (statusCode) { | ||
|
||
var color; | ||
if (statusCode && this._settings.color) { | ||
color = 32; | ||
if (statusCode >= 500) { | ||
color = 31; | ||
} else if (statusCode >= 400) { | ||
color = 33; | ||
} else if (statusCode >= 300) { | ||
color = 36; | ||
if (!data.data) { | ||
data.data = '(none)'; | ||
} | ||
return '\x1b[' + color + 'm' + statusCode + '\x1b[0m'; | ||
|
||
return next(null, internals.utility.formatDefault(data, tags, this._settings)); | ||
} | ||
return statusCode; | ||
}; | ||
} | ||
|
||
module.exports = GoodConsole; | ||
|
||
internals.GoodConsole.attributes = { | ||
pkg: require('../package.json') | ||
}; |
Oops, something went wrong.