Skip to content

Commit

Permalink
Feature/34 logger improvements (#38)
Browse files Browse the repository at this point in the history
* Set more sensible defaults for logger in driver/client and pool.

* add logger types to index.ts exports

* fix issue where LogLevel.Off would not correctly be set in the constructor, fixed by using a default in the constructor instead and not doing an 'implicit' check for undefined + gave explicit values to all LogLevel levels.

* Add tests for logger

* Changes doc
  • Loading branch information
pj-spoelders authored Nov 13, 2024
1 parent d1aa19e commit 6ae29e7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 15 deletions.
6 changes: 4 additions & 2 deletions doc/changes/changes_0.2.0.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
44 changes: 44 additions & 0 deletions src/lib/logger/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
18 changes: 8 additions & 10 deletions src/lib/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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',
);
}
}
3 changes: 1 addition & 2 deletions src/lib/sql-client.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -68,7 +67,7 @@ export class ExasolDriver implements IExasolDriver {

private readonly pool: ConnectionPool<Connection>;

constructor(websocketFactory: websocketFactory, config: Partial<Config>, logger: ILogger = new Logger(LogLevel.Debug)) {
constructor(websocketFactory: websocketFactory, config: Partial<Config>, logger: ILogger = new Logger(LogLevel.Off)) {
// Used internally to avoid parallel execution
this.pool = new ConnectionPool<Connection>(1, logger);
this.config = {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/sql-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ExasolPool {
constructor(
websocketFactory: websocketFactory,
config: Partial<Config> & Partial<ClientPoolConfig>,
logger: ILogger = new Logger(LogLevel.Debug),
logger: ILogger = new Logger(LogLevel.Off),
) {
this.logger = logger;
this.internalPool = getPool(websocketFactory, config, logger);
Expand Down

0 comments on commit 6ae29e7

Please sign in to comment.