4 ms·
I wonder if there's a sensible pattern for wrapping a segment in a context that accumulates log calls within until it errors or returns, then concats and dumps.
by Waterluvian 8y ago
I wonder if there's a sensible pattern for wrapping a segment in a context that accumulates log calls within until it errors or returns, then concats and dumps.
- BonesJustice 8y agoProbably, but some critical errors like stack overflow or out of memory could prevent the stack from unwinding gracefully or otherwise prevent the accumulated log from being written. And then you’d have nothing.
- dragonwriter 8y agoConceptually, if you separate out the application generating the log from the one constructing the structure, so that the former is still streaming individual messages with added context information and the latter (probably a log viewing application, so it is completely out of the critical path of recording information) uses context information to provide a structured view, then that's not a problem.
- dkersten 8y agoAlternatively, as others have mentioned, log as usual but include an instance id and then use an external service/process to aggregate log entries with the same id together.
- dragonwriter 8y agoThat's actually an example, not an alternative (the instance id is the “context information”, the external service/process is the separate component using the context information to construct the structured view.)
- dkersten 8y agoAh, good point.
- viraptor 8y agoThis is a popular concept. See https://www.npmjs.com/package/bunyan#raw--ringbuffer-stream https://www.npmjs.com/package/bunyan#raw--ringbuffer-stream for example. Many languages have an implementation for something like that.
- Osiris 8y agoI did this for a desktop app I wrote. The liver kept an in memory ring buffer and when there was an unexpected error, it would dump all the previous debug level logs to give context to the error.
- pletnes 8y agoI suppose you could concat strings and do it in a finally clause? Or use python’s context managers and do the logging in the __exit__().
- Waterluvian 8y agoExactly my thinking. And the context wraps the view call and therefore the whole transaction.