Scribe.js/lib/logWriter.js

341 lines
7.9 KiB
JavaScript
Raw Normal View History

2014-10-29 11:29:22 +01:00
/* jshint -W098 */
(function() {
2014-10-29 11:29:22 +01:00
'use strict';
var moment = require('moment'),
fs = require('fs'),
2014-10-29 11:29:22 +01:00
mkdirp = require('mkdirp'),
path = require('path');
2014-10-29 11:29:22 +01:00
2014-10-30 18:48:33 +01:00
2014-10-29 11:29:22 +01:00
/**
2014-10-30 18:48:33 +01:00
* rootPaths
*
* Store all rootPaths
* @type {Array}
*/
var rootPaths = [];
/**
* LogWriter
*
* Save console logs on disk
2014-12-20 10:09:14 +01:00
*
2014-10-30 18:48:33 +01:00
* @constructor
*
* @param {String} rootPath root logs folder
*/
var LogWriter = function(rootPath) {
2014-12-20 10:09:14 +01:00
2014-10-30 18:48:33 +01:00
this.rootPath = rootPath || 'logs';
//Check if the folder is already in use
if (rootPaths.indexOf(this.rootPath) > -1) {
throw new Error('Folder ' + this.rootPath + ' already in use');
} else {
rootPaths.push(this.rootPath);
}
//Init history
2014-10-30 19:15:04 +01:00
this.initHistory();
};
/**
* LogWriter.prototype.initHistory
*
* Attach and init the history property
*/
LogWriter.prototype.initHistory = function() {
2014-10-30 18:48:33 +01:00
this.history = {
dates: {}
2014-10-30 18:48:33 +01:00
};
2014-10-30 19:15:04 +01:00
var historyPath = path.join(this.rootPath, 'history.json');
2014-12-20 10:09:14 +01:00
if (!fs.existsSync(historyPath)) { //create file if doesn't exist yet
2014-10-30 19:15:04 +01:00
this.writeFile(
historyPath,
this.history,
function(err) {
2014-10-30 19:15:04 +01:00
if (err) {
throw err;
}
}
);
} else { //get history if file exists
var self = this;
fs.readFile(historyPath, {
"encoding" : "utf-8"
}, function(err, data) {
2014-10-30 18:48:33 +01:00
if (err) {
throw err;
2014-10-30 19:15:04 +01:00
} else {
try {
self.history = JSON.parse(data);
} catch (e) {
throw e;
}
2014-10-30 18:48:33 +01:00
}
2014-10-30 19:15:04 +01:00
});
}
2014-10-30 18:48:33 +01:00
};
/**
* LogWriter.prototype.createDir
2014-10-29 11:29:22 +01:00
*
* Create a dir if it doesn't exist yet
*
* @param {String} path The dir
* @param {Function} callback
*/
LogWriter.prototype.createDir = function(path, callback) {
mkdirp(path, function(err) {
2014-10-29 11:29:22 +01:00
callback(err);
});
};
/**
2014-10-30 18:48:33 +01:00
* LogWriter.prototype.appendFile
2014-10-29 11:29:22 +01:00
*
* Append content to a file
*
* @param {String} pathToFile The file
* @param {String} content
* @param {Function} callback
*/
LogWriter.prototype.appendFile = function(pathToFile, content, callback) {
2014-12-20 10:09:14 +01:00
2014-10-30 18:48:33 +01:00
var self = this;
2014-12-20 10:09:14 +01:00
self.createDir(path.dirname(pathToFile), function(err) {
2014-10-30 18:48:33 +01:00
2014-10-29 11:29:22 +01:00
if (err) {
callback(err);
} else {
2014-12-20 10:09:14 +01:00
2014-10-30 19:15:04 +01:00
var newFile = !fs.existsSync(pathToFile);
2014-10-30 18:48:33 +01:00
fs.appendFile(pathToFile, content, function(err) {
2014-12-20 10:09:14 +01:00
2014-10-30 18:48:33 +01:00
if (err) {
throw err;
} else if (newFile) {
self.newFileHistory(pathToFile);
callback();
}
2014-12-20 10:09:14 +01:00
2014-10-30 18:48:33 +01:00
});
2014-10-29 11:29:22 +01:00
}
});
};
/**
2014-10-30 18:48:33 +01:00
* LogWriter.prototype.writeFile
2014-10-29 11:29:22 +01:00
*
* Write content into a file (erase old one)
*
* @param {String} pathToFile The file
* @param {String} content
* @param {Function} callback
*/
LogWriter.prototype.writeFile = function(pathToFile, content, callback) {
2014-10-30 18:48:33 +01:00
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
this.createDir(path.dirname(pathToFile), function(err) {
2014-10-29 11:29:22 +01:00
if (err) {
callback(err);
} else {
fs.writeFile(pathToFile, content, {
"encoding" : "utf-8"
}, callback);
2014-10-29 11:29:22 +01:00
}
});
};
/**
2014-10-30 18:48:33 +01:00
* LogWriter.prototype.newFileHistory
2014-10-29 11:29:22 +01:00
*
2014-10-30 18:48:33 +01:00
* Save the new file path in history according to the date
*
2014-12-20 10:09:14 +01:00
* @param {String} pathToFile
2014-10-29 11:29:22 +01:00
*/
LogWriter.prototype.newFileHistory = function(pathToFile) {
2014-10-30 18:48:33 +01:00
var historyPath = path.join(this.rootPath, 'history.json'),
self = this;
2014-10-30 18:48:33 +01:00
var today = moment().startOf('day').valueOf().toString();
2014-10-30 18:48:33 +01:00
2014-10-30 19:15:04 +01:00
//Save the path under today key
2014-12-20 10:09:14 +01:00
2014-10-30 19:15:04 +01:00
if (!self.history.dates[today]) {
self.history.dates[today] = [];
}
2014-12-20 10:09:14 +01:00
2014-10-30 19:15:04 +01:00
if (self.history.dates[today].indexOf(pathToFile) === -1) {
2014-10-30 18:48:33 +01:00
2014-10-30 19:15:04 +01:00
self.history.dates[today].push(pathToFile);
2014-10-30 18:48:33 +01:00
self.writeFile(historyPath, self.history, function(err) {
2014-10-30 19:15:04 +01:00
if (err) {
throw err;
2014-10-30 18:48:33 +01:00
}
2014-10-30 19:15:04 +01:00
});
}
};
2014-10-29 11:29:22 +01:00
/**
* LogWriter.prototype.getUser
*
* Tool, return active user
*
* @return {String}
*/
LogWriter.prototype.getUser = function() {
var user = '';
try {
var platformKey = process.platform === 'win32' ? 'USERPROFILE' : 'HOME',
platformDivider = process.platform === 'win32' ? '\\' : '/';
var userDir = process.env[platformKey].toLowerCase();
user = userDir.slice(userDir.lastIndexOf(platformDivider) + 1);
} catch (e) {
user = 'user'; //default
} finally {
return user;
}
};
2014-10-29 11:29:22 +01:00
/**
* LogWriter.prototype.getPath
*
* @param {Object} opt params
* @param {String} opt.logger logger options
2014-10-29 11:29:22 +01:00
*
* @return {String} The path to current folder (without rootPath)
*/
LogWriter.prototype.getPath = function(opt) {
2014-12-20 10:09:14 +01:00
2014-10-29 11:29:22 +01:00
var now = moment();
return path.join(
now.format('YYYY'),
now.format('MMM')
2014-10-29 11:29:22 +01:00
);
};
2014-12-20 10:09:14 +01:00
2014-10-29 11:29:22 +01:00
/**
* LogWriter.prototype.getFile
2014-12-20 10:09:14 +01:00
*
2014-10-29 11:29:22 +01:00
* @param {Object} opt params
* @param {String} opt.logger logger options
2014-10-29 11:29:22 +01:00
*
* @return {String} the filname (with extension)
*/
LogWriter.prototype.getFile = function(opt) {
2014-12-20 10:09:14 +01:00
2014-10-29 11:29:22 +01:00
var now = moment();
return (now.format('DD_MMM_YY')).toLowerCase() +
'.' +
opt.logger.name +
'.json';
};
/**
* LogWriter.prototype.path
*
* @param {Object} opt params
* @param {String} opt.logger logger options
2014-10-29 11:29:22 +01:00
*
* @return {String} the full path to file
*/
LogWriter.prototype.path = function(opt) {
2014-10-29 11:29:22 +01:00
return path.join(
this.rootPath,
this.getPath(opt),
this.getFile(opt)
2014-10-29 11:29:22 +01:00
);
};
/**
* LogWriter.prototype.save
*
* Save a log on disk
*
* @param {Object} log The log to save
2014-10-29 11:39:25 +01:00
*
* @param {Object} opt Options
* @param {String} opt.logger logger options
2014-10-29 11:29:22 +01:00
*/
LogWriter.prototype.save = function(log, opt) {
2014-12-20 10:09:14 +01:00
try {
delete log.opt; //we save logger options in rootPath/[logger].json
} catch(e){
// ignore
}
2014-10-29 11:29:22 +01:00
var json = JSON.stringify(log);
this.appendFile(this.path(opt), json + '\n', function(err) {
2014-10-29 11:29:22 +01:00
if (err) {
throw err;
}
});
};
/**
* LogWriter.prototype.saveOpt
*
* Save logger opt in root folder
*
* @param {Object} logger Logger options.
*/
LogWriter.prototype.saveOpt = function(logger) {
2014-10-29 11:29:22 +01:00
var filePath = path.join(this.rootPath, logger.name + '.json');
2014-10-29 11:29:22 +01:00
this.writeFile(filePath, JSON.stringify(logger), function(err) {
2014-10-29 11:29:22 +01:00
if (err) {
throw err;
}
});
};
2014-10-30 14:28:55 +01:00
/**
2014-12-20 15:16:44 +01:00
* LogWriter.prototype.addLogger
2014-10-30 14:28:55 +01:00
*
* Simply call this.saveOpt() from now
* but could be use for more
*
* @param {Object} logger Console2 logger options
*/
LogWriter.prototype.addLogger = function(logger) {
2014-10-30 14:28:55 +01:00
this.saveOpt(logger);
};
2014-12-20 10:09:14 +01:00
2014-10-30 14:28:55 +01:00
2014-10-29 20:18:27 +01:00
module.exports = {
LogWriter: LogWriter,
folders: rootPaths
2014-10-29 20:18:27 +01:00
};
2014-10-29 11:29:22 +01:00
}());