Skip to content

Commit

Permalink
feature: remove debug params ,only set by server side
Browse files Browse the repository at this point in the history
feature:update logger to gen a log file for any bot
  • Loading branch information
rainfu committed Apr 25, 2023
1 parent 6205648 commit d6dd014
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 52 deletions.
2 changes: 1 addition & 1 deletion commands/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = function (program, conf) {
conf.mode = conf.paper ? 'paper' : 'live'
conf.exchange = exchangename
Object.keys(conf).forEach(function (k) {
if (k !== 'eventBus' && k !== 'secret' && k !== 'db') {
if (k !== 'eventBus' && k !== 'logger' && k !== 'secret' && k !== 'db') {
so[k] = conf[k]
}
})
Expand Down
2 changes: 1 addition & 1 deletion commands/sim.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = function (program, conf) {
var so = s.options
// init bot options don't send this params to client
Object.keys(conf).forEach(function (k) {
if (k !== 'eventBus' && k !== 'secret' && k !== 'db') {
if (k !== 'eventBus' && k !== 'logger' && k !== 'secret' && k !== 'db') {
so[k] = conf[k]
}
})
Expand Down
4 changes: 2 additions & 2 deletions commands/trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var minimist = require('minimist')
, webSocket = require('ws')
, crypto = require('crypto')
, colors = require('colors')
, logger = require('../lib/logger')
module.exports = function (program, conf) {
program
.command('trade [exchange]')
Expand Down Expand Up @@ -52,9 +51,10 @@ module.exports = function (program, conf) {
}
}
var so = s.options
var logger = conf.logger
// init bot options don't send this params to client
Object.keys(conf).forEach(function (k) {
if (k !== 'eventBus' && k !== 'secret' && k !== 'db') {
if (k !== 'eventBus' && k !== 'logger' && k !== 'secret' && k !== 'db') {
so[k] = conf[k]
}
})
Expand Down
1 change: 0 additions & 1 deletion config/conf-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const options = {
future: false,
trade_type: 'auto',
market: 'only_long',
debug: false,
sell_stop_pct: 5,// sell if price drops below this % of bought price (0 to disable)
buy_stop_pct: 0,// buy if price surges above this % of sold price (0 to disable)
max_slippage_pct: 0.5,// avoid trading at a slippage above this pct
Expand Down
5 changes: 0 additions & 5 deletions config/config-structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
],
"value": "live"
},
{
"name": "debug",
"type": "bool",
"value": false
},
{
"name": "other",
"type": "group"
Expand Down
1 change: 0 additions & 1 deletion data/config/binance/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"future": false,
"trade_type": "auto",
"market": "only_long",
"debug": true,
"sell_stop_pct": 5,
"buy_stop_pct": 0,
"max_slippage_pct": 0.5,
Expand Down
1 change: 0 additions & 1 deletion data/config/binanceusdm/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"future": true,
"trade_type": "auto",
"market": "both",
"debug": true,
"sell_stop_pct": 1,
"buy_stop_pct": 0,
"max_slippage_pct": 0.2,
Expand Down
1 change: 0 additions & 1 deletion data/config/binanceusdm/demo2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"conf": "./data/config/binanceusdm/demo2.json",
"name": "demo2",
"with_server": true,
"debug": true,
"period": "1m",
"poll_scan_time": 3000,
"min_periods": 32,
Expand Down
5 changes: 0 additions & 5 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ The basic configuration file is located in the /confg directory, and the file na
list: ['auto', 'autoBuy', 'autoSell', 'manual'],
value: 'live'
},
{
name: 'debug', //whether the debug mode is enabled
type: "bool",
value: false
},
{
name: 'other', // other
type: 'group', // group display
Expand Down
5 changes: 0 additions & 5 deletions docs/zh/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ XCoin通过配置文件初始化机器人,包括以下几类配置文件
list: ['auto', 'autoBuy', 'autoSell', 'manual'],
value: 'live'
},
{
name: 'debug',//调试模式是否开启
type: "bool",
value: false
},
{
name: 'other',//其它
type: 'group',//分组显示
Expand Down
2 changes: 1 addition & 1 deletion lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ let tb = require('timebucket')
, { sendAction } = require('./strategy-helper')
, debug = require('./debug')
, fs = require('fs')
, logger = require('./logger')

let clock
let nice_errors = new RegExp(/(slippage protection|loss protection)/)

module.exports = function (s, conf) {
let eventBus = conf.eventBus
let logger = conf.logger
eventBus.on('kline', onKLine)
eventBus.on('klines', onKLines)
let so = s.options
Expand Down
42 changes: 22 additions & 20 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
const path = require('path')
const log4js = require('log4js')
const dir = process.cwd()

log4js.configure({
appenders: {
console: { type: 'console' },
app: {
type: 'dateFile',
maxLogSize: "10M",
filename: path.join(dir, "./data/logs/app.log")
}
},
categories: {
default: {
appenders: ['console'], level: 'trace',//all log
module.exports = function logger(config) {
console.log('config', config)
log4js.configure({
appenders: {
console: { type: 'console' },
app: {
type: 'dateFile',
maxLogSize: "10M",
filename: path.join(dir, "./data/logs/bot-" + config.user + ".log")
}
},
app: {
appenders: ['app'], level: 'debug',
categories: {
default: {
appenders: ['console'], level: 'trace',//all log
},
app: {
appenders: ['app'], level: 'debug',
},
},
}
})

const logger = log4js.getLogger("app")
module.exports = logger
pm2: config.with_server
})

const logger = log4js.getLogger("app")
return logger
}

17 changes: 9 additions & 8 deletions xcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var path = require('path')
, _ = require('lodash')
, minimist = require('minimist')
, version = require('./package.json').version
, logger = require("./lib/logger")
, loggerInstance = require("./lib/logger")
, EventEmitter = require('events')
, fs = require('fs')

Expand Down Expand Up @@ -60,15 +60,15 @@ function initConfig(args) {
conf = require('./config/conf-base')
secret = require('./config/conf-secret')
} catch (err) {
logger.error(err + ' base config file is not present!')
console.error(err + ' base config file is not present!')
process.exit(1)
}
// 2. load conf overrides file if present or last_config
if (!_.isUndefined(args.conf)) {
try {
overrides = require(path.resolve(process.cwd(), args.conf))
} catch (err) {
logger.error(err + ', failed to load input conf overrides file!')
console.error(err + ', failed to load input conf overrides file!')
}
}
else {
Expand All @@ -84,12 +84,13 @@ function initConfig(args) {
try {
overridesSecret = require(path.resolve(process.cwd(), args.secret))
} catch (err) {
logger.error(err + ', failed to load input secret overrides file!')
console.error(err + ', failed to load input secret overrides file!')
}
}
config = _.defaultsDeep(config, args, overrides, conf)
secret = _.defaultsDeep(overridesSecret, secret)
config.secret = secret
config.logger = loggerInstance(config)
return config
}
/**
Expand All @@ -98,7 +99,7 @@ function initConfig(args) {
* @param {*} args
*/
function initExtra(conf, args) {
module.exports.debug = args.debug || conf.debug
module.exports.debug = args.debug
var eventBus = new EventEmitter()
conf.eventBus = eventBus
}
Expand All @@ -125,9 +126,9 @@ function initDb(xcoin, conf, cb) {
}
require('mongodb').MongoClient.connect(connectionString, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {
if (err) {
logger.error('WARNING: MongoDB Connection Error: ', err)
logger.error('WARNING: without MongoDB some features (such as backfilling/simulation) may be disabled.')
logger.error('Attempted authentication string: ' + connectionString)
console.error('WARNING: MongoDB Connection Error: ', err)
console.error('WARNING: without MongoDB some features (such as backfilling/simulation) may be disabled.')
console.error('Attempted authentication string: ' + connectionString)
if (cb) cb(null, xcoin)
return
}
Expand Down

0 comments on commit d6dd014

Please sign in to comment.