node.js - Exception when attempting to wrap Buffered-writer -
disclaimer: i'm new node (but not javascript).
im trying write logger class, holds few lines @ time in memory, flushes disk when buffer reaching capacity
problem
calling logger wrapper results in exception in buffered writer
im sure ive misunderstood how require() works, , various people advised me create object using new chatlogs.chatlogger() iti dont see many other node libs using way of working
/www/im/node_modules/buffered-writer/lib/buffered-writer.js:125 cb (); ^ typeerror: undefined not function @ writer.flush (/www/nodeim/node_modules/buffered-writer/lib/buffered-writer.js:125:3) @ chatlogger.close (/www/nodeim/helpers/chatlogs.js:27:14) @ object.<anonymous> (/www/nodeim/app.js:76:16) @ module._compile (module.js:456:26) @ object.module._extensions..js (module.js:474:10) @ module.load (module.js:356:32) @ function.module._load (module.js:312:12) @ function.module.runmain (module.js:497:10) @ startup (node.js:119:16) @ node.js:901:3
calling code...
var chatlogs = require('./helpers/chatlogs.js'); var chatlogger_obj = new chatlogs.chatlogger(); chatlogger_obj.open("logs/log.txt"); chatlogger_obj.log("testing"); chatlogger_obj.close(); process.exit(0);
wrapper class
./helpers/chatlogs.js
exports.version = '0.0.1'; var buffer = require('buffered-writer'), fs = require('fs'); var chatlogger = function() { this.handle = null, this.filename = "", this.dirtyops = 0; } chatlogger.prototype.open = function (filename) { //fs.unlink(filename); this.filename = filename; this.handle = buffer.open(filename) .on ("error", function (error) { //this.handle = null; console.log (error); }); } chatlogger.prototype.close = function() { console.log("closing"); this.handle.flush(); this.handle.close(); this.handle = null; } chatlogger.prototype.log = function (str) { console.log(str); this.handle.writeln(str); if (this.dirtyops++ > 5) { console.log("flushing"); this.handle.flush(); this.dirtyops = 0; } } module.exports.chatlogger = chatlogger;
i'm author of module. need pass callback flush function, don't need call flush. when buffered-writer closes or exceed buffer size when writing, data automatically flushed disk.
Comments
Post a Comment