Winston: Version 3.0.0-rc1 json format not formatting meta string propertly

Created on 23 Oct 2017  ·  4Comments  ·  Source: winstonjs/winston

logger.info(message, "abc");

Output is:
{"0":"a","1":"b","2":"c","level":"info","message":"message"}

I guess this is because the meta string is combined into the object before stringifing, It looks much worse with stack trace (huge array). Maybe is such case output should be:
{"metadata":"abc","level":"info","message":"message"}

docs

Most helpful comment

UPDATE:
simple format has this problem too:
Output:
info: message {"0":"a","1":"b","2":"c"}

All 4 comments

UPDATE:
simple format has this problem too:
Output:
info: message {"0":"a","1":"b","2":"c"}

This can be accomplished with using custom formats in [email protected]. Please consider upgrading.

This is not a supported API call in winston – you are providing a string as metadata, not an object. Here are a few valid API calls:

// winston@2 API:
logger.log('info', 'Hello world', { custom: true });
logger.log('info', new Error('Yo, it\'s on fire'));
logger.log('info', '%s %d%%', 'A string', 50, { thisIsMeta: true });

// winston@3 API – a single JSON literal:
logger.log({ level: 'info', message: 'Hello world', custom: true });
logger.log({ level: 'info', message: new Error('Yo, it\'s on fire') });
logger.log({
  level: 'info',
  message: '%s %d%%',
  splat: ['A string', 50],
  meta: { thisIsMeta: true }
});

If you want output that resembles this:

{"metadata":"abc","level":"info","message":"message"}

then do this:

logger.info('message', { metadata: 'abc' });

... going to leave this ticket open as a documentation bug to better explain how variable arity works in winston's core logging APIs.

@ChrisAlderson FYI the reason this is not a custom format issue is because of this block of code:

const info = Object.assign({}, meta, {
  [LEVEL]: level,
  level,
  message: msg
});

since meta is a string in this case – "abc" it becomes the object {"0":"a","1":"b","2":"c"} which is then the base of the info going forward.

Was this page helpful?
0 / 5 - 0 ratings