-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate to es6 classes and new good api
- Loading branch information
1 parent
addeaff
commit 46c6676
Showing
1 changed file
with
141 additions
and
164 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 |
---|---|---|
@@ -1,195 +1,172 @@ | ||
// Load modules | ||
'use strict'; | ||
|
||
const Squeeze = require('good-squeeze').Squeeze; | ||
const Hoek = require('hoek'); | ||
const Moment = require('moment'); | ||
const SafeStringify = require('json-stringify-safe'); | ||
const Stream = require('stream').Transform; | ||
|
||
const internals = { | ||
defaults: { | ||
format: 'YYMMDD/HHmmss.SSS', | ||
utc: true, | ||
color: true | ||
} | ||
}; | ||
|
||
var Squeeze = require('good-squeeze').Squeeze; | ||
var Hoek = require('hoek'); | ||
var Moment = require('moment'); | ||
var SafeStringify = require('json-stringify-safe'); | ||
var Through = require('through2'); | ||
class GoodConsole extends Stream { | ||
constructor (config) { | ||
|
||
super({objectMode: true}); | ||
|
||
this.settings = { | ||
format: 'YYMMDD/HHmmss.SSS', | ||
utc: true, | ||
color: true | ||
}; | ||
this._filter = new Squeeze(config); | ||
} | ||
|
||
_transform (data, enc, next) { | ||
|
||
let eventName = data.event; | ||
// let tags = []; | ||
// | ||
// if (Array.isArray(data.tags)) { | ||
// tags = adta.tags.concat([]); | ||
// } else if (data.tags != null) { | ||
// tags = [data.tags]; | ||
// } | ||
// | ||
// tags.unshift(eventName); | ||
|
||
if (eventName === 'error') { | ||
return next(null, this._formatError(data)); | ||
} | ||
|
||
// Declare internals | ||
if (eventName === 'ops') { | ||
return next(null, this._formatOps(data)); | ||
} | ||
|
||
var internals = { | ||
defaults: { | ||
format: 'YYMMDD/HHmmss.SSS', | ||
utc: true, | ||
color: true | ||
if (eventName === 'response') { | ||
return next(null, this._formatResponse(data)); | ||
} | ||
}; | ||
|
||
module.exports = internals.GoodConsole = function (events, config) { | ||
if (!data.data) { | ||
data.data = '(none)'; | ||
} | ||
|
||
if (!(this instanceof internals.GoodConsole)) { | ||
return new internals.GoodConsole(events, config); | ||
return next(null, this.formatDefault(data)); | ||
} | ||
|
||
_formatOutput (event) { | ||
|
||
let timestamp = Moment(parseInt(event.timestamp, 10)).format(this.settings.format); | ||
|
||
if (!this.settings.utc) { | ||
timestamp.local(); | ||
} | ||
config = config || {}; | ||
this._settings = Hoek.applyToDefaults(internals.defaults, config); | ||
this._filter = new Squeeze(events); | ||
}; | ||
|
||
internals.GoodConsole.prototype.init = function (stream, emitter, callback) { | ||
const output = `${timestamp}, ${event.data}`; | ||
|
||
var self = this; | ||
return output; | ||
} | ||
|
||
if (!stream._readableState.objectMode) { | ||
return callback(new Error('stream must be in object mode')); | ||
_formatMethod (method) { | ||
|
||
const methodColors = { | ||
get: 32, | ||
delete: 31, | ||
put: 36, | ||
post: 33 | ||
}; | ||
|
||
let color; | ||
let formattedMethod = method.toLowerCase(); | ||
if (this.settings.color) { | ||
color = methodColors[method.toLowerCase()] || 34; | ||
formattedMethod = `\x1b[1;${color}m${formattedMethod}\x1b[0m`; | ||
} | ||
|
||
stream.pipe(this._filter).pipe(Through.obj(function goodConsoleTransform (data, enc, next) { | ||
|
||
var eventName = data.event; | ||
var tags = []; | ||
|
||
/*eslint-disable */ | ||
if (Array.isArray(data.tags)) { | ||
tags = data.tags.concat([]); | ||
} else if (data.tags != null) { | ||
tags = [data.tags]; | ||
} | ||
/*eslint-enable */ | ||
|
||
tags.unshift(eventName); | ||
|
||
if (eventName === 'response') { | ||
this.push(self._formatResponse(data, tags)); | ||
return next(); | ||
} | ||
|
||
if (eventName === 'wreck') { | ||
this.push(self._formatWreck(data, tags)); | ||
return next(); | ||
} | ||
|
||
var eventPrintData = { | ||
timestamp: data.timestamp || Date.now(), | ||
tags: tags, | ||
data: undefined | ||
}; | ||
|
||
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); | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
} | ||
|
||
if (eventName === 'error') { | ||
eventPrintData.data = 'message: ' + data.error.message + ' stack: ' + data.error.stack; | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
} | ||
|
||
if (eventName === 'request' || eventName === 'log') { | ||
eventPrintData.data = 'data: ' + (typeof data.data === 'object' ? SafeStringify(data.data) : data.data); | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
} | ||
|
||
// 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)'; | ||
} | ||
|
||
this.push(self._printEvent(eventPrintData)); | ||
return next(); | ||
})).pipe(process.stdout); | ||
|
||
callback(); | ||
}; | ||
return formattedMethod; | ||
} | ||
|
||
_formatStatusCode (statusCode) { | ||
|
||
let 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; | ||
} | ||
|
||
return `\x1b[${color}m${statusCode}\x1b[0m`; | ||
} | ||
|
||
internals.GoodConsole.prototype._printEvent = function (event) { | ||
return statusCode; | ||
} | ||
|
||
var m = Moment(parseInt(event.timestamp, 10)); | ||
if (!this._settings.utc) { m.local(); } | ||
_formatResponse (event) { | ||
|
||
var timestring = m.format(this._settings.format); | ||
var data = event.data; | ||
var output = timestring + ', [' + event.tags.toString() + '], ' + data; | ||
const query = event.query ? JSON.stringify(event.query) : ''; | ||
const method = this._formatMethod(event.method); | ||
const statusCode = this._formatStatusCode(event.statusCode) || ''; | ||
|
||
return output + '\n'; | ||
}; | ||
// 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)`; | ||
|
||
internals.GoodConsole.prototype._formatResponse = function (event, tags) { | ||
const response = { | ||
timestamp: event.timestamp, | ||
data: output | ||
}; | ||
|
||
var query = event.query ? JSON.stringify(event.query) : ''; | ||
var responsePayload = ''; | ||
return this._formatOutput(response); | ||
} | ||
|
||
if (typeof event.responsePayload === 'object' && event.responsePayload) { | ||
responsePayload = 'response payload: ' + SafeStringify(event.responsePayload); | ||
} | ||
_formatOps (event) { | ||
|
||
var method = this._formatMethod(event.method); | ||
var statusCode = this._formatStatusCode(event.statusCode) || ''; | ||
const memory = Math.round(event.proc.mem.rss / (1024 * 1024)); | ||
const output = `memory: ${memory}Mb, uptime (seconds): ${event.proc.uptime}, load: ${event.os.load}` | ||
|
||
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) | ||
}); | ||
}; | ||
const ops = { | ||
timestamp: event.timestamp, | ||
data: output | ||
}; | ||
|
||
internals.GoodConsole.prototype._formatWreck = function (event, tags) { | ||
return this._formatOutput(ops); | ||
} | ||
|
||
var data, method, statusCode; | ||
method = this._formatMethod(event.request.method); | ||
_formatError (event) { | ||
|
||
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); | ||
} | ||
const output = `message: ${event.error.message} stack: ${data.error.stack}`; | ||
|
||
const error = { | ||
timestamp: event.timestamp, | ||
data: output | ||
}; | ||
|
||
return this._formatOutput(error); | ||
} | ||
|
||
return this._printEvent({ | ||
timestamp: event.timestamp, | ||
tags: tags, | ||
data: data | ||
}); | ||
}; | ||
_formatDefault (event) { | ||
|
||
internals.GoodConsole.prototype._formatMethod = function (method) { | ||
const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data; | ||
const output = `data: ${data}`; | ||
|
||
var color; | ||
var methodColors = { | ||
get: 32, | ||
delete: 31, | ||
put: 36, | ||
post: 33 | ||
const defaults = { | ||
timestamp: event.timestamp || Moment().unix(), | ||
data: output | ||
}; | ||
var formattedMethod = method.toLowerCase(); | ||
if (this._settings.color) { | ||
color = methodColors[method.toLowerCase()] || 34; | ||
formattedMethod = '\x1b[1;' + color + 'm' + formattedMethod + '\x1b[0m'; | ||
} | ||
return formattedMethod; | ||
}; | ||
|
||
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; | ||
} | ||
return '\x1b[' + color + 'm' + statusCode + '\x1b[0m'; | ||
} | ||
return statusCode; | ||
}; | ||
return this._formatOutput(defaults); | ||
} | ||
|
||
internals.GoodConsole.attributes = { | ||
pkg: require('../package.json') | ||
}; | ||
} | ||
|
||
module.exports = GoodConsole; |