{"action":"created","issue":{"url":"https://api.github.com/repos/trentm/node-bunyan/issues/37","labels_url":"https://api.github.com/repos/trentm/node-bunyan/issues/37/labels{/name}","comments_url":"https://api.github.com/repos/trentm/node-bunyan/issues/37/comments","events_url":"https://api.github.com/repos/trentm/node-bunyan/issues/37/events","html_url":"https://github.com/trentm/node-bunyan/issues/37","id":6439571,"number":37,"title":"Ensuring writable stream is flushed on exception","user":{"login":"timbowhite","id":857061,"avatar_url":"https://avatars.githubusercontent.com/u/857061?v=3","gravatar_id":"","url":"https://api.github.com/users/timbowhite","html_url":"https://github.com/timbowhite","followers_url":"https://api.github.com/users/timbowhite/followers","following_url":"https://api.github.com/users/timbowhite/following{/other_user}","gists_url":"https://api.github.com/users/timbowhite/gists{/gist_id}","starred_url":"https://api.github.com/users/timbowhite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timbowhite/subscriptions","organizations_url":"https://api.github.com/users/timbowhite/orgs","repos_url":"https://api.github.com/users/timbowhite/repos","events_url":"https://api.github.com/users/timbowhite/events{/privacy}","received_events_url":"https://api.github.com/users/timbowhite/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/trentm/node-bunyan/labels/major","name":"major","color":"e10c02"}],"state":"open","locked":false,"assignee":{"login":"trentm","id":46866,"avatar_url":"https://avatars.githubusercontent.com/u/46866?v=3","gravatar_id":"","url":"https://api.github.com/users/trentm","html_url":"https://github.com/trentm","followers_url":"https://api.github.com/users/trentm/followers","following_url":"https://api.github.com/users/trentm/following{/other_user}","gists_url":"https://api.github.com/users/trentm/gists{/gist_id}","starred_url":"https://api.github.com/users/trentm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trentm/subscriptions","organizations_url":"https://api.github.com/users/trentm/orgs","repos_url":"https://api.github.com/users/trentm/repos","events_url":"https://api.github.com/users/trentm/events{/privacy}","received_events_url":"https://api.github.com/users/trentm/received_events","type":"User","site_admin":false},"milestone":null,"comments":22,"created_at":"2012-08-24T17:25:09Z","updated_at":"2015-11-26T16:29:08Z","closed_at":null,"body":"Hi, this isn't really an issue w/ bunyan, but more of an FYI hack I'm using to ensure writable streams are flushed/closed when unhandled exceptions occur in a process.\n\nFor instance, the following will result in an empty foo.log file:\n\n```javascript\nvar Logger = require('bunyan'),\n    util = require('util'),\n    foo = new Logger({\n        name: \"foo\",\n        streams: [{\n            path: \"./foo.log\"\n        }]\n    });\n\nfoo.info(\"hello\");\n\nthrow new Error('suicide');\n```\n\nThat stinks as the log file won't contain the \"hello\" entry, nor offer any clue as to what happened.  A hack to rectify this:\n```javascript\nvar Logger = require('bunyan'),\n    util = require('util'),\n    foo = new Logger({\n        name: \"foo\",\n        streams: [{\n            path: \"./foo.log\"\n        }]\n    });\n\nfoo.info(\"hello\");\n\n// using uncaughtException is bad m'kay\nprocess.on('uncaughtException', function (err) {\n    // prevent infinite recursion\n    process.removeListener('uncaughtException', arguments.callee);\n\n    // bonus: log the exception\n    foo.fatal(err);\n\n    if (typeof(foo.streams[0]) !== 'object') return;\n\n    // throw the original exception once stream is closed\n    foo.streams[0].stream.on('close', function(streamErr, stream) {\n        throw err;\n    });\n\n    // close stream, flush buffer to disk\n    foo.streams[0].stream.end();\n});\n\nthrow new Error('suicide');\n```\n\nThis probably makes node.js purists grind their teeth, but it's effective at logging both the \"hello\" entry and the exception, and rethrows the original exception.\n\nDoing this w/ multiple streams would be trickier, as you'd have to detect that they were all closed before rethrowing the exception.  I'm not sure that bunyan triggers a \"close\" event when all writable streams are closed, ie:  `foo.on('close', function(streamErr, stream) { throw err; });`.\n\nOne last note, calling `process.nextTick({ process.exit() });` instead of plain old `process.exit();` usually gives the writable stream(s) a chance to flush."},"comment":{"url":"https://api.github.com/repos/trentm/node-bunyan/issues/comments/159954586","html_url":"https://github.com/trentm/node-bunyan/issues/37#issuecomment-159954586","issue_url":"https://api.github.com/repos/trentm/node-bunyan/issues/37","id":159954586,"user":{"login":"qubyte","id":824753,"avatar_url":"https://avatars.githubusercontent.com/u/824753?v=3","gravatar_id":"","url":"https://api.github.com/users/qubyte","html_url":"https://github.com/qubyte","followers_url":"https://api.github.com/users/qubyte/followers","following_url":"https://api.github.com/users/qubyte/following{/other_user}","gists_url":"https://api.github.com/users/qubyte/gists{/gist_id}","starred_url":"https://api.github.com/users/qubyte/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qubyte/subscriptions","organizations_url":"https://api.github.com/users/qubyte/orgs","repos_url":"https://api.github.com/users/qubyte/repos","events_url":"https://api.github.com/users/qubyte/events{/privacy}","received_events_url":"https://api.github.com/users/qubyte/received_events","type":"User","site_admin":false},"created_at":"2015-11-26T16:29:07Z","updated_at":"2015-11-26T16:29:07Z","body":"I was putting off adding more noise to this thread, but it's been some time and there doesn't appear to be movement on bunyan at all. What is the status of this issue and bunyan in general?"}}