Skip to content

Commit

Permalink
feat(service): add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaman committed May 30, 2020
1 parent 46f7a57 commit 8aca0fd
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum Context {
Swaps = 'SWAPS',
Http = 'HTTP',
Backup = 'BACKUP',
Service = 'SERVICE',
}

type Loggers = {
Expand All @@ -46,6 +47,7 @@ type Loggers = {
connext: Logger,
swaps: Logger,
http: Logger,
service: Logger,
};

class Logger {
Expand Down Expand Up @@ -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 }),
};
}

Expand Down
1 change: 1 addition & 0 deletions lib/Xud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class Xud extends EventEmitter {
swapClientManager: this.swapClientManager,
pool: this.pool,
swaps: this.swaps,
logger: loggers.service,
shutdown: this.beginShutdown,
});

Expand Down
4 changes: 4 additions & 0 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions lib/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
};

Expand Down
3 changes: 3 additions & 0 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/jest/Orderbook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const loggers = {
connext: logger,
swaps: logger,
http: logger,
service: logger,
};

const localId = '97945230-8144-11e9-beb7-49ba94e5bd74';
Expand Down
4 changes: 4 additions & 0 deletions test/jest/Service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,6 +21,8 @@ jest.mock('../../lib/p2p/Pool');
const mockedPool = <jest.Mock<Pool>><any>Pool;
jest.mock('../../lib/p2p/Peer');
const mockedPeer = <jest.Mock<Peer>><any>Peer;
jest.mock('../../lib/Logger');
const mockedLogger = <jest.Mock<Logger>><any>Logger;

const getArgs = () => {
return {
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/jest/SwapClientManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const loggers = {
connext: logger,
swaps: logger,
http: logger,
service: logger,
};
jest.mock('../../lib/p2p/Peer');
const mockedPeer = <jest.Mock<Peer>><any>Peer;
Expand Down
1 change: 1 addition & 0 deletions test/jest/integration/Swaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8aca0fd

Please sign in to comment.