node.js - Exporting functions to multiple callers in different files -


i'm building logging module can called multiple callers located in different files.

my objective initialize log file @ start of program , have callers call function logs file initialized earlier without going through whole initialisation again.

i can't quite grasp concept of module exports hence i'm hoping can help.

the actual logging occurs on method write. on main app.js file, can initiate , log fine.

however on different file, i'm having mental block on how can log file without going through creating logfile again.

var fs = require('fs');  var fd = {}, log = {}, debug = false;  var tnlog = function(env, file, hostname, procname, pid) {   if (env == 'development')     debug = true;    fd = fs.createwritestream(file, { flags: 'a', encoding: 'utf8', mode: 0644 });   log = { hostname: hostname, procname: procname, pid: pid }; };  tnlog.prototype.write = function(level, str) {   if (debug)   console.log(str);   else {     log.timestamp = date.now();     log.level = level;     log.str = str;     fd.write(json.stringify(log) + '\n');   } };  exports.tnlog = tnlog; 

this how initialize , logging on main file:

var logfile = '/var/log/node/www/app.log'; var tnlog = require('./lib/tnlog').tnlog,     log = new tnlog(app.get('env'), logfile, os.hostname(), appname, process.pid); 

if can suggest better way of doing things, appreciate that.

edit

the simplest solution put

var logfile = '/var/log/node/www/app.log'; var tnlog = require('./lib/tnlog').tnlog, module.exports = new tnlog(app.get('env'), logfile, os.hostname(), appname, process.pid); 

into separate file (mylogger.js), , require anywhere want log logger = require "./mylogger.js . single instance of tnlog, because node caches exported value.

i see might using express,

app.set("logger",new tnlog(app.get('env'), logfile, os.hostname(), appname, process.pid))

and retrieve anywhere have reference app object app.get("logger").

old

more complicated:

you must decide whether want support logging different files while same app running. if so, absolutely need create object each log file. don't have export constructor function per se, export kind of hybrid between factory , singleton pattern.

you create like:

var loggers = {} module.exports = function getlogger(env, file, hostname, procname, pid) {   key = env + file + hostname + procname + pid)   if(loggers[key]) return loggers[key]   return loggers[key] = new logger(env, file, hostname, procname, pid) } 

i.e. check if have created logger object, based on concatenating function argument variables.

you need create proper logger constructor of course, assume know bit of javascript.

note loggers object remain private variable, logger constructor. because node.js caches object module exports, value of loggers object persist on multiple calls require, part of getlogger closure.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -