From 8aca0fdd5033d2d8aa0cd5d8532f35ebc9d0c5ae Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Thu, 28 May 2020 00:48:57 -0400 Subject: [PATCH] feat(service): add logger --- lib/Logger.ts | 3 +++ lib/Xud.ts | 1 + lib/service/Service.ts | 4 ++++ lib/service/types.ts | 6 ++++-- lib/swaps/Swaps.ts | 3 +++ test/jest/Orderbook.spec.ts | 1 + test/jest/Service.spec.ts | 4 ++++ test/jest/SwapClientManager.spec.ts | 1 + test/jest/integration/Swaps.spec.ts | 1 + 9 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/Logger.ts b/lib/Logger.ts index 78523ce80..5dc1481bf 100644 --- a/lib/Logger.ts +++ b/lib/Logger.ts @@ -33,6 +33,7 @@ export enum Context { Swaps = 'SWAPS', Http = 'HTTP', Backup = 'BACKUP', + Service = 'SERVICE', } type Loggers = { @@ -46,6 +47,7 @@ type Loggers = { connext: Logger, swaps: Logger, http: Logger, + service: Logger, }; class Logger { @@ -121,6 +123,7 @@ class Logger { connext: new Logger({ ...object, context: Context.Connext }), swaps: new Logger({ ...object, context: Context.Swaps }), http: new Logger({ ...object, context: Context.Http }), + service: new Logger({ ...object, context: Context.Service }), }; } diff --git a/lib/Xud.ts b/lib/Xud.ts index 18c9f39f3..08402caa8 100644 --- a/lib/Xud.ts +++ b/lib/Xud.ts @@ -203,6 +203,7 @@ class Xud extends EventEmitter { swapClientManager: this.swapClientManager, pool: this.pool, swaps: this.swaps, + logger: loggers.service, shutdown: this.beginShutdown, }); diff --git a/lib/service/Service.ts b/lib/service/Service.ts index 4043d5792..75f51d858 100644 --- a/lib/service/Service.ts +++ b/lib/service/Service.ts @@ -1,6 +1,7 @@ import { ProvidePreimageEvent, TransferReceivedEvent } from '../connextclient/types'; import { OrderSide, Owner, SwapClientType, SwapRole } from '../constants/enums'; import { OrderAttributes, TradeInstance } from '../db/types'; +import Logger from '../Logger'; import OrderBook from '../orderbook/OrderBook'; import { Currency, isOwnOrder, Order, OrderPortion, OwnLimitOrder, OwnMarketOrder, PlaceOrderEvent } from '../orderbook/types'; import Pool from '../p2p/Pool'; @@ -57,6 +58,7 @@ class Service { private pool: Pool; private version: string; private swaps: Swaps; + private logger: Logger; /** Create an instance of available RPC methods and bind all exposed functions. */ constructor(components: ServiceComponents) { @@ -65,6 +67,7 @@ class Service { this.swapClientManager = components.swapClientManager; this.pool = components.pool; this.swaps = components.swaps; + this.logger = components.logger; this.version = components.version; } @@ -678,6 +681,7 @@ class Service { */ public subscribeSwapFailures = async (args: { includeTaker: boolean }, callback: (swapFailure: SwapFailure) => void) => { this.swaps.on('swap.failed', (deal) => { + this.logger.trace(`notifying SwapFailure subscription for ${deal.rHash} with role ${SwapRole[deal.role]}`); // always alert client for maker matches, taker matches only when specified if (deal.role === SwapRole.Maker || args.includeTaker) { callback(deal as SwapFailure); diff --git a/lib/service/types.ts b/lib/service/types.ts index 846435076..f8be72de4 100644 --- a/lib/service/types.ts +++ b/lib/service/types.ts @@ -7,6 +7,7 @@ import Pool from '../p2p/Pool'; import { RaidenInfo } from '../raidenclient/types'; import SwapClientManager from '../swaps/SwapClientManager'; import Swaps from '../swaps/Swaps'; +import Logger from 'lib/Logger'; /** * The components required by the API service layer. @@ -15,10 +16,11 @@ export type ServiceComponents = { orderBook: OrderBook; swapClientManager: SwapClientManager; pool: Pool; - /** The version of the local xud instance. */ + /** The version of the local xud instance. */ version: string; swaps: Swaps; - /** The function to be called to shutdown the parent process */ + logger: Logger; + /** The function to be called to shutdown the parent process */ shutdown: () => void; }; diff --git a/lib/swaps/Swaps.ts b/lib/swaps/Swaps.ts index 4ee95f729..9bf00384f 100644 --- a/lib/swaps/Swaps.ts +++ b/lib/swaps/Swaps.ts @@ -1095,6 +1095,8 @@ class Swaps extends EventEmitter { }) => { assert(deal.state !== SwapState.Completed, 'Can not fail a completed deal.'); + this.logger.trace(`failing deal ${deal.rHash} in state ${SwapState[deal.state]} & phase ${SwapPhase[deal.phase]} due to ${SwapFailureReason[failureReason]}`); + // If we are already in error state and got another error report we // aggregate all error reasons by concatenation if (deal.state === SwapState.Error) { @@ -1168,6 +1170,7 @@ class Swaps extends EventEmitter { swapClient.removeInvoice(deal.rHash).catch(this.logger.error); // we don't need to await the remove invoice call } + this.logger.trace(`emitting swap.failed event for ${deal.rHash}`); this.emit('swap.failed', deal); if (peer) { diff --git a/test/jest/Orderbook.spec.ts b/test/jest/Orderbook.spec.ts index 0878faaf9..ca4885401 100644 --- a/test/jest/Orderbook.spec.ts +++ b/test/jest/Orderbook.spec.ts @@ -105,6 +105,7 @@ const loggers = { connext: logger, swaps: logger, http: logger, + service: logger, }; const localId = '97945230-8144-11e9-beb7-49ba94e5bd74'; diff --git a/test/jest/Service.spec.ts b/test/jest/Service.spec.ts index a6264978f..58c45b42b 100644 --- a/test/jest/Service.spec.ts +++ b/test/jest/Service.spec.ts @@ -1,4 +1,5 @@ import { Owner } from '../../lib/constants/enums'; +import Logger from '../../lib/Logger'; import Orderbook from '../../lib/orderbook/OrderBook'; import Peer from '../../lib/p2p/Peer'; import Pool from '../../lib/p2p/Pool'; @@ -20,6 +21,8 @@ jest.mock('../../lib/p2p/Pool'); const mockedPool = >Pool; jest.mock('../../lib/p2p/Peer'); const mockedPeer = >Peer; +jest.mock('../../lib/Logger'); +const mockedLogger = >Logger; const getArgs = () => { return { @@ -41,6 +44,7 @@ describe('Service', () => { swaps: new mockedSwaps(), version: '1.0.0', shutdown: jest.fn(), + logger: new mockedLogger(), }; peer = new mockedPeer(); components.pool.getPeer = jest.fn().mockReturnValue(peer); diff --git a/test/jest/SwapClientManager.spec.ts b/test/jest/SwapClientManager.spec.ts index 4e5f0f158..7732f5f5a 100644 --- a/test/jest/SwapClientManager.spec.ts +++ b/test/jest/SwapClientManager.spec.ts @@ -81,6 +81,7 @@ const loggers = { connext: logger, swaps: logger, http: logger, + service: logger, }; jest.mock('../../lib/p2p/Peer'); const mockedPeer = >Peer; diff --git a/test/jest/integration/Swaps.spec.ts b/test/jest/integration/Swaps.spec.ts index 63386666a..1f6c971a4 100644 --- a/test/jest/integration/Swaps.spec.ts +++ b/test/jest/integration/Swaps.spec.ts @@ -76,6 +76,7 @@ describe('Swaps Integration', () => { logger = new mockedLogger(); logger.debug = jest.fn(); logger.error = jest.fn(); + logger.trace = jest.fn(); db = new mockedDB(); pool = new mockedPool(); swapClientManager = new mockedSwapClientManager();