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

Try better fix for #1485 #57

Merged
merged 4 commits into from
Dec 23, 2018
Merged

Try better fix for #1485 #57

merged 4 commits into from
Dec 23, 2018

Conversation

DABH
Copy link
Contributor

@DABH DABH commented Oct 9, 2018

I can no longer log a single object as a splat:
logger.info('hey %O', obj);
doesn't format the splat right anymore...

But I also want this to work:

const winston = require('winston');

let transports = {
  console: new winston.transports.Console({level: 'info'})
};

let {format} = winston;
let logger = winston.createLogger({
  format: format.combine(
    format.splat(),
    format.printf(info => `[${info.label}]: ${info.message}`)
  ),
  transports: [transports.console]
});

logger.log(
  'info',
  'let\'s %s some %s',
  'interpolate',
  'splat parameters',
  {label: 'label!'},
);
// [label!]: let's interpolate some splat parameters

This PR and an associated one in winston make both of these cases work, but @indexzero would be good if you could opine. IIRC in 3.x we shouldn't be assigning to info.meta like this...

@DABH
Copy link
Contributor Author

DABH commented Oct 9, 2018

@indexzero some tests fail but because they're assuming info.meta rather than meta params being merged into info which seems to be what happens otherwise (but only with splat...). What is the desired behavior?

@GeekyDeaks
Copy link

GeekyDeaks commented Oct 24, 2018

I have been recently poking around as the meta params being merged into info also caused me an issue and came across this PR.

I think this issue is identical to the one I raised (winstonjs/winston#1510), but is also related to winstonjs/winston#1486. Only linking so they can all be closed at the same time.

It only happens if it's the first splat parameter that is an object. I'm still trying to get me head around why this is desired. I am guessing it's for backward compatibility, but I thought the old logging used the last object as the meta, not the first?

@GeekyDeaks
Copy link

IMHO this is polluting the info variable, so it's then difficult to distingush true meta data - even worse, you lose meta elements that collide with winston internals, e.g. level and message.

Is it not better to handle the meta in https://github.com/winstonjs/logform/blob/master/simple.js?

Copy link
Member

@indexzero indexzero left a comment

Choose a reason for hiding this comment

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

Glad we got to the bottom of this head scratcher today @DABH. Let's add test for splat to cover the scenarios that we discussed:

let transports = {
  console: new winston.transports.Console({level: 'info'})
};

let {format} = winston;
let logger = winston.createLogger({
  format: format.combine(
    format.splat(),
    format.simple()
  ),
  transports: [transports.console]
});


logger.log(
  'info',
  'any message',
  { label: 'label!' }
);

logger.log(
  'info',
  'let\'s %s some %s',
  'interpolate',
  'splat parameters',
  { label: 'label!' }
);

const terr = new Error('lol please stop doing this');
terr.enum = true;
logger.log(
  'info',
  'any message',
  terr
);

logger.log(
  'info',
  'let\'s %s some %s',
  'interpolate',
  'splat parameters',
  terr
);

logger.log(
  'info',
  'first is a string %s [[%j]]',
  'behold a string',
  { lol: 'what did charlie do' }
);

logger.log(
  'info',
  'first is an object [[%j]]',
  { lol: 'what did charlie do' }
);

👍 once we have test coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants