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.

JS Date values sent as empty objects

See original GitHub issue

Hi guys,

We have noticed that when trying to send a JSON object that contains a field of Date instance from the regular Date type in Javascript, it is serialized as an empty object. I would expect that the proper behavior should be to convert to ISO Date string.

This is happening due to the fact that fluent-logger is using msgpack-lite to encode the objects to be sent. However when a message is encoded it is using a hardcoded codec that is initialized in the instantiation of the module and cannot be accessed externally. See here

So the codec is created without any options, (skipping even the default codecs because preset flag is skipped) and when the type of the Date value given to serialized is checked with typeof, Object is returned and an attempt to browse its keys recursively and encode them results to an empty object, since Date instances do not have any browseable keys.

A solution would be to either provide the codec to the instance so it can be configured and extra packers / unpackers could be added, inherited from constructor options from the fluentd logger class, or even just simply enable the default presets of msgpack-lite to support JS native data types

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mitsos1oscommented, Feb 7, 2019

Hmmmm, I understand. I believe that since Date could be commonly logged in javascript since it is a native type, it should be logged as ISOString as a generic form at least.

This should probably be supported by default but having also available a way to provide some custom packers to the final encoder would be useful for extra customization of the logging serialization format

0reactions
mitsos1oscommented, Feb 15, 2019

I think just adding the extra packer as you did above is a good choice. I don’t think the performance overhead is that limiting. Also if we convert it manually, in case of composite data objects, we will have to browse the object in all depths to process all JS Date objects. Also since Date is a Native JS data type, I believe we will find this quite regularly

Currently, the existing solution simply logs dates as empty object, so we have loss of information.

We could also support an extra param in construction of FluendDSreamer so that accepts an extra param with a property, ex: packers that will contain an object of types and their packing / unpacking mechanisms in some kind of convention format.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I pass an empty variable value to Date() object in ...
I created a function: fD = D => new Date(D).toDateString().toLocaleString("en-US", { timeZone: "America/New_York" )};.
Read more >
Date() constructor - JavaScript - MDN Web Docs - Mozilla
The Date() constructor can create a Date instance or return a string representing the current time.
Read more >
How to Check if Object is Empty in JavaScript - Samantha Ming
Here's a Code Recipe to check if an object is empty or not. ... new Number(); new Boolean(); new Array(); new RegExp(); new...
Read more >
How to Check Whether an Object is a Date - W3docs
The given piece of code will create either a clone of the value if it is a Date, or create an invalid date....
Read more >
Date field with null and empty string not formatting the dates ...
Scenario 1: If JSON data first object date property null value/ empty string present then date formatting not working
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