Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getTimestamp exception thrown when creating Application logger #279

Closed
MatthewPeterGreen opened this issue Jan 12, 2021 · 14 comments
Closed

Comments

@MatthewPeterGreen
Copy link

Creating an application level Logger (via the bootstrap) now appears to cause the following exception:

[ExceptionHandler] instance.getTimestamp is not a function - {"stack":["TypeError: instance.getTimestamp is not a function\n

I created a vanilla application with the following main.ts file:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { utilities as nestWinstonModuleUtilities, WINSTON_MODULE_NEST_PROVIDER, WinstonModule } from 'nest-winston';
import * as winston from 'winston';

const winstonTransports: winston.transport[] = [
  new winston.transports.Console({
    format: winston.format.combine(winston.format.timestamp(), nestWinstonModuleUtilities.format.nestLike()),
  }),
];

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: WinstonModule.createLogger({
      transports: winstonTransports,
    })})
  await app.listen(3000);
}
bootstrap();

Then adding a simple log to the app.controller.ts file

import { Controller, Get, Logger } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {
    Logger.log('Try it then')
  }

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

This should cause the issue as described. If you need any more information just let me know.

Thanks

@gremo
Copy link
Owner

gremo commented Jan 19, 2021

Hi, are you able to setup a minimal repository example?

@MatthewPeterGreen
Copy link
Author

Hi, no problem. I created a barebones project to illustrate the problem. You just need to run it to see the error.
https://github.com/MatthewPeterGreen/nest-winston-issue

@Nightbr
Copy link

Nightbr commented Jan 26, 2021

Like in this issue #275, I get the logger in main.ts with const logger: Logger = app.get(WINSTON_MODULE_NEST_PROVIDER); in it has fixed the issue. Hope it helps.

  await app.listen(port, host, () => {
    const address = configService.get('apiUrl');
    const logger: Logger = app.get(WINSTON_MODULE_NEST_PROVIDER);
    logger.setContext('Main');
    logger.log('Listening at ' + address);
    logger.log('GraphQL at ' + address + '/graphql');
  });

@gremo
Copy link
Owner

gremo commented Jan 26, 2021

@Nightbr good catch!

I can't understand - however - why passing the logger option isn't working, while getting from the service container solved the issue. Any clue? I may update the documentation if we find the exact reason why isn't working.

@Nightbr
Copy link

Nightbr commented Jan 26, 2021

Seems the module initialized the instance so when you override the default logger init with app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));. you need to get the logger with the correct Token WINSTON_MODULE_NEST_PROVIDER otherwise the logger is not defined.

@gremo
Copy link
Owner

gremo commented Apr 5, 2021

I've improved the README, can you both take a look at it and see if the isssue persist?

@Nightbr
Copy link

Nightbr commented Apr 6, 2021

Seems good to me, perhaps a bit complex for new User. Maybe we could create some nestjs boilerplate example with the different configuration? In the code base in an example folder.

@gremo
Copy link
Owner

gremo commented Apr 6, 2021

Thanks for your feedback. Yes I could create a boilerplate code, but I don't know the conventions about it... can you send me some example (maybe other projects)?

@Nightbr
Copy link

Nightbr commented Apr 6, 2021

Yep sure, for example in nestjs/terminus, they have created a folder sample be can be called examples IMO. And in it you have 1 folder per example. In each folder, you have a minimal nestjs app with the configuration you want to demonstrate.

For this module, I think we could have multiple examples such as:

  • Basic Module configuration (what is explain in Quick start)
  • Async Configuration (coupled with the ConfigModule could be a nice real life example)
  • Use Logger in Bootstrap
  • Configure NestLike formatter
  • ...

See https://github.com/nestjs/terminus/tree/master/sample

Maybe create a Pull Request and I'm ok to review & complete the PR if you want.

@gremo
Copy link
Owner

gremo commented Apr 6, 2021

Thanks, it's a great idea. Should I use the nest cli right?

@Nightbr
Copy link

Nightbr commented Apr 6, 2021

As you want! But the nestcli can help you generate quickly a base app 👌

@gremo
Copy link
Owner

gremo commented Apr 6, 2021

Seems I can't. When running the sample app under the "sample" folder of this project, it doesn't work. I think is something about the TypeScript config (and types conflict). It works when I move the sample outside:

nest

@Nightbr
Copy link

Nightbr commented Apr 6, 2021

Could you create a Pull Request where we can see the code & discuss about this, it will be better to not overflow this issue & be able to see your code & changes. Thanks!

@gremo
Copy link
Owner

gremo commented Apr 6, 2021

@Nightbr you're right, look at #324

@gremo gremo closed this as completed Jul 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants