mhart / gelf-stream

A node.js stream to send JS objects to a Graylog2 server (in GELF format)
Other
29 stars 13 forks source link

Is calling stream.end() really necessary? #1

Closed cybersam closed 9 years ago

cybersam commented 10 years ago

The README contains sample code that calls stream.end() directly (because bunyan doesn't do that when the program finishes).

(1) Unfortunately, the sample code is not representative of a typical node.js server, which never "finishes" deliberately. In other words, a typical server is designed to run forever, and will never exits unless it crashes. So, typically, there may be no good place in the server code to call the gelf-stream's end() function. What bad things, if any, can happen if end() is never called during a crash? For example, can we lose log entries?

(2) Even if a server were to intentionally exit (or if the input script has no server at all), there can still be callbacks queued on the main thread that want to do logging. So, simply calling end() (on the gelf-stream) from a server's 'close' event handler is not always the right thing to do.

(3) Is calling the gelf-stream's end() function from a process 'end' event handler the solution to both (1) and (2), or does that also have drawbacks?

(4) Or, is it possible to remove the end() function from the gelf-stream implementation, so that clients don't have to worry about it?

mhart commented 9 years ago

Looks like bunyan still hasn't implemented log.close, so yes, it's still necessary at least to ensure that the node process exits: https://github.com/trentm/node-bunyan/blob/b86b954a6b914775ecd827f9245dc939bb0d1b89/TODO.md#L54 – remember that you can always add this in an uncaught exception handler.

In terms of removing the end() function, that's a core node.js function on a stream: http://nodejs.org/api/stream.html#stream_writable_end_chunk_encoding_callback