Fills embark logs tabs with existing embark logs

A logfile is now generated by default, in the format `.embark/embark-log__YYYY-MM-DD_HH-mm-ss.log`.

When the home tab is loaded, the process logs are fetched for all the processes. The list of processes returned now includes `embark`, and when `/embark-api/process-logs/embark` is fetched, the logFile is parsed and an array of log messages are returned.
This commit is contained in:
emizzle 2018-10-03 23:22:42 +10:00 committed by Pascal Precht
parent 41eb23df82
commit 42cc9b559f
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
4 changed files with 66 additions and 16 deletions

View File

@ -33,23 +33,18 @@ class Console extends Component {
renderTabs() {
const {processLogs, processes, commands} = this.props;
return [
(<Tab title="Embark" key="Embark">
<Logs>
{commands.map((command, index) => <CommandResult key={index} result={command.result}/>)}
</Logs>
</Tab>)
].concat(processes.map(process => (
<Tab title={process.name} key={process.name}>
return processes.map(process => (
<Tab title={process.name} key={process.name} onClick={(e, x) => this.clickTab(e, x)}>
<Logs>
{
processLogs.reverse().filter((item) => item.name === process.name)
.map((item, i) => <p key={i} className={item.logLevel}
dangerouslySetInnerHTML={{__html: convert.toHtml(item.msg)}}></p>)
}
{process.name === "embark" && commands.map((command, index) => <CommandResult key={index} result={command.result}/>)}
</Logs>
</Tab>
)));
));
}
render() {

View File

@ -38,6 +38,9 @@ class Engine {
if (this.interceptLogs || this.interceptLogs === undefined) {
utils.interceptLogs(console, this.logger);
}
// register API calls for the logger
this.logger.registerAPICall(this.plugins);
this.ipc = new IPC({logger: this.logger, ipcRole: this.ipcRole});
if (this.ipc.isClient()) {

View File

@ -1,5 +1,6 @@
require('colors');
let fs = require('./fs.js');
const date = require('date-and-time');
class Logger {
constructor(options) {
@ -8,6 +9,17 @@ class Logger {
this.logLevel = options.logLevel || 'info';
this.logFunction = options.logFunction || console.log;
this.logFile = options.logFile;
this.dateFormat = 'YYYY-MM-DD HH:mm:ss';
this.logRegex = /\[(?<date>\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)\] (?:\[(?<logLevel>\w*)\]:?)?\s?\s?(?<msg>.*)/gmi;
// Use a default logFile if none is specified in the cli,
// in the format .embark/embark-log__YYYY-MM-DD_HH-mm-ss.log.
// This logFile is needed so that when the backend tab starts,
// the initial logs of Embark can be displayed.
if (!this.logFile) {
const now = new Date();
this.logFile = fs.dappPath(`.embark/embark-log__${date.format(now, 'YYYY-MM-DD_HH-mm-ss')}.log`);
}
}
}
@ -27,19 +39,59 @@ Logger.prototype.registerAPICall = function (plugins) {
'ws',
'/embark-api/logs',
(ws, _req) => {
self.events.on("log", function(logLevel, logMsg) {
self.events.on("log", function (logLevel, logMsg) {
ws.send(JSON.stringify({msg: logMsg, msg_clear: logMsg.stripColors, logLevel: logLevel}), () => {});
});
}
);
plugin.registerAPICall(
'get',
'/embark-api/process-logs/embark',
(req, res) => {
res.send(this.parseLogFile());
}
);
};
/**
* Parses the logFile, returning an array of JSON objects containing the
* log messages.
*
* @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)
*/
Logger.prototype.parseLogFile = function () {
let matches;
let logs = [];
const logFile = fs.readFileSync(this.logFile, 'utf8');
while ((matches = this.logRegex.exec(logFile)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (matches.index === this.logRegex.lastIndex) {
this.logRegex.lastIndex++;
}
if(matches && matches.groups){
logs.push({
msg: [matches.groups.msg],
logLevel: matches.groups.logLevel,
name: 'embark',
timestamp: date.parse(matches.groups.date, this.dateFormat).getTime()
});
}
}
return logs;
};
Logger.prototype.writeToFile = function (_txt) {
if (!this.logfile) {
if (!this.logFile) {
return;
}
fs.appendFileSync(this.logFile, "\n" + Array.from(arguments).join(' '));
const formattedDate = [`[${date.format(new Date(), this.dateFormat)}]`]; // adds a timestamp to the logs in the logFile
fs.appendFileSync(this.logFile, "\n" + formattedDate.concat(Array.from(arguments)).join(' '));
};
Logger.prototype.error = function () {
@ -93,8 +145,7 @@ Logger.prototype.dir = function (txt) {
}
this.events.emit("log", "dir", txt);
this.logFunction(txt);
this.writeToFile("[dir]: ");
this.writeToFile(txt);
this.writeToFile("[dir]: ", ...arguments);
};
Logger.prototype.shouldLog = function (level) {

View File

@ -21,7 +21,8 @@ class ProcessManager {
acc.push({state: self.processes[processName].state, name: processName});
return acc;
};
res.send(Object.keys(self.processes).reduce(formatter, []));
// add "embark" process to list of running processes
res.send(Object.keys(self.processes).reduce(formatter, []).concat({ state: "running", name: "embark" }));
}
);
}