question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UTF-8 log message causes remote logger's payload to be truncated

See original GitHub issue

Content-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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
reederzcommented, Apr 28, 2021

If you don’t have time, I can try and write test too, no problem.

  1. make sure ascii message passes on mock server without json decode error
  2. make sure utf-8 with special chars message passes on mock server without json decode error

At this point, I’m not sure what truncates the message based on byte length - client or server…

1reaction
megahertzcommented, Apr 28, 2021

Thank you, but it’s pretty simple. I need more time for testing rather than writing code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LogEntry | Cloud Logging
Cloud Logging truncates label keys that exceed 512 B and label values ... The log entry payload, represented as a Unicode string (UTF-8)....
Read more >
CloudWatch Logs agent reference - AWS Documentation
This reference is for the older deprecated CloudWatch Logs agent. If you use Instance Metadata Service Version 2 (IMDSv2), you must use the...
Read more >
Log4j User Guide (PDF) - Apache Logging Services
lazily construct a log message only if the requested log level is enabled. ... Log4j makes it easy to name Loggers by software...
Read more >
Tracing XML request/responses with JAX-WS - Stack Overflow
Following options enable logging of all communication to the console (technically, you only need one of these, but that depends on the libraries...
Read more >
Seven Typical Problems With Logging Ruby (And How to ...
By default, the Ruby on Rails logger uses ANSI color codes in log messages, which can cause problems if you send logs to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found