ferrerojosh / nest-keycloak-connect

keycloak-nodejs-connect module for Nest
MIT License
311 stars 123 forks source link

logs not available with custom logger #113

Open deeeed opened 2 years ago

deeeed commented 2 years ago

I am tryng to figure out why after replacing the default nest logger with pino logger, my nest-keycloak-connect logs aren't displayed to the console. Here is how I instanciate my module:

@Module({
  imports: [
    LoggerModule.forRoot({
      forRoutes: [{ path: '*', method: RequestMethod.ALL }],
      pinoHttp: {
        level: environment.production ? 'info' : 'debug',
        autoLogging: true,
        // install 'pino-pretty' package in order to use the following option
        transport: environment.production
          ? undefined
          : {
              target: 'pino-pretty',
              options: {
                // genReqId: (req) => {
                //   return uuidv4.uuid();
                // },
                colorize: true,
                levelFirst: true,
                singleLine: true,
                ignore: 'hostname',
                translateTime: 'UTC:yyyy/mm/dd HH:MM:ss TT Z',
                sync: true,
              },
            },
      },
    }),
//...
KeycloakConnectModule.registerAsync({
      useFactory: (configService: ConfigService) => {
        return {
          authServerUrl: configService.get('AUTH_SERVER_URL'),
          useNestLogger: true,
          logLevels: ['log', 'warn', 'debug', 'verbose', 'error'],
          realm: configService.get('AUTH_REALM'),
          clientId: configService.get('AUTH_CLIENT_ID'),
          secret: configService.get('AUTH_SECRET'),
          cookieKey: configService.get('AUTH_COOKIE_KEY'),
          policyEnforcement: PolicyEnforcementMode.PERMISSIVE, // optional
          tokenValidation: TokenValidation.ONLINE, // optional
        };
      },
      inject: [ConfigService],
      imports: [ConfigModule],
    }),

Before the custom pino logger takes over I can see the logs in the console during startup:

Debugger attached.
[Nest] 96345  - 02/14/2022, 10:42:07 AM     LOG [NestFactory] Starting Nest application...
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] FacadeModule dependencies initialized +582ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] CoreModule dependencies initialized +3ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] ClsModule dependencies initialized +1ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] LoggerModule dependencies initialized +1ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM    WARN [KeycloakConnectModule] Option 'logLevels' will be deprecated in the future. It is recommended to override or extend NestJS logger instead.
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] KeycloakConnectModule dependencies initialized +106ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] KeycloakAdminModule dependencies initialized +2ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +152ms
[Nest] 96345  - 02/14/2022, 10:42:08 AM     LOG [InstanceLoader] AppModule dependencies initialized +23ms
.... PINO LOGGER STARTING and log format changes  ....
DEBUG [2022/02/14 02:42:08 AM UTC] (96345): Mounting ClsMiddleware to * {"context":"ClsModule"}
INFO [2022/02/14 02:42:08 AM UTC] (96345): AppController {/}: {"context":"RoutesResolver"}
INFO [2022/02/14 02:42:08 AM UTC] (96345): 🚀 Application is running on: http://localhost:5556

If I leave useNestLogger: true, I won't be able to see the logs from nest-keycloak-connnect. Looking at the code, it just uses Logger from nestjs/common which is what I use in the my app without issue. Any idea why it wouldn't work with the package?

ferrerojosh commented 2 years ago

I think for Nest logger, you need to override the custom implementation as seen here, the logger is already created at runtime during init. I am not really sure how pino-logger somehow overrides the nest logger at runtime.