sematext / winston-logsene

Winston Transport for Logsene
http://sematext.com/logsene
Apache License 2.0
13 stars 6 forks source link

Logs not shipped if the process is very short-lived #10

Closed otisg closed 8 years ago

otisg commented 8 years ago

Reported by @clounie:

I'm having a problem where if the process quits too soon, none of the logs get sent. Here's the example:

https://gist.github.com/clounie/0d0b13477116deda3c000dbbd2d48952

My winston-logsene package is version 1.1.11, the latest I see on Github.

otisg commented 8 years ago

Maybe listening for an (before)exit event should be done? I imagine https://github.com/sematext/logagent-js either already has that or should have it, too?

https://nodejs.org/api/process.html https://www.npmjs.com/package/shutdown-handler

megastef commented 8 years ago

The exit handler can wait max. 10 seconds (upstart would force a kill -9 after 10 sec).

I would try export LOGSENE_LOG_INTERVAL=8000

But we can check the function, which should flush the logs immediatly ...

otisg commented 8 years ago

+1 for adding support for "flush on exit". -1 for increasing the overall indexing frequency because that hurts both sides.

megastef commented 8 years ago

We have already "flushOnExit". I think this issue is related to to https://github.com/winstonjs/winston/issues/704 - I removed "handleExceptions: true" from the winston Console transport in the GIST code and the log message got logged. An interesting observation was that we don't get the "beforeExit" event. I assume console transport does call process.exit() and we have no chance to do any async action then as only "exit" event is triggered. Anyhow we are getting closer. At least I recommend not to use "handleException" on any other transport - a workaround is to handle uncaughtExceptions in the main program and log there to console and to winston and terminate the process after the "logged" event.

megastef commented 8 years ago

Please note: don't use "handleExceptions" in the console transport. There seems to be a conflict in the winston framework - Console transport could close the application before winston-logsene had a chance to flush the logs.

This is the code I used to test (based on the original gist):

var winston = require('winston');
var logsene = require('winston-logsene') 

var logger = new winston.Logger({
    transports: [
        new (winston.transports.Console)({ 
            level: 'debug',
          handleExceptions: false,
          exitOnError: false,
          humanReadableUnhandledException: false
        })
    ]
});
var logger = new winston.Logger()

logger.add (logsene, {
    token: process.env.LOGSENE_TOKEN, 
    type: 'test_logs',
    level: 'debug',
    handleExceptions: true,
    exitOnError: true,
    humanReadableUnhandledException: false
});

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() );

q();

bildschirmfoto 2016-08-25 um 12 36 29

clounie commented 8 years ago

Yep, working here as well. Duly noted @ the console conflict.

Thanks for the quick fix! 👍