first commit
This commit is contained in:
commit
aa7869eb33
386 files changed
+69375
No files matched your search
@@ -0,0 +1,38 @@
|
||||
const fs = require('fs');
|
||||
const _ = require('lodash');
|
||||
|
||||
const BASEPATH = __dirname + '/../../logs/';
|
||||
|
||||
const Logger = function(id) {
|
||||
|
||||
this.fileName = `${id}.log`;
|
||||
|
||||
this.writing = false;
|
||||
this.queue = [];
|
||||
|
||||
_.bindAll(this);
|
||||
}
|
||||
|
||||
Logger.prototype.write = function(line) {
|
||||
if(!this.writing) {
|
||||
this.writing = true;
|
||||
fs.appendFile(
|
||||
BASEPATH + this.fileName,
|
||||
line + '\n',
|
||||
this.handleWriteCallback
|
||||
);
|
||||
} else
|
||||
this.queue.push(line);
|
||||
}
|
||||
|
||||
Logger.prototype.handleWriteCallback = function(err) {
|
||||
if(err)
|
||||
console.error(`ERROR WRITING LOG FILE ${this.fileName}:`, err);
|
||||
|
||||
this.writing = false;
|
||||
|
||||
if(_.size(this.queue))
|
||||
this.write(this.queue.shift())
|
||||
}
|
||||
|
||||
module.exports = Logger;
|
||||
Reference in new issue
Block a user