winstonjs / winston-mongodb

A MongoDB transport for winston
https://github.com/winstonjs/winston-mongodb
296 stars 125 forks source link

MeteorJS integration #112

Open yanickrochon opened 7 years ago

yanickrochon commented 7 years ago

I am using this module with a Meteor app

winston.add(winston.transports.MongoDB, {
  level: 'info',
  silent: Meteor.isProduction,
  db: SystemLogs._driver.mongo.db,
  collection: 'system-logs',
  includeIds: true
});

However, each log entries are created with IDs such as ObjectId("5a1843fcb47a6942ffcb6474"), which is not how other IDs are stored with Meteor, but use the Random.id function.

It would be nice to be able to define an ID function for each log entry, or use MongoDB's default behavior if none is provided. Something like :

winston.add(winston.transports.MongoDB, {
  level: 'info',
  silent: Meteor.isProduction,
  db: SystemLogs._driver.mongo.db,
  collection: SystemLogs._name,
  includeIds: true,
  messageId: Random.id.bind(Random)
});

messageId {Function} : if provided, a function to generate document id to logged entries

yurijmikhalevich commented 6 years ago

Why is it important to use Random.id with Meteor?

yanickrochon commented 6 years ago

Because Meteor handles it's collection either using Strings or ObjectIds with a property called idGeneration. The advantage of using Strings is that searches can be performed using simple equality. Random.id() yields values by default which are 1/2.8651×10²⁶ unique (36^17).

The point is, this feature could allow using this with Meteor apps more easily.

mikkelking commented 2 years ago

I have a fork which allows the options block to pass in a randomId function.

It's here: https://github.com/Back2bikes/winston-mongodb

I haven't added any tests for it, but I can do so and submit a PR