Skip to content

Commit

Permalink
feat: add logging service with winston
Browse files Browse the repository at this point in the history
  • Loading branch information
noxiousghost committed Dec 26, 2024
1 parent d54fc1f commit 2bc3c30
Show file tree
Hide file tree
Showing 4 changed files with 824 additions and 643 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"nest-winston": "^1.9.7",
"pg": "^8.13.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"
"typeorm": "^0.3.20",
"winston": "^3.17.0"
},
"devDependencies": {
"@commitlint/cli": "^19.6.0",
Expand Down
16 changes: 16 additions & 0 deletions src/config/logger.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { format, transports } from 'winston';
const isProduction = process.env.APP_ENV === 'production';
export const loggerConfig = {
format: format.combine(
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
format.printf(({ level, message, timestamp }) => {
return `${timestamp} [${level}]: ${message}`;
}),
),
transports: isProduction
? [
new transports.File({ filename: 'logs/errors.log', level: 'error' }),
new transports.File({ filename: 'logs/all.log' }),
]
: [new transports.Console({ format: format.combine(format.colorize({ all: true })) })],
};
16 changes: 11 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { WinstonModule } from 'nest-winston';
import { loggerConfig } from '@/config/logger.config';
import { Logger } from '@nestjs/common';

async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);

await app.listen(process.env.APP_PORT ?? 3000, () => {
console.info('Listening to server....');
console.info(`Server listening at port http://localhost:${process.env.APP_PORT}`);
const logger = new Logger();
const port = process.env.APP_PORT ?? 3000;
const app = await NestFactory.create(AppModule, {
// this will overwrite the default logger of nestJS with custom winston logger
logger: WinstonModule.createLogger(loggerConfig),
});
await app.listen(port, () => {
logger.log(`App is listening on port ${port}`);
});
}

Expand Down
Loading

0 comments on commit 2bc3c30

Please sign in to comment.