Comments (8)
How do you initialise winston-mongodb with preconnected db object. Could you paste a code snippet?
I added a test for preconnected db object here https://github.com/winstonjs/winston-mongodb/blob/master/test/winston-mongodb-preconnected-test.js.
from winston-mongodb.
for node native driver it's MongoClient.connect:
http://mongodb.github.io/node-mongodb-native/2.0/api/MongoClient.html#.connect
and for mongoose it's mongoose.createConnection:
http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection
both are async, the former with a callback, the latter under the covers as mongoose uses the mongo native node driver as well. The mongoose connection returned has a "mongo" property that represents the native driver (what this lib expects for a "existing db connection"). The mongoose.Connection.mongo property initially has a readyState of 2 (connecting) which switches to 1 (connected).
Winston will cache it's requests if opts.db is a string, but not if it isn't. So, it appears to me that everyone using the native driver will have to cache their database (db) requests until the driver's ready. Seems like a hassle, when all you need is the clients to take a promise and simply wait for the database to be ready. Winston doesn't wait, so either I have to figure out how to cache the db requests or simply pass in the mongo conn string, in which case winston will wait, for its own db connection.
The solution is simple enough, if winston allowed opts.db to be a: string, db, AND promise to a db, then we'd use code like:
https://github.com/winstonjs/winston-mongodb/blob/master/lib/winston-mongodb.js#L123
if ('string' === typeof this.db) {
mongodb.MongoClient.connect(this.db, this.options, function(err, db) {
if (err) {
console.error('winston-mongodb: error initialising logger', err);
return;
}
self.mainDb = db;
authorizeDb(function() {
createCollection(processOpQuery);
});
});
} else {
Q(this.db).then(function(db) {
self.mainDb = db;
authorizeDb(function() {
createCollection(processOpQuery);
});
}
}
};
If they pass in a db, fine, the wrapped promise is instantly resolved then, but if they pass in a promise to a db, we wait and cache the log requests until the promise is resolved. Thing is: winston has already implemented the queing code, so just a couple lines of code and it's all done.
from winston-mongodb.
I see what you want. But, as you can see in the test sample, already connected db object should be passed to winston-mongodb. So, you should wait for callback (or promise) in your database initialization code and init winston-mongodb after the connection to database was established.
from winston-mongodb.
All you modules need a logger, so makes sense to require() it, it's that or make it global, in either case how can it be available to them if it's waiting on the db connection? Say in their init code, they want to log something? How could that be done if you were busy waiting on the database connection? If I'm missing something basic, please enlighten me.
from winston-mongodb.
You should init logger somewhere. Init it first (in the beginning in your initialisation code), than connect to db and init your winston-mongodb logger. After you can do anything else, init your application, load other modules and so on. Doesn't this seems legit?
from winston-mongodb.
Oh. I've only seen modules with a list of requires at the top. This had just come to me: the requires can be anywhere, so your main module requires and code could live inside some init callback. Interesting.
from winston-mongodb.
Yes. But this isn't required to achieve behavior I described. There are different approaches. You may have some modules init methods. You may actually launch your application in your init callback, so any logic will be performed strictly after init. If you use only one logger in your application, you may simply require('winston') anywhere you want, and update it with new transports in your initialisation code.
from winston-mongodb.
As long as winston isn't busy waiting on a database connection (modules simply requiring winston to log). I just did a test with the main app code inside a init callback, kinda ugly but then... didn't work. Turns out there's a bug in this module. I'll create another issue for that. I still think it would be terribly easy to allow db promises, as all the queueing code has already been done. So easy in fact, that I can just fork and do it in my own version, all of 6 lines of code or so. In my case I only wait for the mongoose or the logger, mongoose queues up db calls, and I can hack wintson-mongodb to do the same with the logging calls, them simply require both. Or... just pass in the connection string and be done with it.
from winston-mongodb.
Related Issues (20)
- winston.transports.MongoDB pushes empty root path "/" HOT 3
- How to set the db name? HOT 6
- Stop deprecation of pre-connected DB object HOT 9
- Update repo link in npm HOT 1
- Deprecation warning { useUnifiedTopology: true } when using mongoose v>6.x HOT 5
- Authentication error when passing the database HOT 4
- Load balancer mode requires driver version 4+ HOT 14
- TypeError: value.getLowBits is not a function HOT 5
- No overload matches this call
- can you tell me how to add new property . or expireAfterSeconds HOT 2
- after pushing logs to mongoDB script keeps running and is not ending HOT 5
- winston-transport this.once in the code causes app to crash. HOT 2
- How to add custom field?
- NodeJS 20 Deprecation Warning DEP0170 HOT 11
- [Feature Request]: Make CI work HOT 6
- Ignoring level HOT 4
- [Bug]: can't format timestamp using `winston.format.timestamp` HOT 1
- [Bug]: Code snippet hangs up when logging to MongoDB. HOT 2
- [Bug]: Cannot find package "mongodb-extjson" HOT 2
- [Bug]: ObjectId in meta transformed to empty object HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from winston-mongodb.