2017-12-05 23:14:46 +00:00
|
|
|
require('colors');
|
2018-03-10 18:45:56 +00:00
|
|
|
let fs = require('./fs.js');
|
2018-10-03 13:22:42 +00:00
|
|
|
const date = require('date-and-time');
|
2018-10-09 23:15:39 +00:00
|
|
|
const constants = require('../constants');
|
|
|
|
|
|
|
|
const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss:SSS';
|
2018-10-14 07:02:31 +00:00
|
|
|
const LOG_REGEX = new RegExp(/\[(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d:\d\d\d)\] (?:\[(\w*)\]:?)?\s?\s?(.*)/gmi);
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Logger {
|
|
|
|
constructor(options) {
|
2018-03-15 21:18:20 +00:00
|
|
|
this.events = options.events || {emit: function(){}};
|
2018-10-16 15:16:47 +00:00
|
|
|
this.logLevels = Object.keys(Logger.logLevels);
|
2017-03-30 11:12:39 +00:00
|
|
|
this.logLevel = options.logLevel || 'info';
|
|
|
|
this.logFunction = options.logFunction || console.log;
|
2018-04-19 04:25:43 +00:00
|
|
|
this.logFile = options.logFile;
|
2018-10-04 12:22:41 +00:00
|
|
|
this.context = options.context;
|
2018-10-03 13:22:42 +00:00
|
|
|
// Use a default logFile if none is specified in the cli,
|
2018-10-04 12:22:41 +00:00
|
|
|
// in the format .embark/logs/embark_<context>.log.
|
2018-10-03 13:22:42 +00:00
|
|
|
if (!this.logFile) {
|
2018-10-09 23:15:39 +00:00
|
|
|
this.logFile = fs.dappPath(`${constants.logs.logPath}embark_${this.context}.log`);
|
2018-10-04 12:22:41 +00:00
|
|
|
// creates log dir if it doesn't exist, and overwrites existing log file if it exists
|
|
|
|
fs.outputFileSync(this.logFile, '');
|
2018-10-03 13:22:42 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2018-10-09 23:15:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the logFile, returning an array of JSON objects containing the
|
|
|
|
* log messages.
|
|
|
|
* @param {Number} limit specifies how many log messages to return from the
|
|
|
|
* end of the log file
|
|
|
|
* @returns {Array} array containing
|
|
|
|
* - msg: the log message
|
|
|
|
* - logLevel: log level (ie 'info', 'debug')
|
|
|
|
* - name: process name (always "embark")
|
|
|
|
* - timestamp: timestamp of log message (milliseconds since 1/1/1970)
|
|
|
|
*/
|
|
|
|
parseLogFile(limit) {
|
|
|
|
let matches;
|
|
|
|
let logs = [];
|
|
|
|
const logFile = fs.readFileSync(this.logFile, 'utf8');
|
2018-10-14 07:02:31 +00:00
|
|
|
while ((matches = LOG_REGEX.exec(logFile)) !== null) {
|
|
|
|
// This is necessary to avoid infinite loops with zero-width matches
|
|
|
|
if (matches.index === LOG_REGEX.lastIndex) {
|
|
|
|
LOG_REGEX.lastIndex++;
|
|
|
|
}
|
2018-10-09 23:15:39 +00:00
|
|
|
|
2018-10-14 07:02:31 +00:00
|
|
|
if (matches && matches.length) {
|
|
|
|
logs.push({
|
|
|
|
msg: [matches[3]],
|
|
|
|
logLevel: matches[2],
|
|
|
|
name: 'embark',
|
|
|
|
timestamp: date.parse(matches[1], DATE_FORMAT).getTime()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 23:15:39 +00:00
|
|
|
|
|
|
|
// if 'limit' is specified, get log lines from the end of the log file
|
2018-10-10 03:49:10 +00:00
|
|
|
if(limit && limit > 0 && logs.length > limit){
|
2018-10-09 23:15:39 +00:00
|
|
|
logs.slice(limit * -1);
|
|
|
|
}
|
|
|
|
return logs;
|
|
|
|
};
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:16:47 +00:00
|
|
|
Logger.logLevels = {
|
|
|
|
error: 'error',
|
|
|
|
warn: 'warn',
|
|
|
|
info: 'info',
|
|
|
|
debug: 'debug',
|
|
|
|
trace: 'trace'
|
|
|
|
};
|
|
|
|
|
2018-03-13 10:57:02 +00:00
|
|
|
Logger.prototype.registerAPICall = function (plugins) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
let plugin = plugins.createPlugin('dashboard', {});
|
|
|
|
plugin.registerAPICall(
|
|
|
|
'ws',
|
2018-08-01 09:28:25 +00:00
|
|
|
'/embark-api/logs',
|
2018-08-01 15:14:02 +00:00
|
|
|
(ws, _req) => {
|
2018-10-03 13:22:42 +00:00
|
|
|
self.events.on("log", function (logLevel, logMsg) {
|
2018-03-13 10:57:02 +00:00
|
|
|
ws.send(JSON.stringify({msg: logMsg, msg_clear: logMsg.stripColors, logLevel: logLevel}), () => {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2018-10-03 13:22:42 +00:00
|
|
|
};
|
|
|
|
|
2018-08-01 15:14:02 +00:00
|
|
|
Logger.prototype.writeToFile = function (_txt) {
|
2018-10-09 23:15:39 +00:00
|
|
|
const formattedDate = [`[${date.format(new Date(), DATE_FORMAT)}]`]; // adds a timestamp to the logs in the logFile
|
2018-10-03 13:22:42 +00:00
|
|
|
fs.appendFileSync(this.logFile, "\n" + formattedDate.concat(Array.from(arguments)).join(' '));
|
2018-03-10 18:45:56 +00:00
|
|
|
};
|
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.error = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('error'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "error", ...arguments);
|
2018-10-09 23:15:39 +00:00
|
|
|
this.logFunction(...Array.from(arguments).map(t => {return t ? t.red : t;}));
|
2018-06-19 18:06:56 +00:00
|
|
|
this.writeToFile("[error]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.warn = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('warn'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-27 11:34:17 +00:00
|
|
|
this.events.emit("log", "warn", ...arguments);
|
2018-10-09 23:15:39 +00:00
|
|
|
this.logFunction(...Array.from(arguments).map(t => {return t ? t.yellow : t;}));
|
2018-06-19 18:06:56 +00:00
|
|
|
this.writeToFile("[warning]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.info = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('info'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "info", ...arguments);
|
2018-10-09 23:15:39 +00:00
|
|
|
this.logFunction(...Array.from(arguments).map(t => {return t ? t.green : t;}));
|
2018-06-19 18:06:56 +00:00
|
|
|
this.writeToFile("[info]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.debug = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('debug'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "debug", ...arguments);
|
|
|
|
this.logFunction(...arguments);
|
|
|
|
this.writeToFile("[debug]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.trace = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('trace'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "trace", ...arguments);
|
|
|
|
this.logFunction(...arguments);
|
|
|
|
this.writeToFile("[trace]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-04-09 19:24:01 +00:00
|
|
|
Logger.prototype.dir = function (txt) {
|
|
|
|
if (!txt || !(this.shouldLog('info'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.events.emit("log", "dir", txt);
|
|
|
|
this.logFunction(txt);
|
2018-10-03 13:22:42 +00:00
|
|
|
this.writeToFile("[dir]: ", ...arguments);
|
2018-04-09 19:24:01 +00:00
|
|
|
};
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.shouldLog = function (level) {
|
2016-09-17 03:56:25 +00:00
|
|
|
return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel));
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Logger;
|