Skip to content

Commit

Permalink
Merge pull request #1046 from ExchangeUnion/lnd-logger
Browse files Browse the repository at this point in the history
feat(logger): optional subcontext
  • Loading branch information
sangaman authored Jun 24, 2019
2 parents d90f00e + f894a7c commit 926610f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
53 changes: 43 additions & 10 deletions lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,34 @@ class Logger {

private level: string;
private context: Context;
private subcontext?: string;
private logger?: winston.Logger;
private filename?: string;
private instanceId: number;

constructor({ level, filename, context, instanceId, disabled, dateFormat }:
{instanceId?: number, level?: string, filename?: string, context?: Context, disabled?: boolean, dateFormat?: string}) {

this.level = level || Level.Trace;
this.context = context || Context.Global;
this.instanceId = instanceId || 0;
private dateFormat?: string;

constructor({
level = Level.Trace,
filename,
context = Context.Global,
subcontext,
instanceId = 0,
disabled,
dateFormat,
}: {
instanceId?: number,
level?: string,
filename?: string,
context?: Context,
subcontext?: string,
disabled?: boolean,
dateFormat?: string,
}) {
this.level = level;
this.context = context;
this.subcontext = subcontext;
this.instanceId = instanceId;
this.dateFormat = dateFormat;

if (disabled) {
return;
Expand All @@ -71,7 +90,8 @@ class Logger {
}),
];

if (filename !== '') {
if (filename) {
this.filename = filename;
transports.push(new winston.transports.File({
filename,
level: this.level,
Expand Down Expand Up @@ -100,14 +120,27 @@ class Logger {
};
}

public createSubLogger = (subcontext: string) => {
return new Logger({
subcontext,
instanceId: this.instanceId,
level: this.level,
filename: this.filename,
context: this.context,
disabled: this.logger === undefined,
dateFormat: this.dateFormat,
});
}

private getLogFormat = (colorize: boolean, dateFormat?: string) => {
const { format } = winston;

const context = this.subcontext ? `${this.context}-${this.subcontext}` : this.context;
if (this.instanceId > 0) {
return format.printf(info => `${getTsString(dateFormat)} [${this.context}][${this.instanceId}] ` +
return format.printf(info => `${getTsString(dateFormat)} [${context}][${this.instanceId}] ` +
`${this.getLevel(info.level, colorize)}: ${info.message}`);
} else {
return format.printf(info => `${getTsString(dateFormat)} [${this.context}] ${this.getLevel(info.level, colorize)}: ${info.message}`);
return format.printf(info => `${getTsString(dateFormat)} [${context}] ${this.getLevel(info.level, colorize)}: ${info.message}`);
}
}

Expand Down
9 changes: 0 additions & 9 deletions lib/lndclient/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,3 @@ export type Chain = {
network: string,
chain: string,
};

export type LndLogger = {
error: Function,
warn: Function,
info: Function,
verbose: Function,
debug: Function,
trace: Function,
};
21 changes: 3 additions & 18 deletions lib/swaps/SwapClientManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Config from '../Config';
import SwapClient from './SwapClient';
import LndClient from '../lndclient/LndClient';
import { LndLogger, LndInfo } from '../lndclient/types';
import { LndInfo } from '../lndclient/types';
import RaidenClient from '../raidenclient/RaidenClient';
import Logger, { Loggers } from '../Logger';
import { Loggers } from '../Logger';
import { errors } from './errors';
import { Currency } from '../orderbook/types';
import { Models } from '../db/DB';
Expand Down Expand Up @@ -41,21 +41,6 @@ class SwapClientManager extends EventEmitter {
this.raidenClient = new RaidenClient(config.raiden, loggers.raiden);
}

/**
* Wraps each lnd logger call with currency.
* @returns A wrapped lnd logger object.
*/
private static wrapLndLogger = (logger: Logger, currency: string): LndLogger => {
return {
error: (msg: string) => logger.error(`${currency}: ${msg}`),
warn: (msg: string) => logger.warn(`${currency}: ${msg}`),
info: (msg: string) => logger.info(`${currency}: ${msg}`),
verbose: (msg: string) => logger.verbose(`${currency}: ${msg}`),
debug: (msg: string) => logger.debug(`${currency}: ${msg}`),
trace: (msg: string) => logger.trace(`${currency}: ${msg}`),
};
}

/**
* Starts all swap clients, binds event listeners
* and waits for the swap clients to initialize.
Expand All @@ -70,7 +55,7 @@ class SwapClientManager extends EventEmitter {
const lndClient = new LndClient(
lndConfig,
currency,
(SwapClientManager.wrapLndLogger(this.loggers.lnd, currency) as Logger),
this.loggers.lnd.createSubLogger(currency),
);
this.swapClients.set(currency, lndClient);
initPromises.push(lndClient.init());
Expand Down
8 changes: 7 additions & 1 deletion test/jest/SwapClientManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ jest.mock('../../lib/db/DB', () => {
});
});
jest.mock('../../lib/Config');
jest.mock('../../lib/Logger');
jest.mock('../../lib/Logger', () => {
return jest.fn().mockImplementation(() => {
return {
createSubLogger: () => {},
};
});
});
jest.mock('../../lib/nodekey/NodeKey');
const mockLndPubKey = 1;
const lndInfoMock = jest.fn(() => Promise.resolve());
Expand Down

0 comments on commit 926610f

Please sign in to comment.