-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
feat(common): add force-console option to console logger #15503
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
Merged
kamilmysliwiec
merged 2 commits into
nestjs:master
from
mag123c:feat/add-forceconsole-option-to-logger
Sep 16, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,163 @@ | ||
| import { ConsoleLogger, INestApplication } from '@nestjs/common'; | ||
| import { Test } from '@nestjs/testing'; | ||
| import * as request from 'supertest'; | ||
| import { AppModule } from '../src/app.module'; | ||
| import * as sinon from 'sinon'; | ||
| import { expect } from 'chai'; | ||
|
|
||
| describe('ForceConsole Option', () => { | ||
| let app: INestApplication; | ||
|
|
||
| describe('When forceConsole is true', () => { | ||
| let consoleLogSpy: sinon.SinonSpy; | ||
| let consoleErrorSpy: sinon.SinonSpy; | ||
| let processStdoutSpy: sinon.SinonSpy; | ||
| let processStderrSpy: sinon.SinonSpy; | ||
|
|
||
| beforeEach(async () => { | ||
| // Spy on console and process methods | ||
| consoleLogSpy = sinon.spy(console, 'log'); | ||
| consoleErrorSpy = sinon.spy(console, 'error'); | ||
| processStdoutSpy = sinon.spy(process.stdout, 'write'); | ||
| processStderrSpy = sinon.spy(process.stderr, 'write'); | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| imports: [AppModule], | ||
| }).compile(); | ||
|
|
||
| app = moduleRef.createNestApplication({ | ||
| forceConsole: true, | ||
| logger: ['log', 'error'], | ||
| }); | ||
|
|
||
| await app.init(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| consoleLogSpy.restore(); | ||
| consoleErrorSpy.restore(); | ||
| processStdoutSpy.restore(); | ||
| processStderrSpy.restore(); | ||
| await app.close(); | ||
| }); | ||
|
|
||
| it('should use console.log instead of process.stdout.write', async () => { | ||
| const logger = new ConsoleLogger('TestContext', { forceConsole: true }); | ||
| logger.log('Test log message'); | ||
|
|
||
| // Should use console.log when forceConsole is true | ||
| expect(consoleLogSpy.called).to.be.true; | ||
| // Verify console.log was called with the message | ||
| const consoleLogCalls = consoleLogSpy | ||
| .getCalls() | ||
| .filter(call => | ||
| call.args.some(arg => String(arg).includes('Test log message')), | ||
| ); | ||
| expect(consoleLogCalls.length).to.be.greaterThan(0); | ||
| }); | ||
|
|
||
| it('should use console.error instead of process.stderr.write', async () => { | ||
| const logger = new ConsoleLogger('TestContext', { forceConsole: true }); | ||
| logger.error('Test error message'); | ||
|
|
||
| // Should use console.error when forceConsole is true | ||
| expect(consoleErrorSpy.called).to.be.true; | ||
| // Verify console.error was called with the message | ||
| const consoleErrorCalls = consoleErrorSpy | ||
| .getCalls() | ||
| .filter(call => | ||
| call.args.some(arg => String(arg).includes('Test error message')), | ||
| ); | ||
| expect(consoleErrorCalls.length).to.be.greaterThan(0); | ||
| }); | ||
|
|
||
| it('should handle GET request with forceConsole option enabled', () => { | ||
| return request(app.getHttpServer()).get('/hello').expect(200); | ||
| }); | ||
| }); | ||
|
|
||
| describe('When forceConsole is false (default)', () => { | ||
| let consoleLogSpy: sinon.SinonSpy; | ||
| let consoleErrorSpy: sinon.SinonSpy; | ||
| let processStdoutSpy: sinon.SinonSpy; | ||
| let processStderrSpy: sinon.SinonSpy; | ||
|
|
||
| beforeEach(async () => { | ||
| // Spy on console and process methods | ||
| consoleLogSpy = sinon.spy(console, 'log'); | ||
| consoleErrorSpy = sinon.spy(console, 'error'); | ||
| processStdoutSpy = sinon.spy(process.stdout, 'write'); | ||
| processStderrSpy = sinon.spy(process.stderr, 'write'); | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| imports: [AppModule], | ||
| }).compile(); | ||
|
|
||
| app = moduleRef.createNestApplication({ | ||
| logger: ['log', 'error'], | ||
| // forceConsole is not set, defaults to false | ||
| }); | ||
|
|
||
| await app.init(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| consoleLogSpy.restore(); | ||
| consoleErrorSpy.restore(); | ||
| processStdoutSpy.restore(); | ||
| processStderrSpy.restore(); | ||
| await app.close(); | ||
| }); | ||
|
|
||
| it('should not directly call console.log when forceConsole is false', async () => { | ||
| const logger = new ConsoleLogger('TestContext'); | ||
|
|
||
| // Reset spy to ensure clean state | ||
| consoleLogSpy.resetHistory(); | ||
|
|
||
| logger.log('Test log message'); | ||
|
|
||
| // When forceConsole is false, should not call console.log | ||
| expect(consoleLogSpy.called).to.be.false; | ||
| }); | ||
|
|
||
| it('should not directly call console.error when forceConsole is false', async () => { | ||
| const logger = new ConsoleLogger('TestContext'); | ||
|
|
||
| // Reset spy to ensure clean state | ||
| consoleErrorSpy.resetHistory(); | ||
|
|
||
| logger.error('Test error message'); | ||
|
|
||
| // When forceConsole is false, should not call console.error | ||
| expect(consoleErrorSpy.called).to.be.false; | ||
| }); | ||
| }); | ||
|
|
||
| describe('When forceConsole is set via NestFactory.create', () => { | ||
| it('should apply forceConsole to the default logger', async () => { | ||
| const consoleLogSpy = sinon.spy(console, 'log'); | ||
| const processStdoutSpy = sinon.spy(process.stdout, 'write'); | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| imports: [AppModule], | ||
| }).compile(); | ||
|
|
||
| const testApp = moduleRef.createNestApplication({ | ||
| forceConsole: true, | ||
| }); | ||
|
|
||
| await testApp.init(); | ||
|
|
||
| // The logger created by NestFactory should respect forceConsole option | ||
| const logger = new ConsoleLogger('AppContext', { forceConsole: true }); | ||
| logger.log('Application started'); | ||
|
|
||
| expect(consoleLogSpy.called).to.be.true; | ||
|
|
||
| consoleLogSpy.restore(); | ||
| processStdoutSpy.restore(); | ||
| await testApp.close(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if i dont ever instantiate this class, i won't be able to set this to true right?
can we add a config so that when i do
NestFactory.create(...), i can set the forceConsole to true?