From 7db783e391604fb4d950362bde17786de68f7146 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Mon, 17 Jul 2017 15:19:28 +0200 Subject: [PATCH] [enh] #1037 Migrate Logger --- .gitignore | 1 + app/lib/computation/BlockchainContext.ts | 2 +- app/lib/dal/drivers/SQLiteDriver.ts | 2 +- app/lib/dal/fileDAL.ts | 2 +- app/lib/dal/fileDALs/ConfDAL.ts | 2 +- app/lib/dal/sqliteDAL/AbstractSQLite.ts | 2 +- app/lib/dal/sqliteDAL/MetaDAL.ts | 2 +- app/lib/{logger/index.js => logger.ts} | 36 ++++++++++++++++++++---- app/lib/logger/callbackLogger.js | 21 -------------- app/lib/streams/multicaster.js | 2 +- app/lib/streams/router.js | 2 +- app/lib/wizard.js | 2 +- app/modules/check-config.js | 2 +- app/modules/reset.js | 2 +- app/modules/wizard.js | 2 +- app/service/BlockchainService.ts | 2 +- app/service/IdentityService.ts | 2 +- app/service/MembershipService.ts | 2 +- app/service/PeeringService.ts | 2 +- app/service/TransactionsService.ts | 2 +- index.js | 2 +- server.js | 2 +- test/fast/prover/pow-1-cluster.js | 2 +- test/fast/prover/pow-2-engine.js | 2 +- test/integration/branches2.js | 2 +- test/integration/forwarding.js | 2 +- test/integration/proof-of-work.js | 2 +- test/integration/tools/commit.js | 2 +- test/integration/tools/node.js | 2 +- test/integration/tools/toolbox.js | 2 +- 30 files changed, 59 insertions(+), 53 deletions(-) rename app/lib/{logger/index.js => logger.ts} (71%) delete mode 100644 app/lib/logger/callbackLogger.js diff --git a/.gitignore b/.gitignore index f19d2281f..b0ce50ee3 100644 --- a/.gitignore +++ b/.gitignore @@ -47,5 +47,6 @@ app/lib/dal/sqliteDAL/*.js* app/lib/dal/sqliteDAL/index/*.js* app/lib/dal/fileDALs/*.js* app/lib/dal/fileDAL.js* +app/lib/logger*js* app/service/*.js* app/lib/wot.js* \ No newline at end of file diff --git a/app/lib/computation/BlockchainContext.ts b/app/lib/computation/BlockchainContext.ts index 2b267ece2..9f4e2b389 100644 --- a/app/lib/computation/BlockchainContext.ts +++ b/app/lib/computation/BlockchainContext.ts @@ -98,7 +98,7 @@ export class BlockchainContext { this.conf = newConf; this.blockchain = theBlockchain this.quickSynchronizer = theQuickSynchronizer - this.logger = require('../logger')(this.dal.profile); + this.logger = require('../logger').NewLogger(this.dal.profile); } checkBlock(block: BlockDTO, withPoWAndSignature = true): Promise<any> { diff --git a/app/lib/dal/drivers/SQLiteDriver.ts b/app/lib/dal/drivers/SQLiteDriver.ts index e2af704fa..3a47dd1b9 100644 --- a/app/lib/dal/drivers/SQLiteDriver.ts +++ b/app/lib/dal/drivers/SQLiteDriver.ts @@ -11,7 +11,7 @@ export class SQLiteDriver { constructor( private path:string ) { - this.logger = require('../../logger')('driver') + this.logger = require('../../logger').NewLogger('driver') } getDB(): Promise<any> { diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index b2f5f0ca3..43dcd1b4f 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -20,7 +20,7 @@ const readline = require('readline') const _ = require('underscore'); const common = require('duniter-common'); const indexer = require('../indexer').Indexer -const logger = require('../logger')('filedal'); +const logger = require('../logger').NewLogger('filedal'); const Configuration = require('../entity/configuration'); const Merkle = require('../entity/merkle'); const Transaction = require('../entity/transaction'); diff --git a/app/lib/dal/fileDALs/ConfDAL.ts b/app/lib/dal/fileDALs/ConfDAL.ts index 590ae8d41..4ae012502 100644 --- a/app/lib/dal/fileDALs/ConfDAL.ts +++ b/app/lib/dal/fileDALs/ConfDAL.ts @@ -11,7 +11,7 @@ export class ConfDAL extends AbstractCFS { constructor(rootPath:string, qioFS:any) { super(rootPath, qioFS) - this.logger = require('../../logger')() + this.logger = require('../../logger').NewLogger() } init() { diff --git a/app/lib/dal/sqliteDAL/AbstractSQLite.ts b/app/lib/dal/sqliteDAL/AbstractSQLite.ts index b74dcf022..f888f168e 100644 --- a/app/lib/dal/sqliteDAL/AbstractSQLite.ts +++ b/app/lib/dal/sqliteDAL/AbstractSQLite.ts @@ -6,7 +6,7 @@ import {SQLiteDriver} from "../drivers/SQLiteDriver" const _ = require('underscore'); const co = require('co'); const colors = require('colors'); -const logger = require('../../logger')('sqlite'); +const logger = require('../../logger').NewLogger('sqlite'); export interface BeforeSaveHook<T> { (t:T): void diff --git a/app/lib/dal/sqliteDAL/MetaDAL.ts b/app/lib/dal/sqliteDAL/MetaDAL.ts index f19f58acf..83d175dcc 100644 --- a/app/lib/dal/sqliteDAL/MetaDAL.ts +++ b/app/lib/dal/sqliteDAL/MetaDAL.ts @@ -11,7 +11,7 @@ import {WalletDAL} from "./WalletDAL"; import {MIndexDAL} from "./index/MIndexDAL"; const _ = require('underscore') -const logger = require('../../logger')('metaDAL'); +const logger = require('../../logger').NewLogger('metaDAL'); const common = require('duniter-common'); const rawer = require('duniter-common').rawer; const constants = require('./../../constants'); diff --git a/app/lib/logger/index.js b/app/lib/logger.ts similarity index 71% rename from app/lib/logger/index.js rename to app/lib/logger.ts index 11b5189d4..74fda628f 100644 --- a/app/lib/logger/index.js +++ b/app/lib/logger.ts @@ -2,7 +2,31 @@ const moment = require('moment'); const path = require('path'); const winston = require('winston'); -const cbLogger = require('./callbackLogger'); + +/*************** + * CALLBACK LOGGER + ***************/ + +const util = require('util'); + +const CallbackLogger = winston.transports.CallbackLogger = function (options:any) { + + this.name = 'customLogger'; + this.level = options.level || 'info'; + this.callback = options.callback; + this.timestamp = options.timestamp; +}; + +util.inherits(CallbackLogger, winston.Transport); + +CallbackLogger.prototype.log = function (level:string, msg:string, meta:any, callback:any) { + this.callback(level, msg, this.timestamp()); + callback(null, true); +}; + +/*************** + * NORMAL LOGGER + ***************/ const customLevels = { levels: { @@ -45,10 +69,10 @@ const logger = new (winston.Logger)({ // Singletons let loggerAttached = false; -logger.addCallbackLogs = (callbackForLog) => { +logger.addCallbackLogs = (callbackForLog:any) => { if (!loggerAttached) { loggerAttached = true; - logger.add(cbLogger, { + logger.add(CallbackLogger, { callback: callbackForLog, level: 'trace', levels: customLevels.levels, @@ -63,7 +87,7 @@ logger.addCallbackLogs = (callbackForLog) => { // Singletons let loggerHomeAttached = false; -logger.addHomeLogs = (home, level) => { +logger.addHomeLogs = (home:string, level:string) => { if (!muted) { if (loggerHomeAttached) { logger.remove(winston.transports.File); @@ -98,4 +122,6 @@ logger.mute = () => { /** * Convenience function to get logger directly */ -module.exports = () => logger; +export function NewLogger() { + return logger +} diff --git a/app/lib/logger/callbackLogger.js b/app/lib/logger/callbackLogger.js deleted file mode 100644 index 08de074c3..000000000 --- a/app/lib/logger/callbackLogger.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; - -const util = require('util'); -const winston = require('winston'); - -const CallbackLogger = winston.transports.CallbackLogger = function (options) { - - this.name = 'customLogger'; - this.level = options.level || 'info'; - this.callback = options.callback; - this.timestamp = options.timestamp; -}; - -util.inherits(CallbackLogger, winston.Transport); - -CallbackLogger.prototype.log = function (level, msg, meta, callback) { - this.callback(level, msg, this.timestamp()); - callback(null, true); -}; - -module.exports = CallbackLogger; diff --git a/app/lib/streams/multicaster.js b/app/lib/streams/multicaster.js index 250bac781..9cbc4d74f 100644 --- a/app/lib/streams/multicaster.js +++ b/app/lib/streams/multicaster.js @@ -12,7 +12,7 @@ const Revocation = require('../../lib/entity/revocation'); const Membership = require('../../lib/entity/membership'); const Block = require('../../lib/entity/block'); const Transaction = require('../../lib/entity/transaction'); -const logger = require('../logger')('multicaster'); +const logger = require('../logger').NewLogger('multicaster'); const WITH_ISOLATION = true; diff --git a/app/lib/streams/router.js b/app/lib/streams/router.js index f97138ab4..b8c75a01f 100644 --- a/app/lib/streams/router.js +++ b/app/lib/streams/router.js @@ -16,7 +16,7 @@ function Router (PeeringService, dal) { dal = theDAL; }; - const logger = require('../logger')('router'); + const logger = require('../logger').NewLogger('router'); stream.Transform.call(this, { objectMode: true }); diff --git a/app/lib/wizard.js b/app/lib/wizard.js index 61b509854..4c113a8d9 100644 --- a/app/lib/wizard.js +++ b/app/lib/wizard.js @@ -3,7 +3,7 @@ const co = require('co'); const constants = require('./constants'); const async = require('async'); const inquirer = require('inquirer'); -const logger = require('./logger')('wizard'); +const logger = require('./logger').NewLogger('wizard'); module.exports = function () { return new Wizard(); diff --git a/app/modules/check-config.js b/app/modules/check-config.js index 614f6e37b..7605ebd1d 100644 --- a/app/modules/check-config.js +++ b/app/modules/check-config.js @@ -3,7 +3,7 @@ const co = require('co'); const constants = require('../lib/constants'); const wizard = require('../lib/wizard'); -const logger = require('../lib/logger')('wizard'); +const logger = require('../lib/logger').NewLogger('wizard'); module.exports = { duniter: { diff --git a/app/modules/reset.js b/app/modules/reset.js index 5786f37a3..cf6e08ffd 100644 --- a/app/modules/reset.js +++ b/app/modules/reset.js @@ -3,7 +3,7 @@ const co = require('co'); const constants = require('../lib/constants'); const wizard = require('../lib/wizard'); -const logger = require('../lib/logger')('wizard'); +const logger = require('../lib/logger').NewLogger('wizard'); module.exports = { duniter: { diff --git a/app/modules/wizard.js b/app/modules/wizard.js index 6786a16ed..014451cab 100644 --- a/app/modules/wizard.js +++ b/app/modules/wizard.js @@ -3,7 +3,7 @@ const Q = require('q'); const co = require('co'); const wizard = require('../lib/wizard'); -const logger = require('../lib/logger')('wizard'); +const logger = require('../lib/logger').NewLogger('wizard'); module.exports = { duniter: { diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts index 2c874f7d7..fb9a28c98 100644 --- a/app/service/BlockchainService.ts +++ b/app/service/BlockchainService.ts @@ -40,7 +40,7 @@ export class BlockchainService { setConfDAL(newConf:ConfDTO, newDAL:FileDAL, newKeyPair:any) { this.dal = newDAL; this.conf = newConf; - this.logger = require('../lib/logger')(this.dal.profile) + this.logger = require('../lib/logger').NewLogger(this.dal.profile) this.quickSynchronizer = new QuickSynchronizer(this.server.blockchain, this.conf, this.dal, this.logger) this.mainContext.setConfDAL(this.conf, this.dal, this.server.blockchain, this.quickSynchronizer) this.selfPubkey = newKeyPair.publicKey; diff --git a/app/service/IdentityService.ts b/app/service/IdentityService.ts index 97a7d9657..25c351971 100644 --- a/app/service/IdentityService.ts +++ b/app/service/IdentityService.ts @@ -26,7 +26,7 @@ export class IdentityService { setConfDAL(newConf:ConfDTO, newDAL:FileDAL) { this.dal = newDAL; this.conf = newConf; - this.logger = require('../lib/logger')(this.dal.profile); + this.logger = require('../lib/logger').NewLogger(this.dal.profile); } searchIdentities(search:string) { diff --git a/app/service/MembershipService.ts b/app/service/MembershipService.ts index 5fa1defa1..f825e18b3 100644 --- a/app/service/MembershipService.ts +++ b/app/service/MembershipService.ts @@ -17,7 +17,7 @@ export class MembershipService { setConfDAL(newConf:ConfDTO, newDAL:FileDAL) { this.dal = newDAL; this.conf = newConf; - this.logger = require('../lib/logger')(this.dal.profile); + this.logger = require('../lib/logger').NewLogger(this.dal.profile); } current() { diff --git a/app/service/PeeringService.ts b/app/service/PeeringService.ts index 31de19ac0..c9305c637 100644 --- a/app/service/PeeringService.ts +++ b/app/service/PeeringService.ts @@ -11,7 +11,7 @@ const events = require('events'); const rp = require('request-promise'); const multicaster = require('../lib/streams/multicaster'); const keyring = require('duniter-common').keyring; -const logger = require('../lib/logger')('peering'); +const logger = require('../lib/logger').NewLogger('peering'); const dos2unix = require('duniter-common').dos2unix; const hashf = require('duniter-common').hashf; const rawer = require('duniter-common').rawer; diff --git a/app/service/TransactionsService.ts b/app/service/TransactionsService.ts index d987d811f..4dc613b16 100644 --- a/app/service/TransactionsService.ts +++ b/app/service/TransactionsService.ts @@ -20,7 +20,7 @@ export class TransactionService { setConfDAL(newConf:ConfDTO, newDAL:FileDAL) { this.dal = newDAL; this.conf = newConf; - this.logger = require('../lib/logger')(this.dal.profile); + this.logger = require('../lib/logger').NewLogger(this.dal.profile); } processTx(txObj:any) { diff --git a/index.js b/index.js index 974d5531b..a7bb57d05 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const Server = require('./server'); const directory = require('./app/lib/system/directory'); const constants = require('./app/lib/constants'); const wizard = require('./app/lib/wizard'); -const logger = require('./app/lib/logger')('duniter'); +const logger = require('./app/lib/logger').NewLogger('duniter'); const configDependency = require('./app/modules/config'); const wizardDependency = require('./app/modules/wizard'); diff --git a/server.js b/server.js index d07d87c2f..4c23e1e65 100644 --- a/server.js +++ b/server.js @@ -24,7 +24,7 @@ function Server (home, memoryOnly, overrideConf) { stream.Duplex.call(this, { objectMode: true }); const paramsP = directory.getHomeParams(memoryOnly, home); - const logger = require('./app/lib/logger')('server'); + const logger = require('./app/lib/logger').NewLogger('server'); const that = this; that.home = home; that.conf = null; diff --git a/test/fast/prover/pow-1-cluster.js b/test/fast/prover/pow-1-cluster.js index 9f1a20b63..130dffe2f 100644 --- a/test/fast/prover/pow-1-cluster.js +++ b/test/fast/prover/pow-1-cluster.js @@ -3,7 +3,7 @@ const co = require('co') const should = require('should') const powCluster = require('../../../app/modules/prover/lib/powCluster') -const logger = require('../../../app/lib/logger')() +const logger = require('../../../app/lib/logger').NewLogger() let master diff --git a/test/fast/prover/pow-2-engine.js b/test/fast/prover/pow-2-engine.js index f9b189905..2a2d301ee 100644 --- a/test/fast/prover/pow-2-engine.js +++ b/test/fast/prover/pow-2-engine.js @@ -3,7 +3,7 @@ const co = require('co'); const should = require('should'); const engine = require('../../../app/modules/prover/lib/engine'); -const logger = require('../../../app/lib/logger')() +const logger = require('../../../app/lib/logger').NewLogger() describe('PoW Engine', () => { diff --git a/test/integration/branches2.js b/test/integration/branches2.js index 30d8bd4fe..9aeb656ff 100644 --- a/test/integration/branches2.js +++ b/test/integration/branches2.js @@ -15,7 +15,7 @@ const expectJSON = httpTest.expectJSON; const expectHttpCode = httpTest.expectHttpCode; if (constants.MUTE_LOGS_DURING_UNIT_TESTS) { - require('../../app/lib/logger')().mute(); + require('../../app/lib/logger').NewLogger().mute(); } // Trace these errors diff --git a/test/integration/forwarding.js b/test/integration/forwarding.js index 6d5d7a7e9..3ab062acc 100644 --- a/test/integration/forwarding.js +++ b/test/integration/forwarding.js @@ -13,7 +13,7 @@ const MEMORY_MODE = true; require('duniter-bma').duniter.methods.noLimit(); // Disables the HTTP limiter if (constants.MUTE_LOGS_DURING_UNIT_TESTS) { - require('../../app/lib/logger')().mute(); + require('../../app/lib/logger').NewLogger().mute(); } describe("Forwarding", function() { diff --git a/test/integration/proof-of-work.js b/test/integration/proof-of-work.js index 9488ed3e1..70ce187f2 100644 --- a/test/integration/proof-of-work.js +++ b/test/integration/proof-of-work.js @@ -5,7 +5,7 @@ const should = require('should'); const toolbox = require('./tools/toolbox'); const Block = require('../../app/lib/entity/block'); const constants = require('../../app/lib/constants'); -const logger = require('../../app/lib/logger')(); +const logger = require('../../app/lib/logger').NewLogger(); const blockProver = require('../../app/modules/prover').duniter.methods.blockProver; /*** diff --git a/test/integration/tools/commit.js b/test/integration/tools/commit.js index 046e59756..6b308127f 100644 --- a/test/integration/tools/commit.js +++ b/test/integration/tools/commit.js @@ -3,7 +3,7 @@ var _ = require('underscore'); var co = require('co'); var rp = require('request-promise'); -var logger = require('../../../app/lib/logger')('test'); +var logger = require('../../../app/lib/logger').NewLogger('test'); module.exports = function makeBlockAndPost(theServer, extraProps) { return function(manualValues) { diff --git a/test/integration/tools/node.js b/test/integration/tools/node.js index fd25c02f4..a1fc07e96 100644 --- a/test/integration/tools/node.js +++ b/test/integration/tools/node.js @@ -25,7 +25,7 @@ var UNTIL_TIMEOUT = 115000; function Node (dbName, options) { - var logger = require('../../../app/lib/logger')(dbName); + var logger = require('../../../app/lib/logger').NewLogger(dbName); var that = this; var started = false; that.server = null; diff --git a/test/integration/tools/toolbox.js b/test/integration/tools/toolbox.js index e9fbc3b92..b464d5c73 100644 --- a/test/integration/tools/toolbox.js +++ b/test/integration/tools/toolbox.js @@ -16,7 +16,7 @@ const bma = require('duniter-bma').duniter.methods.bma; const multicaster = require('../../../app/lib/streams/multicaster'); const dtos = require('duniter-bma').duniter.methods.dtos; const duniter = require('../../../index'); -const logger = require('../../../app/lib/logger')('toolbox'); +const logger = require('../../../app/lib/logger').NewLogger('toolbox'); require('duniter-bma').duniter.methods.noLimit(); // Disables the HTTP limiter -- GitLab