-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './custom.logger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './test.logger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |