From 3891b5660e7e0f7703a141f1bf0198ec6dbb39cb Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Tue, 28 Aug 2018 16:37:02 +0530 Subject: [PATCH] Add winston logger for different verbosity level --- remix-tests/package.json | 3 +- remix-tests/src/index.js | 1 - remix-tests/src/logger.js | 58 +++++++++++++++++++++++++++++++++++++++ remix-tests/src/run.js | 55 +++++++++++++++++++++++-------------- 4 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 remix-tests/src/logger.js diff --git a/remix-tests/package.json b/remix-tests/package.json index a7ecf7e529..df2d3cab1f 100644 --- a/remix-tests/package.json +++ b/remix-tests/package.json @@ -46,7 +46,8 @@ "signale": "^1.2.1", "solc": "^0.4.24", "standard": "^10.0.3", - "web3": "1.0.0-beta.27" + "web3": "1.0.0-beta.27", + "winston": "^3.0.0" }, "devDependencies": { "mocha": "^5.1.0" diff --git a/remix-tests/src/index.js b/remix-tests/src/index.js index d5e86afc6c..0bd322382d 100644 --- a/remix-tests/src/index.js +++ b/remix-tests/src/index.js @@ -12,7 +12,6 @@ const Web3 = require('web3') const Provider = require('remix-simulator').Provider var createWeb3Provider = function () { - signale.info('Creating providers') let web3 = new Web3() web3.setProvider(new Provider()) return web3 diff --git a/remix-tests/src/logger.js b/remix-tests/src/logger.js new file mode 100644 index 0000000000..6dc6253bf1 --- /dev/null +++ b/remix-tests/src/logger.js @@ -0,0 +1,58 @@ +var gray = require('ansi-gray') +const winston = require('winston') +var timestamp = require('time-stamp') +var supportsColor = require('color-support') + +function hasFlag (flag) { + return ((typeof (process) !== 'undefined') && (process.argv.indexOf('--' + flag) !== -1)) +} + +function addColor (str) { + if (hasFlag('no-color')) { + return str + } + + if (hasFlag('color')) { + return gray(str) + } + + if (supportsColor()) { + return gray(str) + } + + return str +} +function getTimestamp () { + return '[' + addColor(timestamp('HH:mm:ss')) + ']' +} +// create winston logger format +const logFmt = winston.format.printf((info) => { + return `${getTimestamp()} ${info.level}: ${info.message}` +}) + +class Log { + constructor () { + this.logger = winston.createLogger({ + level: 'error', + transports: [new winston.transports.Console()], + format: winston.format.combine( + winston.format.colorize({ all: true }), + logFmt + ) + }) + } + setVerbosity (v) { + this.logger.configure({ + level: v, + transports: [new winston.transports.Console()], + format: winston.format.combine( + winston.format.colorize({ all: true }), + logFmt + ) + }) + } +} + +module.exports = { + Log +} diff --git a/remix-tests/src/run.js b/remix-tests/src/run.js index 5793c1210d..c06c1aace5 100644 --- a/remix-tests/src/run.js +++ b/remix-tests/src/run.js @@ -2,35 +2,48 @@ const commander = require('commander') const Web3 = require('web3') const RemixTests = require('./index.js') const fs = require('fs') -const { Signale } = require('signale') const Provider = require('remix-simulator').Provider +const { Log } = require('./logger.js') +const logger = new Log() +const log = logger.logger require('colors') -// signale configuration -const options = { - types: { - greet: { - badge: '\nšŸ‘', - label: '', - color: 'yellow' - } +// parse verbosity +function mapVerbosity (v) { + const levels = { + 0: 'error', + 1: 'warn', + 2: 'info', + 3: 'verbose', + 4: 'debug', + 5: 'silly' } + return levels[v] } -const signale = new Signale(options) - -commander.action(function (filename) { - signale.greet(('Running remix-tests: Unit testing for solidity.\n').yellow) - let web3 = new Web3() - // web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')) - web3.setProvider(new Provider()) - // web3.setProvider(new web3.providers.WebsocketProvider('ws://localhost:8546')) +// get current version +const pjson = require('../package.json') +commander + .version(pjson.version) + .option('-v, --verbose ', 'run with verbosity', mapVerbosity) + .action(function (filename) { + // Console message + console.log(('\n\tšŸ‘ :: Running remix-tests - Unit testing for solidity :: šŸ‘\t\n').white) + // set logger verbosity + if (commander.verbose) { + logger.setVerbosity(commander.verbose) + log.info('verbosity level set to ' + commander.verbose.blue) + } + let web3 = new Web3() + // web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')) + web3.setProvider(new Provider()) + // web3.setProvider(new web3.providers.WebsocketProvider('ws://localhost:8546')) - let isDirectory = fs.lstatSync(filename).isDirectory() - RemixTests.runTestFiles(filename, isDirectory, web3) -}) + let isDirectory = fs.lstatSync(filename).isDirectory() + RemixTests.runTestFiles(filename, isDirectory, web3) + }) if (!process.argv.slice(2).length) { - signale.fatal('Please specify a filename') + log.error('Please specify a filename') process.exit() }