I have configured winston logger to log on both console and file with below given code.
On file, everything is getting properly logged except when the errors are being thrown at NestJS bootstrapping time.
Below is the winston configuration code
export const winstonLogger = WinstonModule.createLogger({
level: 'silly',
transports: [
new winston.transports.Console({ format: winston.format.simple() }),
new winston.transports.File({
filename: 'logs/log.txt',
format: winston.format.simple(),
level: 'info',
}),
],
exitOnError: false,
});
And this the code of main.ts file where I deliberately thrown an error.
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: winstonLogger,
});
const configService = app.get(ConfigService);
throw new Error('My error');
await app.listen(configService.get<number>('APP_PORT')!);
}
bootstrap();
Now, when I am starting the app, following are the logs in console
info: Starting Nest application... {"context":"NestFactory"}
info: AppModule dependencies initialized {"context":"InstanceLoader"}
info: MongooseModule dependencies initialized {"context":"InstanceLoader"}
info: ConfigHostModule dependencies initialized {"context":"InstanceLoader"}
info: ConfigModule dependencies initialized {"context":"InstanceLoader"}
info: ConfigModule dependencies initialized {"context":"InstanceLoader"}
info: MongooseCoreModule dependencies initialized {"context":"InstanceLoader"}
info: MongooseModule dependencies initialized {"context":"InstanceLoader"}
info: MongooseModule dependencies initialized {"context":"InstanceLoader"}
info: LangchainModule dependencies initialized {"context":"InstanceLoader"}
info: ChatModule dependencies initialized {"context":"InstanceLoader"}
/Users/uddeshyavishwakarma/Desktop/sr-chatbot/backend/src/main.ts:18
throw new Error('My error');
^
Error: My error
at bootstrap (/Users/uddeshyavishwakarma/Desktop/sr-chatbot/backend/src/main.ts:18:9)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
But, below are the logs written in file
info: Starting Nest application... {"context":"NestFactory"}
info: AppModule dependencies initialized {"context":"InstanceLoader"}
info: MongooseModule dependencies initialized {"context":"InstanceLoader"}
info: ConfigHostModule dependencies initialized {"context":"InstanceLoader"}
info: ConfigModule dependencies initialized {"context":"InstanceLoader"}
info: ConfigModule dependencies initialized {"context":"InstanceLoader"}
info: MongooseCoreModule dependencies initialized {"context":"InstanceLoader"}
Not only the error log is missing but also last 4 info logs are missing in the file
FYI: packages used:
"@nestjs/common": "^10.0.0"
"@nestjs/core": "^10.0.0"
"nest-winston": "^1.9.4"
"winston": "^3.13.0"
techwizards01 techwizards01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.