there is nothing to complain about, aside of self modifying code and
violation of the principle of least astonishment. but there are (at least)
2 other ways to do this without code modification:
1.
module.exports = function(opts){
if( ! isInitialized ){
// do something with opts
// if initalization is mandatory do defaults silently or throw an error
}
return bunyan.createLogger()
}
var logger = require(./logger)(opts)
2.
module.exports = function(opts){
if( ! isInitialized ){
// if initalization is mandatory do defaults silently or throw an error
}
return bunyan.createLogger()
}
module.exports.init = function() {
if( ! isInitialized ){
// do something with opts
}
}
var logger = require(./logger)()
logger.init(opts)
if you want less parens on user side you could make your logger function
polymorphic, but it may be too confusing.
Am Freitag, 22. März 2013 06:50:04 UTC+1 schrieb Nabeel S.:
>
> Hi all,
>
> I've been meaning to post this up, as it's a pattern I've been using with
> any modules that act as a wrapper to something.
> I just wanted to get some feedback, as I haven't seen this explicitly
> anywhere, and I am curious if I may be causing problems down the line.
>
> I have quite a few apps, which all share a common set of libraries, in a
> package; ie: logging, configuration, database, etc.
> I'll take logging as an example, I use bunyan.
>
> exports.init = function(opts) {
>
> // Do something with options
> // There's a bunch of stuff for different streams to different services
>
> var logger = bunyan.createLogger(...);
> module.exports = logger;
> }
>
> Then in the app initialization:
>
> require('package/lib/logger').init({ ... });
>
> And now in some other module:
>
> var logger = require('package/lib/logger');
>
> logger.debug(...);
>
> Basically replacing the exports with the instance, and then it can be
> called from anywhere, and accessed.
> So my question would be - aside from losing the ability to re-initialize
> (these should theoretically only be initialized once) - is there any
> downside to this pattern; in terms of memory or efficiency?
>
> Nabeel
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.