I have implemented the Pino Logger (iamolegga/nestjs-pino) like described in the documentation, into my NestJS application. And I can see the startup messages like route mappings and controller initialization, as well as "incoming request" and "request completed". See code below.
But as soon as I use the logger inside a service or controller of mine (see document.service.ts), the logs do not appear in the console.
// main.ts
import { bootstrap } from "./app";
// Start development server
bootstrap()
.then(async (app) => {
await app.listen(process.env.LISTEN_PORT, "0.0.0.0");
})
.catch((err) => console.log(err));
// app.ts
export async function bootstrap() {
let fastifyAdapter = new FastifyAdapter({
logger: pinoOptions,
trustProxy: true,
} as FastifyServerOptions);
const app = await NestFactory.create<NestFastifyApplication>(AppModule, fastifyAdapter, { bufferLogs: true });
// pino logger
app.useLogger(app.get(Logger));
app.useGlobalInterceptors(new LoggerErrorInterceptor());
await app.init();
return app;
}
// app.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule, TypeOrmModuleOptions } from "@nestjs/typeorm";
import { Params } from "nestjs-pino/params";
@Module({
imports: [
LoggerModule.forRoot({
pinoHttp: pinoOptions,
} as Params),
TypeOrmModule.forRootAsync({
useFactory: async (logger: PinoLogger): Promise<TypeOrmModuleOptions> => {
return {
...(await getDataSourceConfig()),
autoLoadEntities: true,
logger: new PinoTypeormLogger(logger),
};
},
inject: [PinoLogger],
}),
AnnouncementModule,
// more modules excluded
],
})
export class AppModule {}
// app-logger.ts
import { PinoLoggerOptions } from "fastify/types/logger";
export const pinoOptions: PinoLoggerOptions = {
level: "debug",
transport: {
targets: [
{
target: "pino-pretty",
level: "debug",
options: {
name: "terminal",
colorize: false,
singleLine: true,
},
},
],
},
redact: { paths: ["req.headers.authorization", "req.headers.cookie"], censor: "**REDACTED**" },
};
// document.service.ts
@Injectable()
export class DocumentService {
private readonly logger = new Logger(DocumentService.name);
async getDocument() {
this.logger.info("This does not appear in the console!");
}
}
Does anybody have an idea what the cause of this could be?
realneo, you try to change the logger level at your config? Because your transport only specific debug, and you try to log info level.