Skip to content

Commit

Permalink
add logging for UI gekkos, fix askmike#972
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Aug 20, 2017
1 parent f531043 commit 315f97e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
4 changes: 1 addition & 3 deletions core/workers/pipeline/messageHandlers/realtimeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
4 changes: 2 additions & 2 deletions web/routes/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var config = {};
// GENERAL SETTINGS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

config.silent = true;
config.debug = false;
config.silent = false;
config.debug = true;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CONFIGURING TRADING ADVICE
Expand Down
12 changes: 12 additions & 0 deletions web/routes/startGekko.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -102,6 +112,8 @@ module.exports = function *() {
}
broadcast(wsEvent);
return;
} else if(event.log) {
logger.write(event.log);
}

let updates = {};
Expand Down
40 changes: 40 additions & 0 deletions web/state/logger.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 315f97e

Please sign in to comment.