UTF-8 log message causes remote logger's payload to be truncated
See original GitHub issueContent-Length header is incorrectly calculated here https://github.com/megahertz/electron-log/blob/master/src/transports/remote.js#L74 . Is is being set to string length of the body, instead of byte length.
Consider this snippet:
var log = require('electron-log');
log.transports.remote.level = 'debug';
log.transports.remote.url = 'http://localhost:5000'
log.debug({'foo': 'bar'});
The payload received will be valid as expected:
{"client":{"name":"electron-application"},"data":[{"foo":"bar"}],"date":1619172197913,"level":"debug","variables":{}}
However, if you have UTF-8 characters in your log message, which can be more than 1 byte in size, the payload will be truncated.
E.g.:
log.debug({'föö': 'bar'});
Would result in invalid truncated json like so:
{"client":{"name":"electron-application"},"data":[{"föö":"bar"}],"date":1619172226499,"level":"debug","variables":{
Notice, how payload is truncated by exactly 2 characters, because each ö
is 2 bytes in size.
Better way would be not to calculate content-length at all or calculate it something like this:
options.headers['Content-Length'] = (new TextEncoder().encode(body)).length;
We use your module in production, and because of this, we lose quite a few log messages due to server json parsing errors 😦
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
If you don’t have time, I can try and write test too, no problem.
At this point, I’m not sure what truncates the message based on byte length - client or server…
Thank you, but it’s pretty simple. I need more time for testing rather than writing code.