diff --git a/lib/Logger.ts b/lib/Logger.ts index 788877f88..9a038878c 100644 --- a/lib/Logger.ts +++ b/lib/Logger.ts @@ -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; @@ -71,7 +82,8 @@ class Logger { }), ]; - if (filename !== '') { + if (filename) { + this.filename = filename; transports.push(new winston.transports.File({ filename, level: this.level, @@ -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}`); } } diff --git a/lib/lndclient/types.ts b/lib/lndclient/types.ts index d17281fe2..484a9c0a6 100644 --- a/lib/lndclient/types.ts +++ b/lib/lndclient/types.ts @@ -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, -}; diff --git a/lib/swaps/SwapClientManager.ts b/lib/swaps/SwapClientManager.ts index 84f634f74..db7d27a2b 100644 --- a/lib/swaps/SwapClientManager.ts +++ b/lib/swaps/SwapClientManager.ts @@ -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'; @@ -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. @@ -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()); diff --git a/test/jest/SwapClientManager.spec.ts b/test/jest/SwapClientManager.spec.ts index d5f9a9400..82f2c49f3 100644 --- a/test/jest/SwapClientManager.spec.ts +++ b/test/jest/SwapClientManager.spec.ts @@ -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());