Skip to content

Commit

Permalink
feat: Implement CustomLogger class
Browse files Browse the repository at this point in the history
  • Loading branch information
spuxx1701 committed Aug 15, 2024
1 parent b2a567d commit 883fa99
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
leadingUnderscore: 'allow',
},
{ selector: 'property', format: ['camelCase', 'UPPER_CASE'], leadingUnderscore: 'allow' },
{ selector: 'property', format: ['camelCase', 'PascalCase', 'UPPER_CASE'], leadingUnderscore: 'allow' },
{ selector: 'property', modifiers: ['requiresQuotes'], format: null },
{ selector: 'method', format: ['camelCase'], leadingUnderscore: 'allow' },
{ selector: 'function', format: ['camelCase', 'PascalCase'] },
Expand Down
38 changes: 38 additions & 0 deletions packages/nest-utils/src/logging/custom.logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TestAppLogger } from '../testing/logging';
import { ApplicationLogLevel, CustomLogger } from './custom.logger';

describe('CustomLogger', () => {
it('should be ok', () => {
const logger = new CustomLogger();
expect(logger).toBeDefined();
expect(logger).toBeInstanceOf(CustomLogger);
});

describe('ApplicationLogLevel', () => {
it('should log the expected amount of messages with default logging enabled', () => {
const logger = new TestAppLogger({
logLevel: ApplicationLogLevel.Default,
});
logger.debug('hello world');
logger.verbose('hello world');
logger.log('hello world');
logger.warn('hello world');
logger.error('hello world');
logger.fatal('hello world');
expect(logger.printMessages).toHaveBeenCalledTimes(4);
});

it('should log the expected amount of messages with verbose logging enabled', () => {
const logger = new TestAppLogger({
logLevel: ApplicationLogLevel.Verbose,
});
logger.debug('hello world');
logger.verbose('hello world');
logger.log('hello world');
logger.warn('hello world');
logger.error('hello world');
logger.fatal('hello world');
expect(logger.printMessages).toHaveBeenCalledTimes(6);
});
});
});
34 changes: 34 additions & 0 deletions packages/nest-utils/src/logging/custom.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ConsoleLogger } from '@nestjs/common';

/**
* The application log level.
*/
export const ApplicationLogLevel = {
Default: 'default',
Verbose: 'verbose',
} as const;
export type ApplicationLogLevel = (typeof ApplicationLogLevel)[keyof typeof ApplicationLogLevel];

/**
* The custom application logger. This logger extends the NestJS console logger, but
* simplifies the API to set the application's log levels.
* @example
* // main.ts
* const app = await NestFactory.create(AppModule, {
* logger: new CustomLogger({ logLevel: ApplicationLogLevel.Verbose }),
* });
*/
export class CustomLogger extends ConsoleLogger {
constructor(options?: { logLevel?: ApplicationLogLevel; context?: string }) {
super();
const { logLevel, context } = { logLevel: 'default', ...options };
if (logLevel === ApplicationLogLevel.Verbose) {
this.setLogLevels(['debug', 'verbose', 'log', 'warn', 'error', 'fatal']);
} else {
this.setLogLevels(['log', 'warn', 'error', 'fatal']);
}
if (context) {
this.setContext(context);
}
}
}
1 change: 1 addition & 0 deletions packages/nest-utils/src/logging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './custom.logger';
1 change: 1 addition & 0 deletions packages/nest-utils/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Otherwise, they will not be included into the bundle.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
export * from './env';
export * from './logging';
export * from './transformers';

// As long as NestJS does not support TypeScript's newer resolution algorithms like 'Node16',
Expand Down
1 change: 1 addition & 0 deletions packages/nest-utils/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
export * from './testing/container';
export * from './testing/supertest';
export * from './testing/logging';
1 change: 1 addition & 0 deletions packages/nest-utils/src/testing/logging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './test.logger';
19 changes: 19 additions & 0 deletions packages/nest-utils/src/testing/logging/test.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CustomLogger } from '../../logging/custom.logger';

/**
* A custom logger for testing purposes. Replaces `printMessages` with a mocked function
* that can be spied on.
* @example
* const logger = new TestAppLogger();
* logger.debug('hello world');
* expect(logger.printMessages).toHaveBeenCalledTimes(1);
*
* // Or hand it over to TestContainer
* const container = await TestContainer.create({
* logger,
* // ...
* });
*/
export class TestAppLogger extends CustomLogger {
printMessages = vitest.fn();
}

0 comments on commit 883fa99

Please sign in to comment.