From 0760965bda6bcfd0f340a1b86c36b70845744453 Mon Sep 17 00:00:00 2001 From: emizzle Date: Thu, 4 Oct 2018 22:22:41 +1000 Subject: [PATCH] =?UTF-8?q?Overwrite=20log=20file=20so=20.embark=20doesn?= =?UTF-8?q?=E2=80=99t=20bloat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The embark log file is being overwritten each time embark is run. There is a separate log file for each context, so that running, for example, `embark run` then `embark console` doesn’t get the `run` log overwritten with the `console` log. --- cmd/cmd_controller.js | 4 ++-- lib/core/engine.js | 2 +- lib/core/fs.js | 11 +++++++++++ lib/core/logger.js | 13 +++++-------- lib/index.js | 2 +- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 27785a35e..47da1639a 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -19,7 +19,7 @@ class EmbarkController { let Config = require('../lib/core/config.js'); this.events = new Events(); - this.logger = new Logger({logLevel: Logger.logLevels.debug, events: this.events}); + this.logger = new Logger({logLevel: Logger.logLevels.debug, events: this.events, context: this.context}); this.config = new Config({env: env, logger: this.logger, events: this.events, context: this.context}); this.config.loadConfigFiles(options); @@ -501,7 +501,7 @@ class EmbarkController { }); let platform; - + async.waterfall([ function initEngine(callback) { engine.init({}, () => { diff --git a/lib/core/engine.js b/lib/core/engine.js index 9fd9b8373..6bf9684cc 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -29,7 +29,7 @@ class Engine { let options = _options || {}; this.events = options.events || this.events || new Events(); - this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile}); + this.logger = options.logger || new Logger({context: this.context, logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile}); this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context, webServerConfig: this.webServerConfig}); this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs}); this.plugins = this.config.plugins; diff --git a/lib/core/fs.js b/lib/core/fs.js index 84ff83c56..65a941285 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -145,6 +145,10 @@ function copyPreserve(sourceFilePath, targetFilePath) { fs.copySync(sourceFilePath, targetFilePath); } +function outputFileSync(){ + return fs.outputFileSync.apply(fs.outputFile, arguments); +} + module.exports = { mkdirpSync, mkdirp, @@ -172,6 +176,13 @@ module.exports = { dappPath, pkgPath, createWriteStream, +<<<<<<< HEAD tmpDir, copyPreserve +||||||| merged common ancestors + tmpDir +======= + tmpDir, + outputFileSync +>>>>>>> Overwrite log file so .embark doesn’t bloat }; diff --git a/lib/core/logger.js b/lib/core/logger.js index 0d0624b36..7e45f0067 100644 --- a/lib/core/logger.js +++ b/lib/core/logger.js @@ -9,16 +9,16 @@ class Logger { this.logLevel = options.logLevel || 'info'; this.logFunction = options.logFunction || console.log; this.logFile = options.logFile; + this.context = options.context; this.dateFormat = 'YYYY-MM-DD HH:mm:ss:SSS'; this.logRegex = /\[(?\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d:\d\d\d)\] (?:\[(?\w*)\]:?)?\s?\s?(?.*)/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. + // in the format .embark/logs/embark_.log. if (!this.logFile) { - const now = new Date(); - this.logFile = fs.dappPath(`.embark/embark-log__${date.format(now, 'YYYY-MM-DD_HH-mm-ss')}.log`); + this.logFile = fs.dappPath(`.embark/logs/embark_${this.context}.log`); + // creates log dir if it doesn't exist, and overwrites existing log file if it exists + fs.outputFileSync(this.logFile, ''); } } } @@ -80,9 +80,6 @@ Logger.prototype.parseLogFile = function () { }; Logger.prototype.writeToFile = function (_txt) { - if (!this.logFile) { - return; - } 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(' ')); }; diff --git a/lib/index.js b/lib/index.js index f8b5c3313..66d781c74 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,7 +13,7 @@ class Embark { let Config = require('./core/config.js'); this.events = new Events(); - this.logger = new Logger({logLevel: 'debug', events: this.events}); + this.logger = new Logger({logLevel: 'debug', events: this.events, context: this.context}); this.config = new Config({env: env, logger: this.logger, events: this.events, context: this.context}); this.config.loadConfigFiles(options);