Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format timestamp and meta independently of log function and allow timestamp to be passed into to log function #634

Closed
wants to merge 7 commits into from

Conversation

JiCiT
Copy link

@JiCiT JiCiT commented May 19, 2015

I had a need to be able to format both timestamp and meta without making a call to common.log, so I separated those into separate functions.

The bigger issue is that I'm doing some logging directly within a database and also feeding the logging info back to node for console, etc. logging. I want the timestamp to be consistent across both the database and other logging, so I want to be able to feed the timestamp directly into the call to log.

I made an effort to ensure that if the timestamp is not passed into the call to log, then existing functionality remains the same.

@indexzero
Copy link
Member

We can't change the method signature to log. Too many third party transports have agreed to that contract.

@indexzero
Copy link
Member

That being said your changes to common.log are very welcome 👍. That method is a beast right now.

@JiCiT
Copy link
Author

JiCiT commented May 19, 2015

That makes sense about so many down-streams having agreed to the contract. I just added a new commit to check the transport.log definition and either include timestamp in the call or not based on transport's defintion.

jason added 2 commits May 31, 2015 00:58
Correct errors in common.formatMeta
  meta vs. options.meta
  logical errors in type checking
logger was forcing meta to always be an object when not defined
@sajacy
Copy link

sajacy commented Jul 14, 2015

A different perspective here: I think instead of going down the path of changing method signatures, it might be better to keep this timestamp coordination functionality out of the logging internals, and instead, use the options.timestamp function to achieve the desired behavior. We can make it essentially a data provider:

If the timestamp function implementation closes over the logger, and subscribes to events for logging, it can be aware of when different transports are logging, and provide them with the same timestamp (i.e. keep a queue, and shift the head off after the desired loggers are done logging). Sketched out what it might look like:

var logger = // winston stuff

// LTC: "locally coordinated time"
var LTC = {
  "ts": [],
  "getTimestamp": function getTimestamp() { return LTC.ts[0]; },
  "log": function log() { 
    LTC.ts.push(new Date()); // this is the timestamp to log
    logger.prototype.log.apply(logger, arguments);  // log stuff...
  }
};

logger.on("logging", function() { LTC.ts.shift(); }); // discard the timestamp

// configure transports:
logger.add(SomeTransport, { "timestamp": LTC.getTimestamp, ... });

@@ -116,16 +116,24 @@ Logger.prototype.extend = function (target) {
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Logger.prototype.log = function (level) {
Logger.prototype.log = function (timestamp, level) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot change this method signature.

@indexzero
Copy link
Member

Just following up here. We really can't change method signatures. If you'd like to update your implementation please let us know.

Your internal changes to common.log are welcome and will likely land in [email protected]

@JiCiT
Copy link
Author

JiCiT commented Nov 4, 2015

I changed the implementation so the function signature hasn't changed. Optional timestamp would still be passed as first parameter.

timestamp,
args;

if (new Date(level).toString() !== 'Invalid Date') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too much of a performance hit. I've been working on a number of benchmarks for comparing [email protected] to [email protected] and this will be a big hit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JiCiT Could you compare new Date(level).toString() to doing isNaN(Date.parse('asd'))? I think the second option could be cheaper.

@indexzero
Copy link
Member

Howdy! Thank you for your contribution. This PR is against the [email protected] release line. Since [email protected] is now out we are only accepting major bug fixes.

This is no longer an issue in winston@3 since the info Objects are no longer decycled or cloned. Please checkout [email protected] today!

npm install winston@next

@indexzero indexzero closed this Oct 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants