-
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.
refactoring to new features of es6 and new interface of good. Closes #63
- Loading branch information
1 parent
46c6676
commit d7e3e52
Showing
6 changed files
with
417 additions
and
861 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,172 +1,182 @@ | ||
'use strict'; | ||
|
||
const Squeeze = require('good-squeeze').Squeeze; | ||
// Load Modules | ||
|
||
const Hoek = require('hoek'); | ||
const Moment = require('moment'); | ||
const Stream = require('stream'); | ||
const SafeStringify = require('json-stringify-safe'); | ||
const Stream = require('stream').Transform; | ||
|
||
const internals = { | ||
defaults: { | ||
format: 'YYMMDD/HHmmss.SSS', | ||
utc: true, | ||
color: true | ||
} | ||
defaults: { | ||
format: 'YYMMDD/HHmmss.SSS', | ||
utc: true, | ||
color: true | ||
} | ||
}; | ||
|
||
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)); | ||
} | ||
internals.utility = { | ||
formatOutput(event, settings) { | ||
|
||
if (eventName === 'ops') { | ||
return next(null, this._formatOps(data)); | ||
} | ||
let timestamp = Moment(parseInt(event.timestamp, 10)); | ||
|
||
if (eventName === 'response') { | ||
return next(null, this._formatResponse(data)); | ||
} | ||
if (settings.utc) { | ||
timestamp = timestamp.utc(); | ||
} | ||
|
||
if (!data.data) { | ||
data.data = '(none)'; | ||
} | ||
timestamp = timestamp.format(settings.format); | ||
|
||
return next(null, this.formatDefault(data)); | ||
} | ||
event.tags = event.tags.toString(); | ||
const tags = ` [${event.tags}] `; | ||
|
||
_formatOutput (event) { | ||
const output = `${timestamp},${tags}${event.data}`; | ||
|
||
let timestamp = Moment(parseInt(event.timestamp, 10)).format(this.settings.format); | ||
return output; | ||
}, | ||
|
||
if (!this.settings.utc) { | ||
timestamp.local(); | ||
} | ||
formatMethod(method, settings) { | ||
|
||
const output = `${timestamp}, ${event.data}`; | ||
const methodColors = { | ||
get: 32, | ||
delete: 31, | ||
put: 36, | ||
post: 33 | ||
}; | ||
|
||
return output; | ||
} | ||
let formattedMethod = method.toLowerCase(); | ||
if (settings.color) { | ||
const color = methodColors[method.toLowerCase()] || 34; | ||
formattedMethod = `\x1b[1;${color}m${formattedMethod}\x1b[0m`; | ||
} | ||
|
||
_formatMethod (method) { | ||
return formattedMethod; | ||
}, | ||
|
||
const methodColors = { | ||
get: 32, | ||
delete: 31, | ||
put: 36, | ||
post: 33 | ||
}; | ||
formatStatusCode(statusCode, settings) { | ||
|
||
let color; | ||
let formattedMethod = method.toLowerCase(); | ||
if (this.settings.color) { | ||
color = methodColors[method.toLowerCase()] || 34; | ||
formattedMethod = `\x1b[1;${color}m${formattedMethod}\x1b[0m`; | ||
} | ||
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 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`; | ||
} | ||
return `\x1b[${color}m${statusCode}\x1b[0m`; | ||
} | ||
|
||
return statusCode; | ||
}, | ||
|
||
return statusCode; | ||
} | ||
formatResponse(event, tags, settings) { | ||
|
||
_formatResponse (event) { | ||
const query = event.query ? JSON.stringify(event.query) : ''; | ||
const method = internals.utility.formatMethod(event.method, settings); | ||
const statusCode = internals.utility.formatStatusCode(event.statusCode, settings) || ''; | ||
|
||
const query = event.query ? JSON.stringify(event.query) : ''; | ||
const method = this._formatMethod(event.method); | ||
const statusCode = this._formatStatusCode(event.statusCode) || ''; | ||
// 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)`; | ||
|
||
// 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 | ||
}; | ||
|
||
const response = { | ||
timestamp: event.timestamp, | ||
data: output | ||
}; | ||
return internals.utility.formatOutput(response, settings); | ||
}, | ||
|
||
return this._formatOutput(response); | ||
} | ||
formatOps(event, tags, settings) { | ||
|
||
_formatOps (event) { | ||
const memory = Math.round(event.proc.mem.rss / (1024 * 1024)); | ||
const output = `memory: ${memory}Mb, uptime (seconds): ${event.proc.uptime}, load: [${event.os.load}]`; | ||
|
||
const memory = Math.round(event.proc.mem.rss / (1024 * 1024)); | ||
const output = `memory: ${memory}Mb, uptime (seconds): ${event.proc.uptime}, load: ${event.os.load}` | ||
const ops = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
const ops = { | ||
timestamp: event.timestamp, | ||
data: output | ||
}; | ||
return internals.utility.formatOutput(ops, settings); | ||
}, | ||
|
||
return this._formatOutput(ops); | ||
} | ||
formatError(event, tags, settings) { | ||
|
||
_formatError (event) { | ||
const output = `message: ${event.error.message} stack: ${event.error.stack}`; | ||
|
||
const output = `message: ${event.error.message} stack: ${data.error.stack}`; | ||
const error = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
const error = { | ||
timestamp: event.timestamp, | ||
data: output | ||
}; | ||
return internals.utility.formatOutput(error, settings); | ||
}, | ||
|
||
return this._formatOutput(error); | ||
} | ||
formatDefault(event, tags, settings) { | ||
|
||
_formatDefault (event) { | ||
const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data; | ||
const output = `data: ${data}`; | ||
|
||
const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data; | ||
const output = `data: ${data}`; | ||
const defaults = { | ||
timestamp: event.timestamp, | ||
tags, | ||
data: output | ||
}; | ||
|
||
return internals.utility.formatOutput(defaults, settings); | ||
} | ||
}; | ||
|
||
const defaults = { | ||
timestamp: event.timestamp || Moment().unix(), | ||
data: output | ||
}; | ||
class GoodConsole extends Stream.Transform { | ||
constructor(config) { | ||
|
||
return this._formatOutput(defaults); | ||
} | ||
super({ objectMode: true }); | ||
|
||
config = config || {}; | ||
this._settings = Hoek.applyToDefaults(internals.defaults, config); | ||
} | ||
|
||
_transform(data, enc, next) { | ||
|
||
const eventName = data.event; | ||
let tags = []; | ||
|
||
if (Array.isArray(data.tags)) { | ||
tags = data.tags.concat([]); | ||
} | ||
else if (data.tags) { | ||
tags = [data.tags]; | ||
} | ||
|
||
tags.unshift(eventName); | ||
|
||
if (eventName === 'error') { | ||
return next(null, internals.utility.formatError(data, tags, this._settings)); | ||
} | ||
|
||
if (eventName === 'ops') { | ||
return next(null, internals.utility.formatOps(data, tags, this._settings)); | ||
} | ||
|
||
if (eventName === 'response') { | ||
return next(null, internals.utility.formatResponse(data, tags, this._settings)); | ||
} | ||
|
||
if (!data.data) { | ||
data.data = '(none)'; | ||
} | ||
|
||
return next(null, internals.utility.formatDefault(data, tags, this._settings)); | ||
} | ||
} | ||
|
||
module.exports = GoodConsole; | ||
|
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
Oops, something went wrong.