mirror of https://github.com/embarklabs/embark.git
refactor(@embark/core): refactor logger to use modern JS APIs
This also removes `parseLogFile()` as it's not used anywhere inside Embark.
This commit is contained in:
parent
6b4052ee7b
commit
8c9e6b11c8
|
@ -36,7 +36,7 @@
|
|||
"ci": "npm run qa",
|
||||
"clean": "npm run reset",
|
||||
"lint": "eslint src/",
|
||||
"qa": "npm-run-all lint _typecheck _build",
|
||||
"qa": "npm-run-all lint _typecheck _build test",
|
||||
"reset": "npx rimraf dist embark-*.tgz package",
|
||||
"solo": "embark-solo",
|
||||
"test": "jest"
|
||||
|
|
|
@ -5,7 +5,6 @@ const { escapeHtml } = require('./utils');
|
|||
const util = require('util');
|
||||
|
||||
const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss:SSS';
|
||||
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);
|
||||
|
||||
export const LogLevels = {
|
||||
error: 'error',
|
||||
|
@ -18,11 +17,9 @@ export const LogLevels = {
|
|||
export class Logger {
|
||||
constructor(options) {
|
||||
this.events = options.events || {emit: function(){}};
|
||||
this.logLevels = Object.keys(LogLevels);
|
||||
this.logLevel = options.logLevel || 'info';
|
||||
this._logFunction = options.logFunction || console.log;
|
||||
this.logFunction = function() {
|
||||
const args = Array.from(arguments);
|
||||
this.logFunction = function(...args) {
|
||||
const color = args[args.length - 1];
|
||||
args.splice(args.length - 1, 1);
|
||||
this._logFunction(...args.filter(arg => arg !== undefined && arg !== null).map(arg => {
|
||||
|
@ -35,62 +32,22 @@ export class Logger {
|
|||
this.logFile = options.logFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
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++;
|
||||
}
|
||||
|
||||
if (matches && matches.length) {
|
||||
logs.push({
|
||||
msg: [matches[3]],
|
||||
logLevel: matches[2],
|
||||
name: 'embark',
|
||||
timestamp: date.parse(matches[1], DATE_FORMAT).getTime()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// if 'limit' is specified, get log lines from the end of the log file
|
||||
if(limit && limit > 0 && logs.length > limit){
|
||||
logs.slice(limit * -1);
|
||||
}
|
||||
return logs;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.prototype.registerAPICall = function (plugins) {
|
||||
const self = this;
|
||||
|
||||
registerAPICall(plugins) {
|
||||
let plugin = plugins.createPlugin('dashboard', {});
|
||||
plugin.registerAPICall(
|
||||
'ws',
|
||||
'/embark-api/logs',
|
||||
(ws, _req) => {
|
||||
self.events.on("log", function (logLevel, logMsg) {
|
||||
this.events.on("log", (logLevel, logMsg) => {
|
||||
logMsg = escapeHtml(logMsg);
|
||||
ws.send(JSON.stringify({msg: logMsg, msg_clear: logMsg.stripColors, logLevel: logLevel}), () => {});
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
Logger.prototype.writeToFile = function (_txt) {
|
||||
writeToFile(...args) {
|
||||
if (!this.logFile) {
|
||||
return;
|
||||
}
|
||||
|
@ -98,71 +55,74 @@ Logger.prototype.writeToFile = function (_txt) {
|
|||
let origin = "[" + ((new Error().stack).split("at ")[3]).trim() + "]";
|
||||
|
||||
const formattedDate = [`[${date.format(new Date(), DATE_FORMAT)}]`]; // adds a timestamp to the logs in the logFile
|
||||
fs.appendFileSync(this.logFile, "\n" + formattedDate.concat(origin, Array.from(arguments)).join(' '));
|
||||
};
|
||||
fs.appendFileSync(this.logFile, "\n" + formattedDate.concat(origin, args).join(' '));
|
||||
}
|
||||
|
||||
Logger.prototype.error = function () {
|
||||
if (!arguments.length || !(this.shouldLog('error'))) {
|
||||
error(...args) {
|
||||
if (!args.length || !(this.shouldLog('error'))) {
|
||||
return;
|
||||
}
|
||||
this.events.emit("log", "error", ...arguments);
|
||||
this.logFunction(...Array.from(arguments), 'red');
|
||||
this.writeToFile("[error]: ", ...arguments);
|
||||
};
|
||||
this.events.emit("log", "error", args);
|
||||
this.logFunction(...Array.from(args), 'red');
|
||||
this.writeToFile("[error]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.warn = function () {
|
||||
if (!arguments.length || !(this.shouldLog('warn'))) {
|
||||
warn(...args) {
|
||||
if (!args.length || !(this.shouldLog('warn'))) {
|
||||
return;
|
||||
}
|
||||
this.events.emit("log", "warn", ...arguments);
|
||||
this.logFunction(...Array.from(arguments), 'yellow');
|
||||
this.writeToFile("[warning]: ", ...arguments);
|
||||
};
|
||||
this.events.emit("log", "warn", args);
|
||||
this.logFunction(...Array.from(args), 'yellow');
|
||||
this.writeToFile("[warning]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.info = function () {
|
||||
if (!arguments.length || !(this.shouldLog('info'))) {
|
||||
info(...args) {
|
||||
if (!args.length || !(this.shouldLog('info'))) {
|
||||
return;
|
||||
}
|
||||
this.events.emit("log", "info", ...arguments);
|
||||
this.logFunction(...Array.from(arguments), 'green');
|
||||
this.writeToFile("[info]: ", ...arguments);
|
||||
};
|
||||
this.events.emit("log", "info", args);
|
||||
this.logFunction(...Array.from(args), 'green');
|
||||
this.writeToFile("[info]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.consoleOnly = function () {
|
||||
if (!arguments.length || !(this.shouldLog('info'))) {
|
||||
consoleOnly(...args) {
|
||||
if (!args.length || !(this.shouldLog('info'))) {
|
||||
return;
|
||||
}
|
||||
this.logFunction(...Array.from(arguments), 'green');
|
||||
this.writeToFile("[consoleOnly]: ", ...arguments);
|
||||
};
|
||||
this.logFunction(...Array.from(args), 'green');
|
||||
this.writeToFile("[consoleOnly]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.debug = function () {
|
||||
if (!arguments.length || !(this.shouldLog('debug'))) {
|
||||
debug(...args) {
|
||||
if (!args.length || !(this.shouldLog('debug'))) {
|
||||
return;
|
||||
}
|
||||
this.events.emit("log", "debug", ...arguments);
|
||||
this.logFunction(...arguments, null);
|
||||
this.writeToFile("[debug]: ", ...arguments);
|
||||
};
|
||||
this.events.emit("log", "debug", args);
|
||||
this.logFunction(args, null);
|
||||
this.writeToFile("[debug]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.trace = function () {
|
||||
if (!arguments.length || !(this.shouldLog('trace'))) {
|
||||
trace(...args) {
|
||||
if (!args.length || !(this.shouldLog('trace'))) {
|
||||
return;
|
||||
}
|
||||
this.events.emit("log", "trace", ...arguments);
|
||||
this.logFunction(...arguments, null);
|
||||
this.writeToFile("[trace]: ", ...arguments);
|
||||
};
|
||||
this.events.emit("log", "trace", args);
|
||||
this.logFunction(args, null);
|
||||
this.writeToFile("[trace]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.dir = function (txt) {
|
||||
dir(...args) {
|
||||
const txt = args[0];
|
||||
if (!txt || !(this.shouldLog('info'))) {
|
||||
return;
|
||||
}
|
||||
this.events.emit("log", "dir", txt);
|
||||
this.logFunction(txt, null);
|
||||
this.writeToFile("[dir]: ", ...arguments);
|
||||
};
|
||||
this.writeToFile("[dir]: ", args);
|
||||
}
|
||||
|
||||
Logger.prototype.shouldLog = function (level) {
|
||||
return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel));
|
||||
};
|
||||
shouldLog(level) {
|
||||
const logLevels = Object.keys(LogLevels);
|
||||
return (logLevels.indexOf(level) <= logLevels.indexOf(this.logLevel));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ describe('core/logger', () => {
|
|||
sinon.restore();
|
||||
});
|
||||
|
||||
test('it should should use custom log function for logging', () => {
|
||||
test('it should use custom log function for logging', () => {
|
||||
logger.info('Hello world');
|
||||
assert(testLogFn.calledOnce);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue