Skip to content

Commit

Permalink
build: implement minimal pino logger
Browse files Browse the repository at this point in the history
  • Loading branch information
0-vortex committed Oct 13, 2022
1 parent 1818871 commit b991dad
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 2 deletions.
206 changes: 206 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -63,10 +63,12 @@
"@supabase/supabase-js": "^1.35.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"nestjs-pino": "^3.1.1",
"nestjs-supabase-auth": "^1.0.9",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
"pg": "^8.8.0",
"pino-http": "^8.2.1",
"rxjs": "^7.5.6",
"semver": "^7.3.7",
"typeorm": "^0.3.9",
Expand Down Expand Up @@ -101,6 +103,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.1",
"jest": "29.0.2",
"pino-pretty": "^9.1.1",
"rimraf": "^3.0.2",
"supertest": "^6.2.4",
"ts-node": "^10.9.1",
Expand Down
23 changes: 22 additions & 1 deletion src/app.module.ts
@@ -1,9 +1,10 @@
import { Module } from "@nestjs/common";
import {Module, RequestMethod} from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { HttpModule } from "@nestjs/axios";
import { TerminusModule } from "@nestjs/terminus";
import { TypeOrmModuleOptions } from "@nestjs/typeorm/dist/interfaces/typeorm-options.interface";
import { LoggerModule } from "nestjs-pino";
import { DataSource } from "typeorm";

import { RepoModule } from "./repo/repo.module";
Expand Down Expand Up @@ -59,6 +60,26 @@ import { UserModule } from "./user/user.module";
}) as TypeOrmModuleOptions,
inject: [ConfigService],
}),
LoggerModule.forRoot({
pinoHttp: {
// name: "api.os",
level: process.env.NODE_ENV !== "production" ? "debug" : "info",
transport: process.env.NODE_ENV !== "production"
? {
target: "pino-pretty",
options: {
colorize: true,
levelFirst: true,
translateTime: "UTC:hh:MM:ss.l",
// singleLine: true,
messageFormat: "\x1B[33m[{context}] \x1B[32m{msg}",
ignore: "pid,hostname,context",
},
}
: undefined,
},
exclude: [{ method: RequestMethod.ALL, path: "check" }],
}),
TerminusModule,
HttpModule,
AuthModule,
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Expand Up @@ -7,6 +7,7 @@ import { SwaggerModule, DocumentBuilder, SwaggerCustomOptions } from "@nestjs/sw
import { ValidationPipe, VersioningType } from "@nestjs/common";
import fastifyHelmet from "@fastify/helmet";
import { ConfigService } from "@nestjs/config";
import { Logger, LoggerErrorInterceptor } from "nestjs-pino";
import fastifyRateLimit from "@fastify/rate-limit";
import path from "node:path";
import { writeFile } from "node:fs/promises";
Expand All @@ -19,10 +20,10 @@ async function bootstrap () {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ logger: false }),
{ bufferLogs: true },
);
const configService = app.get(ConfigService);
const apiDomain = String(configService.get("api.domain"));

const markdownDescription = `
## Swagger-UI API Documentation
Expand Down Expand Up @@ -66,6 +67,8 @@ code | condition
## Additional links`;

app.useLogger(app.get(Logger));
app.useGlobalInterceptors((new LoggerErrorInterceptor));
app.enableCors();
app.enableVersioning({
type: VersioningType.URI,
Expand Down

0 comments on commit b991dad

Please sign in to comment.