Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/34 logger improvements #38

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading