Skip to content

Commit

Permalink
feat(backend): use pino logger for json formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dthib committed Dec 2, 2024
1 parent 5be034f commit 05af574
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 237 deletions.
22 changes: 22 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SentryModule } from '@sentry/nestjs/setup';
import { LoggerModule } from 'nestjs-pino';
import { AuthModule } from './auth/auth.module';
import { CollectivitesModule } from './collectivites/collectivites.module';
import { CommonModule } from './common/common.module';
Expand All @@ -17,6 +18,27 @@ import { TrpcRouter } from './trpc/trpc.router';

@Module({
imports: [
LoggerModule.forRoot({
pinoHttp: {
formatters: {
level: (label, number) => {
return {
level: label, // On renvoie le label du niveau plutôt que sa valeur numérique
};
},
},
transport:
process.env.NODE_ENV === 'production'
? undefined // Default configuration to console in json
: {
target: 'pino-pretty',
options: {
singleLine: true,
colorize: true,
},
},
},
}),
SentryModule.forRoot(),
ConfigModule.forRoot({
ignoreEnvFile: process.env.NODE_ENV === 'production', // In production, environment variables are set by the deployment
Expand Down
8 changes: 5 additions & 3 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
import './common/services/sentry.service';
// Other imports
import { patchNestjsSwagger, ZodValidationPipe } from '@anatine/zod-nestjs';
import { Logger } from '@nestjs/common';
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { Logger } from 'nestjs-pino';
import { AppModule } from './app.module';
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
import { initApplicationCredentials } from './common/services/gcloud.helper';
import { TrpcRouter } from './trpc/trpc.router';

const logger = new Logger('main');
const port = process.env.PORT || 8080;
logger.log(`Launching NestJS app on port ${port}`);

async function bootstrap() {
initApplicationCredentials();

const app = await NestFactory.create(AppModule, {
logger: ['fatal', 'error', 'warn', 'log'], // No debug by default
});
const logger = app.get(Logger);
logger.log(`Launching NestJS app on port ${port}`);
app.useLogger(logger);

const { httpAdapter } = app.get(HttpAdapterHost);

app.enableCors();
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@
"leaflet-defaulticon-compatibility": "^0.1.2",
"lodash": "^4.17.21",
"luxon": "^3.5.0",
"nestjs-pino": "^4.1.0",
"next": "^14.2.16",
"nodemailer": "^6.9.14",
"pino-http": "^10.3.0",
"pino-pretty": "^13.0.0",
"postgres": "^3.4.4",
"posthog-js": "^1.131.3",
"react": "18.3.1",
Expand Down
Loading

0 comments on commit 05af574

Please sign in to comment.