mperdeck/jsnlog.js

Appender should have a timeout to send a non-full batch

jackric opened this issue · 2 comments

Current behaviour:

var ajaxAppender=JL.createAjaxAppender('ajaxAppender');
ajaxAppender.setOptions({"batchSize": 3});
JL().setOptions({
    "appenders": [ajaxAppender]
});
JL().info("Unwrapping pizza");
JL().info("Turning on oven");
// No logs sent by Ajax yet, because 2 < 3
setTimeout(function() { JL().info("Putting pizza in oven") }, 20 * 60 * 1000);

This caught me out for a while. I guess I assumed batchSize was there to be tweaked for performance with high frequency logging, and the appender would automatically drain.

Proposed behaviour:

ajaxAppender.setOptions({"batchSize": 3, "batchPeriod": 1000});
JL().info("Unwrapping pizza");
JL().info("Turning on oven");
// 1000ms elapses, then 2 log messages are sent by Ajax
setTimeout(function() { JL().info("Putting pizza in oven") }, 20 * 60 * 1000);

I understand the behaviour of an implementation is debatable. Happy to work on some pull requests if this otherwise seems sane.

I concur that it would be good to have a configurable timeout on the buffer (so log items spend at most x milliseconds in the buffer before being sent).

Others concur with you too and have opened an issue in the jsnlog project:
mperdeck/jsnlog#83

I would welcome a pull request. The timeout would have to be configurable (not fixed). Also, please keep your code change as small and simple as possible, so it is easy for me to review it and be comfortable that it works.

In the latest release 2.24.0, I introduced a new property on appenders "batchTimeout". Setting this (in ms) guarantees that a message won't sit in the batch buffer for longer than the given time out.

http://jsnlog.com/Documentation/Configuration/JSNLog/AjaxAppender
http://jsnlog.com/Documentation/JSNLogJs/AjaxAppender/SetOptions