Skip to content

Instantly share code, notes, and snippets.

@clounie
Created August 19, 2016 23:18
Show Gist options
  • Save clounie/0d0b13477116deda3c000dbbd2d48952 to your computer and use it in GitHub Desktop.
Save clounie/0d0b13477116deda3c000dbbd2d48952 to your computer and use it in GitHub Desktop.
winston-logsene exception problem
If you throw an exception, winston-logsene isn't sending the log.
Take the example below. I went into logsene-js to add some logs for context, which are noted below.
Then run `LOGSENE_TOKEN=xxxxxxxxxx node test.js` to see the result.
It works fine with a short-running process (i.e. when `q()` is commented out). However, if you throw an exception to end the process, LogseneJS doesn't send it.
/**
* Sending log entry to LOGSENE - this function is triggered every 100 log message or 30 seconds.
* @callback {function} optional callback function
*/
Logsene.prototype.send = function (callback) {
process.stdout.write( 'Entering Logsene.prototype.send' );
var self = this
var count = this.logCount
this.logCount = 0
var options = {
url: this.url,
logCount: count,
headers: {
'User-Agent': 'logsene-js',
'Content-Type': 'application/json',
'Connection': 'Close',
'x-logsene-origin': this.xLogseneOrigin || xLogseneOrigin
},
body: this.bulkReq.getContents(),
agent: self.httpAgent,
method: 'POST'
}
process.stdout.write('Body:');
process.stdout.write( '\n\n' + options.body.toString() + '\n\n' );
this.bulkReq = null
this.bulkReq = new streamBuffers.WritableStreamBuffer({
initialSize: initialBufferSize,
incrementAmount: incrementBuffer
})
if (options.body === false) {
return
}
process.stdout.write( 'Line before `var req = request.post(options, function( err, res ) {`' );
var req = request.post(options, function (err, res) {
process.stdout.write( 'err = ' );
process.stderr.write( JSON.stringify( err ) );
if (err || (res && res.statusCode > 399)) {
var winston = require('winston');
var logsene = require('winston-logsene')
var logger = new winston.Logger({
transports: [
new (winston.transports.Console)({
level: 'debug',
handleExceptions: true,
humanReadableUnhandledException: true
}),
]
});
// var logger = new winston.Logger()
logger.add (logsene, {
token: process.env.LOGSENE_TOKEN,
type: 'test_logs',
level: 'debug',
handleException: true,
humanReadableUnhandledException: true
});
logger.error( 'LOG IN TEST.JS' );
function a() { someUndefinedFunction(); }
function b() { a(); }
function c() { b(); }
function d() { c(); }
function e() { d(); }
function f() { e(); }
function g() { f(); }
function h() { g(); }
function i() { h(); }
function j() { i(); }
function k() { j(); }
function l() { k(); }
function m() { l(); }
function n() { m(); }
function o() { n(); }
function p() { o(); }
function q() { p(); }
console.log( new Date().toISOString() );
// *ONLY* If you comment q() out will the logs will send to Logsene
q();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment