diff --git a/core/workers/pipeline/messageHandlers/realtimeHandler.js b/core/workers/pipeline/messageHandlers/realtimeHandler.js index 7f72a7b32..6af801736 100644 --- a/core/workers/pipeline/messageHandlers/realtimeHandler.js +++ b/core/workers/pipeline/messageHandlers/realtimeHandler.js @@ -5,14 +5,12 @@ module.exports = cb => { return { message: message => { + if(message.type === 'error') { cb(message.error); console.error(message.error); } - else if(message.type === 'log') - console.log(message.log); - else cb(null, message); diff --git a/logs/.gitignore b/logs/.gitignore new file mode 100644 index 000000000..c96a04f00 --- /dev/null +++ b/logs/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/web/routes/baseConfig.js b/web/routes/baseConfig.js index b950661ff..496c4c307 100644 --- a/web/routes/baseConfig.js +++ b/web/routes/baseConfig.js @@ -6,8 +6,8 @@ var config = {}; // GENERAL SETTINGS // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -config.silent = true; -config.debug = false; +config.silent = false; +config.debug = true; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CONFIGURING TRADING ADVICE diff --git a/web/routes/startGekko.js b/web/routes/startGekko.js index 463d343ac..507bab964 100644 --- a/web/routes/startGekko.js +++ b/web/routes/startGekko.js @@ -4,6 +4,7 @@ const moment = require('moment'); const pipelineRunner = promisify(require('../../core/workers/pipeline/parent')); const cache = require('../state/cache'); +const Logger = require('../state/logger'); const broadcast = cache.get('broadcast'); const apiKeyManager= cache.get('apiKeyManager'); const gekkoManager = cache.get('gekkos'); @@ -49,6 +50,15 @@ module.exports = function *() { let errored = false; + var logType = type; + if(logType === 'leech') { + if(config.trader && config.trader.enabled) + logType = 'tradebot'; + else + logType = 'papertrader'; + } + const logger = new Logger(logType); + console.log('Gekko', id, 'started'); const child = pipelineRunner(mode, config, (err, event) => { @@ -102,6 +112,8 @@ module.exports = function *() { } broadcast(wsEvent); return; + } else if(event.log) { + logger.write(event.log); } let updates = {}; diff --git a/web/state/logger.js b/web/state/logger.js new file mode 100644 index 000000000..fc65930e4 --- /dev/null +++ b/web/state/logger.js @@ -0,0 +1,40 @@ +const fs = require('fs'); +const moment = require('moment'); +const _ = require('lodash'); + +const BASEPATH = __dirname + '/../../logs/'; + +const Logger = function(type) { + + const now = moment().utc().format('YYYY-MM-DD-HH-mm'); + this.fileName = `${now}-UTC-${type}.log`; + + this.writing = false; + this.queue = []; + + _.bindAll(this); +} + +Logger.prototype.write = function(line) { + if(!this.writing) { + this.writing = true; + fs.appendFile( + BASEPATH + this.fileName, + line + '\n', + this.handleWriteCallback + ); + } else + this.queue.push(line); +} + +Logger.prototype.handleWriteCallback = function(err) { + if(err) + console.error(`ERROR WRITING LOG FILE ${this.fileName}:`, err); + + this.writing = false; + + if(_.size(this.queue)) + this.write(this.queue.shift()) +} + +module.exports = Logger; \ No newline at end of file