Skip to content

Commit

Permalink
Fixed bug in Logger, cleaned up types (#83)
Browse files Browse the repository at this point in the history
Fixed logger

Signed-off-by: nithinkdb <[email protected]>

Signed-off-by: nithinkdb <[email protected]>
  • Loading branch information
nithinkdb authored Oct 26, 2022
1 parent f609014 commit ca4f539
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
14 changes: 9 additions & 5 deletions lib/DBSQLLogger.ts
Original file line number Diff line number Diff line change
@@ -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 }),
};
Expand All @@ -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;
}
}
}
5 changes: 5 additions & 0 deletions lib/contracts/IDBSQLLogger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export interface LoggerOptions {
filepath?: string;
level?: LogLevel;
}

export default interface IDBSQLLogger {
log(level: LogLevel, message: string): void;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/DBSQLOperation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/DBSQLSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ca4f539

Please sign in to comment.