nestjspinojspino

Why Pino logger.debug doesn't work in NestJS


When I use pino logger.debug() it doesn't log anything. My configuration and usage below. Package nestjs-pino

app.module.ts

import { Module } from '@nestjs/common';
import { LoggerModule } from 'nestjs-pino';
import pino from 'pino';
import { ConfigModule } from '@nestjs/config';
import configuration from './config/configuration';

const pinoPretty: pino.TransportSingleOptions = {
  target: 'pino-pretty',
  options: {
    singleLine: true,
    translateTime: 'yyyy-dd-mm, h:MM:ss TT'
  }
};

const file: pino.TransportSingleOptions = {
  target: 'pino/file',
  options: { destination: `./logs/combined.log` }
};

const targets = [pinoPretty, file];

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        autoLogging: false,
        level: 'debug',
        transport: { targets }
      }
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration],
      cache: true
    })
  ]
})
export class AppModule {}
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as cookieParser from 'cookie-parser';
import { Logger, LoggerErrorInterceptor } from 'nestjs-pino';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useGlobalPipes(new ValidationPipe({ forbidUnknownValues: true, whitelist: true }));
  app.use(cookieParser());
  app.useLogger(app.get(Logger));

  const config = app.get<ConfigService>(ConfigService);

  await app.listen(config.get<number>('port') || 4000);
}
bootstrap();

How I use it

import { Logger, Provider } from '@nestjs/common';

function () {
  const logger = new Logger('Redis');
  logger.debug('Redis client warming up');
}

After investigation, I have found that debug log is actually presented here in the data on the image below. But when the Atomic.store is executed and logs appear in console - debug is not there

enter image description here


Solution

  • I was facing the same issue you need to specify a level per target.

    Example of my configuration:

    enter image description here

    Which would result in the following for you:

    const pinoPretty: pino.TransportSingleOptions = {
        level: 'debug',
        target: 'pino-pretty',
        options: {
            singleLine: true,
            translateTime: 'yyyy-dd-mm, h:MM:ss TT'
        }
    };
    
    const file: pino.TransportSingleOptions = {
        level: 'debug',
        target: 'pino/file',
        options: { destination: `./logs/combined.log` }
    };