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

Meta is broken again in 5.0.0 #160

Open
arcreigh opened this issue Jun 5, 2019 · 8 comments
Open

Meta is broken again in 5.0.0 #160

arcreigh opened this issue Jun 5, 2019 · 8 comments

Comments

@arcreigh
Copy link

arcreigh commented Jun 5, 2019

The below code does not work when trying to set the metadata currently in version 5.0.0
Rolling back to 4.0.9 was my only option.

is there a new way to setting metadata?

in 3.0.0 you could simply set winston.error(err.message, err) and the second argument would automatically populate the meta field in mongo. now I need to set the parameters via an object in 4.0.9. So what is the new way for 5.0.0?

const winston = require('winston');
module.exports = function(err, req, res, next){
console.log(err);
winston.error({message:err.message, meta:err});
res.status(500).send('something went wrong...');
}
@soldatovsh
Copy link

try this:

new transports.MongoDB({
        ...config.mongoDB,
        format: format.printf((info) => { 
          const splat = info[Symbol.for("splat")]                 
          info.metadata = splat && splat[0] || null
        }),
      })

@leodinas-hao
Copy link

leodinas-hao commented Nov 19, 2019

Have the same issue. I'm using "winston": "3.2.1" with "winston-mongodb": "5.0.1".
The meta data gets lost, but it's there in other transports.

In addition, I tried above, but it doesn't work for me.

Downgrade "winston-mongodb" to v4.0.9 and the issue resolved

@MastroLindus
Copy link

It seems to me that it's not broken, but it has a different API from normal winston.
Winston considers metadata any property in the options object that are not "level" and "message", while this library specifically looks for a metakey property that defaults to "metadata" and is configurable when creating the logger.
I think the behavior should be changed to follow the normal winston API as it is both confusing and not conforming to the other transports

@yurijmikhalevich
Copy link
Member

yurijmikhalevich commented Mar 24, 2020

@MastroLindus changing the API to agree with the default winston API makes sense. The change will be included in the next update.

@HugoPoi
Copy link

HugoPoi commented Jan 15, 2021

Workaround 1

  const winston = require('winston');
  require('winston-mongodb');
  const _ = require('lodash');

  const insertMetaForWinstonMongo = winston.format(logEntry => {
    logEntry.metadata = _.chain(logEntry).omit(logEntry, ['level', 'message']).omitBy((value, key) => _.isSymbol(key)).value();
   // For winston-mongodb < 5.x  I was using
   // logEntry.meta = _.chain(logEntry).omit(logEntry, ['level', 'message']).omitBy((value, key) => _.isSymbol(key)).value();
    return logEntry;
  });

  const logger = winston.createLogger({
        transports: [new winston.transports.MongoDB()],
        format: insertMetaForWinstonMongo(),
      });

EDIT Workaround 2

  const winston = require('winston');
  require('winston-mongodb');

  const insertMetaForWinstonMongo = winston.format(logEntry => {
    logEntry.metadata = logEntry.meta;
    return logEntry;
  });

  const logger = winston.createLogger({
        transports: [new winston.transports.MongoDB()],
        format: winston.format.combine(winston.format.splat(), insertMetaForWinstonMongo()),
      });

It's a breacking change in 5.x

@leviwheatcroft
Copy link

@HugoPoi 's formatter in un-lodash is something like:

const insertMetaForWinstonMongo = winston.format((logEntry) => {
  const metaEntries = Object.entries(logEntry).filter(([key]) => {
    return (key !== 'level' && key !== 'message' && typeof key !== 'symbol')
  })
  logEntry.metadata = Object.fromEntries(metaEntries)
  return logEntry
})

@Rinidh
Copy link

Rinidh commented Jan 26, 2024

@yurijmikhalevich : the winston-mongodb API has not yet been changed to match the winston API to allow easily pass meta data, without extra format configurations

@Rinidh
Copy link

Rinidh commented Jan 26, 2024

Currently, as with winston-mongodb v5.1.1 and winston v3.11.0, only @HugoPoi 's Workaround 1 works as best configurations to apply

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

No branches or pull requests

8 participants