by falcolas · 6y ago · 0 comments
Put highly detailed log messages into an in-memory ring buffer, and when you encounter an error, log out the entire contents of the ring buffer. That way you get detailed information when things go wrong, an inspectable-while-debugging record of actions, all while not overwhelming your log aggregation system with useless information.
Debugging is also the primary use case for logging program internals.
Analytics should be done on non-internal logs (such as traffic logs), unless you're just logging everything to the same destination without any structure, which is probably a bad idea.
Analytics instead should be a separate stream in most cases.
If you're placing the ringbuffer inside an appender, then the appender's encoder will format. However, if the ringbuffer itself is an appender, then you can store the logging events as is until it's time to dump them -- this is the approach in terse-logback [1]
Another option is to put the ringbuffer BEFORE sending it to the logging framework, so it's never processed by SLF4J at all. This means that it's more accessible to the application, at the cost of losing the initial timestamp of the logging event. This is the approach taken in Blindsight. [2]
Shout out to Bunyan, which is (AFAIK) the only server side logging framework that has ringbuffers out of the box. [3]
[1] https://tersesystems.github.io/terse-logback/guide/ringbuffe...
[2] https://tersesystems.github.io/blindsight/usage/buffer.html
[3] https://github.com/trentm/node-bunyan#raw--ringbuffer-stream
Logging can also be done in a separate thread, reducing even that exceptionally minor cost.
http://psy-lob-saw.blogspot.com/2016/12/linked-array-queues-...
I've benchmarked code myself, and the flamegraphs will barely show the SpscArrayQueue in the profile.
Any examples you can provide?
https://www.ibm.com/developerworks/aix/library/au-buffer/ind...
http://www.exampler.com/writing/ring-buffer.pdf
A google search for "logging to a ring buffer" will provide more.