Suppress Startup log in NestJS
Example
1
2
3
4
5
6
7
8
9
10
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { bootstrapLogger } from './bootstrap-logger';
...
const app = await NestFactory.create(AppModule, {
logger: bootstrapLogger(),
});
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// bootstrap-logger.ts
import { Format } from 'logform';
import { WinstonModule } from 'nest-winston';
import { format, transports } from 'winston';
import { customConsoleFormat } from './customConsole';
export const bootstrapLogger = () => {
const customFormat: Format = customConsoleFormat();
const suppressStartupLog = format(info => {
if (
info.level === 'info' &&
(info.context === 'InstanceLoader' ||
info.context === 'RoutesResolver' ||
info.context === 'RouterExplorer')
) {
return false;
}
return info;
});
const transport = new transports.Console({
level: 'DEBUG',
format: format.combine(suppressStartupLog(), customFormat),
});
return WinstonModule.createLogger({ transports: [transport] });
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// custom-console.ts
import { format } from 'winston';
export const customConsoleFormat = (moduleName?: string) =>
format.printf(({ context, level, timestamp, message, ..._meta }) => {
return (
('undefined' !== typeof moduleName ? `[${moduleName}] ` : '') +
`${level.charAt(0).toUpperCase() + level.slice(1)} ` +
('undefined' !== typeof timestamp ? `${new Date(timestamp).toISOString()} ` : '') +
('undefined' !== typeof context ? `(${context}) ` : '') +
message
);
});
This post is licensed under CC BY 4.0 by the author.