Skip to content

Commit

Permalink
feat(logger): optional subcontext
Browse files Browse the repository at this point in the history
This adds an optional subcontext for loggers to print on every log
statement. This is used in place of wrapping the log methods for the lnd
loggers to display currency, but can also be reused in a generic fashion
going forward.

Before:

```
19/06/2019 15:44:43.634 [LND] info: BTC: trying to verify connection...
```

After:

```
19/06/2019 15:44:43.634 [LND-BTC] info: trying to verify connection...
```
  • Loading branch information
sangaman committed Jun 19, 2019
1 parent d11bee6 commit 205c74e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
45 changes: 35 additions & 10 deletions lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,26 @@ 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 +82,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 +112,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 205c74e

Please sign in to comment.