Преглед изворни кода

能输出到文件了,但所有输出都到文件了?

龚成明 пре 2 година
родитељ
комит
7b884ec31c
2 измењених фајлова са 34 додато и 5 уклоњено
  1. 31 5
      libs/task.js
  2. 3 0
      scripts/monitors/price-monitor.js

+ 31 - 5
libs/task.js

@@ -3,20 +3,46 @@ class Task {
   delayTime
   name
   logger
+  fileLogger
 
-  constructor(taskName, delayTime, initFun, onTickFun) {
+  constructor(taskName, delayTime, initFun, onTickFun, logFilePath='task.log') {
     this.name = taskName
     this.delayTime = delayTime
 
     this.init = initFun
     this.onTick = onTickFun
 
-    this.loggerInit()
+    this.consoleLoggerInit()
+    this.fileLoggerInit(logFilePath)
   }
 
-  loggerInit() {
-    this.logger = require('log4js').getLogger(this.name)
-    this.logger.level = 'debug'
+  consoleLoggerInit() {
+    const log4js = require('log4js')
+
+    this.logger = log4js.getLogger(this.name)
+    this.logger.level = log4js.levels.TRACE
+  }
+
+  fileLoggerInit(logFilePath) {
+    const log4js = require('log4js')
+
+    log4js.configure({
+      appenders: {
+        main: {
+          type: 'file',
+          filename: logFilePath
+        }
+      },
+      categories: {
+        default: {
+          appenders: ['main'],
+          level: log4js.levels.TRACE
+        }
+      }
+    })
+
+    this.fileLogger = log4js.getLogger(this.name)
+    this.fileLogger.level = log4js.levels.TRACE
   }
 
   async Start() {

+ 3 - 0
scripts/monitors/price-monitor.js

@@ -3,8 +3,10 @@ const OneInch = require('../../libs/web3/1inch')
 const BinanceSpot = require('../../libs/binance/binance-spot')
 const Config = require('../../config/config')
 
+// TODO 下一步:将特定的价格数据输出到日志文件
 const showPrices = function(context, task) {
   const logger = task.logger
+  const fileLogger = task.fileLogger
   const tokenMap = context.tokenMap
 
   console.clear()
@@ -19,6 +21,7 @@ const showPrices = function(context, task) {
     const BinancePrice = token.BinancePrice
     const distancePrice = BinancePrice - OneInchPrice
 
+    fileLogger.info(`${OneInchPrice}, ${BinancePrice}, ${distancePrice}`)
     console.info(`| ${pair}\t\t| ${OneInchPrice}\t\t| ${BinancePrice}\t\t| ${distancePrice}\t\t|`)
   })
   console.info(`-----------------------------------------------------------------------------------------------------------------`)