From ca4f539550e0e55c661c9375ff09c54cea3e2df8 Mon Sep 17 00:00:00 2001 From: Nithin Krishnamurthi <110488533+nithinkdb@users.noreply.github.com> Date: Wed, 26 Oct 2022 16:44:13 -0700 Subject: [PATCH] Fixed bug in Logger, cleaned up types (#83) Fixed logger Signed-off-by: nithinkdb Signed-off-by: nithinkdb --- examples/logging.js | 2 +- lib/DBSQLLogger.ts | 14 +++++++++----- lib/contracts/IDBSQLLogger.ts | 5 +++++ tests/unit/DBSQLOperation.test.js | 2 +- tests/unit/DBSQLSession.test.js | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/logging.js b/examples/logging.js index 06b4b24c..a197184d 100644 --- a/examples/logging.js +++ b/examples/logging.js @@ -3,7 +3,7 @@ const { DBSQLClient, DBSQLLogger, LogLevel } = require('../'); // This logger will emit logs to console and log.txt // -const logger = new DBSQLLogger('log.txt', LogLevel.info); +const logger = new DBSQLLogger({ filepath: 'log.txt', level: LogLevel.info }); const client = new DBSQLClient({ logger: logger }); diff --git a/lib/DBSQLLogger.ts b/lib/DBSQLLogger.ts index b922542f..e4270c80 100644 --- a/lib/DBSQLLogger.ts +++ b/lib/DBSQLLogger.ts @@ -1,12 +1,15 @@ import winston, { Logger } from 'winston'; -import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger'; +import IDBSQLLogger, { LoggerOptions, LogLevel } from './contracts/IDBSQLLogger'; export default class DBSQLLogger implements IDBSQLLogger { logger: Logger; - transports: any; + transports: { + console: winston.transports.ConsoleTransportInstance; + file?: winston.transports.FileTransportInstance; + }; - constructor(filepath?: string, level = LogLevel.info) { + constructor({ level = LogLevel.info, filepath }: LoggerOptions = {}) { this.transports = { console: new winston.transports.Console({ handleExceptions: true, level }), }; @@ -24,8 +27,9 @@ export default class DBSQLLogger implements IDBSQLLogger { } setLevel(level: LogLevel) { - for (const key of Object.keys(this.transports)) { - this.transports[key].level = level; + this.transports.console.level = level; + if (this.transports.file) { + this.transports.file.level = level; } } } diff --git a/lib/contracts/IDBSQLLogger.ts b/lib/contracts/IDBSQLLogger.ts index 01e65af4..7cad3346 100644 --- a/lib/contracts/IDBSQLLogger.ts +++ b/lib/contracts/IDBSQLLogger.ts @@ -1,3 +1,8 @@ +export interface LoggerOptions { + filepath?: string; + level?: LogLevel; +} + export default interface IDBSQLLogger { log(level: LogLevel, message: string): void; } diff --git a/tests/unit/DBSQLOperation.test.js b/tests/unit/DBSQLOperation.test.js index 306a6d12..49e747f2 100644 --- a/tests/unit/DBSQLOperation.test.js +++ b/tests/unit/DBSQLOperation.test.js @@ -9,7 +9,7 @@ const getResult = require('../../dist/DBSQLOperation/getResult').default; // Create logger that won't emit // -const logger = new DBSQLLogger(LogLevel.error); +const logger = new DBSQLLogger({ level: LogLevel.error }); class OperationHandleMock { constructor(hasResultSet = true) { diff --git a/tests/unit/DBSQLSession.test.js b/tests/unit/DBSQLSession.test.js index 2bd622c0..bdb87468 100644 --- a/tests/unit/DBSQLSession.test.js +++ b/tests/unit/DBSQLSession.test.js @@ -7,7 +7,7 @@ const DBSQLOperation = require('../../dist/DBSQLOperation').default; // Create logger that won't emit // -const logger = new DBSQLLogger(LogLevel.error); +const logger = new DBSQLLogger({ level: LogLevel.error }); function createDriverMock(customMethodHandler) { customMethodHandler = customMethodHandler || ((methodName, value) => value);