coolog-appender-logentries
Advanced tools
Comparing version 0.1.1 to 0.2.0
117
index.js
@@ -5,9 +5,30 @@ /*jshint node:true, indent:2, white:true, laxcomma:true, undef:true, strict:true, unused:true, eqnull:true, camelcase: false */ | ||
var logentries = require('node-logentries') | ||
, inspect = require('seye.js').inspector({ stream: null }) | ||
, chalk = require('chalk') | ||
, cluster = require('cluster') | ||
; | ||
// always enable chalk colors | ||
chalk.enabled = true; | ||
var COLOR_MAP = { | ||
'log' : 'white' | ||
, 'error' : 'red' | ||
, 'warn' : 'yellow' | ||
, 'info' : 'cyan' | ||
, 'ok' : 'green' | ||
, 'debug' : 'grey' | ||
}; | ||
module.exports = function (token) { | ||
var log | ||
, hostname = require('os').hostname() | ||
, logentries_logger | ||
, _log_helper | ||
, _generate_color | ||
, _getLine | ||
; | ||
@@ -22,2 +43,18 @@ if (typeof token !== 'string' || token.length === 0) { | ||
_generate_color = function (str) { | ||
var hash = 0 | ||
, c | ||
, key; | ||
if (str.length === 0) return hash; | ||
for (var i = 0; i < str.length; i++) { | ||
c = str.charCodeAt(i); | ||
hash = ((hash<<5) - hash) + c; | ||
hash = hash & hash; // Convert to 32bit integer | ||
} | ||
key = Object.keys(COLOR_MAP)[Math.abs(hash) % Object.keys(COLOR_MAP).length]; | ||
return COLOR_MAP[key]; | ||
}; | ||
_getLine = function () { | ||
@@ -27,2 +64,46 @@ var e = new Error(); | ||
return line; | ||
}; | ||
_log_helper = function (level_name, channel_name, filename, color, line, msg) { | ||
var channel_name_color = _generate_color(channel_name) | ||
, level_name_str | ||
, le_level | ||
; | ||
if (msg.indexOf('\n') > -1) { | ||
// If string is multiline call _log for each string | ||
msg.split('\n').forEach(function (row) { | ||
_log_helper(level_name, channel_name, filename, color, line, row); | ||
}); | ||
} else { | ||
level_name_str = level_name; | ||
if (level_name_str.length < 5) { | ||
for (var i = 5 - level_name_str.length; i > 0; i--) { | ||
level_name_str = ' ' + level_name_str; | ||
} | ||
} | ||
if (level_name === 'debug') { | ||
le_level = 'debug'; | ||
} else if (level_name === 'ok') { | ||
le_level = 'info'; | ||
} else if (level_name === 'info') { | ||
le_level = 'info'; | ||
} else if (level_name === 'warn') { | ||
le_level = 'alert'; | ||
} else if (level_name === 'error') { | ||
le_level = 'emerg'; | ||
} else { | ||
le_level = 'debug'; | ||
} | ||
logentries_logger[le_level].apply(null, [ | ||
chalk.bold.grey('[') + hostname + ' ' + chalk[channel_name_color](channel_name + ', ') + chalk.blue(filename + ':' + line) + chalk.bold.grey(']') | ||
, chalk.cyan((cluster.isWorker) ? '#' + cluster.worker.id : 'mm') | ||
, chalk[color](level_name_str + ':') | ||
, msg | ||
]); | ||
} | ||
}; | ||
@@ -32,37 +113,27 @@ | ||
var msg = '' | ||
, le_level | ||
, logger | ||
, le_args | ||
, color | ||
, line = _getLine() | ||
; | ||
args.forEach(function (item) { | ||
if ('object' === typeof item) { | ||
msg = msg + JSON.stringify(item); | ||
if ('string' !== typeof item) { | ||
msg = msg + inspect(item) + ' '; | ||
} else { | ||
msg = msg + item; | ||
msg = msg + item + ' '; | ||
} | ||
}); | ||
if (level_name === 'debug') { | ||
le_level = 'debug'; | ||
} else if (level_name === 'ok') { | ||
le_level = 'info'; | ||
} else if (level_name === 'info') { | ||
le_level = 'info'; | ||
} else if (level_name === 'warn') { | ||
le_level = 'alert'; | ||
} else if (level_name === 'error') { | ||
le_level = 'emerg'; | ||
if (COLOR_MAP[level_name] === undefined) { | ||
throw new TypeError('Level ' + level_name + ' is not defined (or non-existent color).'); | ||
} else { | ||
le_level = 'debug'; | ||
} | ||
color = COLOR_MAP[level_name]; | ||
} | ||
logger = logentries_logger[le_level]; | ||
le_args = [require('os').hostname(), filename + ":" + _getLine(), msg]; | ||
logger.apply(null, le_args); | ||
_log_helper(level_name, channel_name, filename, color, line, msg); | ||
}; | ||
return { | ||
'log': log | ||
log: log | ||
}; | ||
}; | ||
}; |
{ | ||
"name": "coolog-appender-logentries", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "logentries appender for coolog", | ||
@@ -19,4 +19,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"node-logentries": "~0.1.2" | ||
"node-logentries": "~0.1.2", | ||
"seye.js": "~0.2.1", | ||
"chalk": "~0.3.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4249
110
3
+ Addedchalk@~0.3.0
+ Addedseye.js@~0.2.1
+ Addedansi-styles@0.2.0(transitive)
+ Addedchalk@0.3.0(transitive)
+ Addedcolors@0.6.2(transitive)
+ Addedhas-color@0.1.7(transitive)
+ Addedseye.js@0.2.1(transitive)