diff --git a/doc/changes/changes_0.2.0.md b/doc/changes/changes_0.2.0.md index 06e8c6d..7d69094 100644 --- a/doc/changes/changes_0.2.0.md +++ b/doc/changes/changes_0.2.0.md @@ -1,14 +1,16 @@ -# Exasol Driver ts 0.2.0, released 2024-??-?? +# Exasol Driver ts 0.2.0, released 2024-11-13 -Code name: TBD +Code name: Connection pool and logging ## Summary Adds `ExasolPool`, which is a connection pool using `ExasolDriver` underneath. See the user guide for a new section on how to configure and use the `ExasolPool` class. +Logging is now configurable for both driver/client and pool. The defaults are now also more sensible (off). An issue with switching off logging was also resolved. ## Features - #28: Add a connection pool. +- #34: Make log level configurable. ## Dependency Updates diff --git a/src/index.ts b/src/index.ts index f10bd65..e0ef467 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,3 +6,4 @@ export * from './lib/commands'; export * from './lib/sql-client.interface'; export * from './lib/statement'; export * from './lib/connection'; +export * from './lib/logger/logger'; diff --git a/src/lib/logger/logger.spec.ts b/src/lib/logger/logger.spec.ts new file mode 100644 index 0000000..ce2d834 --- /dev/null +++ b/src/lib/logger/logger.spec.ts @@ -0,0 +1,44 @@ +import { LogLevel, Logger } from './logger'; + +describe('Logger', () => { + it('Should "console.log()" with default Log level (= debug) when using logger.debug()', () => { + const defaultLogger = new Logger(); + + const logSpy = jest.spyOn(global.console, 'log'); + defaultLogger.debug('hello'); + expect(logSpy).toHaveBeenCalled(); + }); + it('Should "console.log()" with log level debug when using logger.debug()', () => { + const defaultLogger = new Logger(LogLevel.Debug); + + const logSpy = jest.spyOn(global.console, 'log'); + defaultLogger.debug('hello'); + expect(logSpy).toHaveBeenCalled(); + }); + it('Should not "console.log" with log level "off" when using logger.debug()', () => { + const defaultLogger = new Logger(LogLevel.Off); + + const logSpy = jest.spyOn(global.console, 'log'); + defaultLogger.debug('hello'); + expect(logSpy).not.toHaveBeenCalled(); + }); + it('Should not "console.log" with a lower debug level than debug when using logger.debug()', () => { + const defaultLogger = new Logger(LogLevel.Error); + + const logSpy = jest.spyOn(global.console, 'log'); + defaultLogger.debug('hello'); + expect(logSpy).not.toHaveBeenCalled(); + }); + + it('Should "console.log" with a higher debug level than debug when using logger.debug()', () => { + const defaultLogger = new Logger(LogLevel.Trace); + + const logSpy = jest.spyOn(global.console, 'log'); + defaultLogger.debug('hello'); + expect(logSpy).toHaveBeenCalled(); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); +}); diff --git a/src/lib/logger/logger.ts b/src/lib/logger/logger.ts index bfed8eb..c14f080 100644 --- a/src/lib/logger/logger.ts +++ b/src/lib/logger/logger.ts @@ -1,11 +1,11 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ export enum LogLevel { Off = 0, - Error, - Warn, - Info, - Debug, - Trace, + Error = 1, + Warn = 2, + Info = 3, + Debug = 4, + Trace = 5, } export type LoggerMethod = (...data: any) => void; @@ -21,10 +21,8 @@ export interface ILogger { export class Logger implements ILogger { private readonly level: LogLevel = LogLevel.Debug; - constructor(level?: number) { - if (level) { - this.level = level; - } + constructor(level: LogLevel = LogLevel.Debug) { + this.level = level; } private readonly emptyLog = () => { @@ -81,7 +79,7 @@ export class Logger implements ILogger { `%c[${new Date().toISOString()}] %c${level}%c: %s`, 'color: gray', 'color: ' + color, - 'color: inherit' + 'color: inherit', ); } } diff --git a/src/lib/sql-client.ts b/src/lib/sql-client.ts index 809dbc9..226e9ee 100644 --- a/src/lib/sql-client.ts +++ b/src/lib/sql-client.ts @@ -1,6 +1,5 @@ import * as forge from 'node-forge'; - import { getURIScheme } from './utils'; import { CreatePreparedStatementResponse, PublicKeyResponse, SQLQueriesResponse, SQLResponse } from './types'; import { Statement } from './statement'; @@ -68,7 +67,7 @@ export class ExasolDriver implements IExasolDriver { private readonly pool: ConnectionPool; - constructor(websocketFactory: websocketFactory, config: Partial, logger: ILogger = new Logger(LogLevel.Debug)) { + constructor(websocketFactory: websocketFactory, config: Partial, logger: ILogger = new Logger(LogLevel.Off)) { // Used internally to avoid parallel execution this.pool = new ConnectionPool(1, logger); this.config = { diff --git a/src/lib/sql-pool.ts b/src/lib/sql-pool.ts index 174f4c5..6ffcbcd 100644 --- a/src/lib/sql-pool.ts +++ b/src/lib/sql-pool.ts @@ -56,7 +56,7 @@ export class ExasolPool { constructor( websocketFactory: websocketFactory, config: Partial & Partial, - logger: ILogger = new Logger(LogLevel.Debug), + logger: ILogger = new Logger(LogLevel.Off), ) { this.logger = logger; this.internalPool = getPool(websocketFactory, config, logger);