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

Custom logger for nestjs-hasura #351

Closed
foxted opened this issue Dec 23, 2021 · 10 comments
Closed

Custom logger for nestjs-hasura #351

foxted opened this issue Dec 23, 2021 · 10 comments

Comments

@foxted
Copy link
Contributor

foxted commented Dec 23, 2021

Hi there,

Is there a way to override the default logger in nestjs-hasura? Would like to extend it to use our custom logger but I don't really know how.

We are sending json logs to Google Cloud Logging, would love to use the same format, but the custom logger isn't picked up by the module. Ex.:

{"context":"NestFactory","level":"info","message":"Starting Nest application...","timestamp":"2021-12-23T05:53:40.767Z"}
{"context":"InstanceLoader","level":"info","message":"ConfigHostModule dependencies initialized","timestamp":"2021-12-23T05:53:40.788Z"}
{"context":"InstanceLoader","level":"info","message":"DiscoveryModule dependencies initialized","timestamp":"2021-12-23T05:53:40.788Z"}
{"context":"InstanceLoader","level":"info","message":"AppModule dependencies initialized","timestamp":"2021-12-23T05:53:40.788Z"}
{"context":"InstanceLoader","level":"info","message":"TerminusModule dependencies initialized","timestamp":"2021-12-23T05:53:40.788Z"}
{"context":"InstanceLoader","level":"info","message":"ConfigModule dependencies initialized","timestamp":"2021-12-23T05:53:40.932Z"}
{"context":"InstanceLoader","level":"info","message":"ConfigModule dependencies initialized","timestamp":"2021-12-23T05:53:40.932Z"}
{"context":"InstanceLoader","level":"info","message":"FirebaseModule dependencies initialized","timestamp":"2021-12-23T05:53:40.933Z"}
{"context":"InstanceLoader","level":"info","message":"HasuraModule dependencies initialized","timestamp":"2021-12-23T05:53:40.933Z"}
{"context":"InstanceLoader","level":"info","message":"HealthchecksModule dependencies initialized","timestamp":"2021-12-23T05:53:40.933Z"}
{"context":"RoutesResolver","level":"info","message":"HealthchecksController {/healthcheck}:","timestamp":"2021-12-23T05:53:41.393Z"}
{"context":"RouterExplorer","level":"info","message":"Mapped {/healthcheck, GET} route","timestamp":"2021-12-23T05:53:41.395Z"}
{"context":"RouterExplorer","level":"info","message":"Mapped {/healthcheck/auth, GET} route","timestamp":"2021-12-23T05:53:41.396Z"}
{"context":"RoutesResolver","level":"info","message":"EventHandlerController {/hasura}:","timestamp":"2021-12-23T05:53:41.396Z"}
{"context":"RouterExplorer","level":"info","message":"Mapped {/hasura/events, POST} route","timestamp":"2021-12-23T05:53:41.396Z"}
[Nest] 65647   - 2021-12-22, 9:53:41 p.m.   [HasuraModule] Initializing Hasura Module
[Nest] 65647   - 2021-12-22, 9:53:41 p.m.   [HasuraModule] Discovered 1 hasura event handlers
[Nest] 65647   - 2021-12-22, 9:53:41 p.m.   [HasuraModule] Registering hasura event handlers from HealthchecksService
{"context":"NestApplication","level":"info","message":"Nest application successfully started","timestamp":"2021-12-23T05:53:41.418Z"}
{"context":"NestApplication","level":"info","message":"Application ready on port 4001","timestamp":"2021-12-23T05:53:41.420Z"}

We can see in the example that the logs are shown using Nest.js default logger instead.

Thanks!

@underfisk
Copy link
Contributor

I think we can create a factory for the logger so that you can use your own logger like the official Nestjs core package does
Would you like to create a PR for that? I can do that if you prefer

@underfisk
Copy link
Contributor

underfisk commented Jan 11, 2022

After investigating this request I have found no need on having a custom logger as a config property, there's a better way on having a consistent logger across the nestjs app which is overriding nestjs default logger.
If you use useLogger it will override the logger factory which means when nestjs hasura module instantiates its logger instance it's going to use your custom logger, therefore, all logs will be subjective to change if you wish to format them in another way

https://docs.nestjs.com/v7/techniques/logger#extend-built-in-logger

If this doesn't help then I can re-evaluate this request

Here's an example of the custom logger that I've made just so you can see the logs are passed into my custom stream

Screenshot 2022-01-11 at 9 20 05 PM

@foxted
Copy link
Contributor Author

foxted commented Jan 13, 2022

My code does use useLogger, but for some reason it doesn't seem to work:

async function bootstrap() {
    const logger = WinstonModule.createLogger(loggerOptions);

    const app = await NestFactory.create(AppModule, {
        logger,
    });

    app.useLogger(logger);

    const options = new DocumentBuilder()
        .setTitle('Webhooks API')
        .setDescription('Webhooks API')
        .setVersion(process.env.npm_package_version)
        .build();

    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('api', app, document);

    app.use(helmet());
    app.enableCors();

    const port = process.env.PORT || 4004;

    await app.listen(port);

    logger.log(`Application ready on port ${port}`, 'NestApplication');
}

Here's what I get:

[2022-01-13T01:28:32.245Z][NestFactory][info] Starting Nest application...
[2022-01-13T01:28:32.267Z][InstanceLoader][info] ConfigHostModule dependencies initialized
[2022-01-13T01:28:32.267Z][InstanceLoader][info] DiscoveryModule dependencies initialized
[2022-01-13T01:28:32.267Z][InstanceLoader][info] TerminusModule dependencies initialized
[2022-01-13T01:28:32.426Z][InstanceLoader][info] StripeModule dependencies initialized
[2022-01-13T01:28:32.426Z][InstanceLoader][info] ConfigModule dependencies initialized
[2022-01-13T01:28:32.426Z][InstanceLoader][info] ConfigModule dependencies initialized
[2022-01-13T01:28:32.427Z][InstanceLoader][info] FirebaseModule dependencies initialized
[2022-01-13T01:28:32.427Z][InstanceLoader][info] AppModule dependencies initialized
[2022-01-13T01:28:32.427Z][InstanceLoader][info] HasuraModule dependencies initialized
[2022-01-13T01:28:32.428Z][InstanceLoader][info] HealthchecksModule dependencies initialized
[2022-01-13T01:28:32.950Z][RoutesResolver][info] HealthchecksController {/healthcheck}:
[2022-01-13T01:28:32.952Z][RouterExplorer][info] Mapped {/healthcheck, GET} route
[2022-01-13T01:28:32.952Z][RouterExplorer][info] Mapped {/healthcheck/auth, GET} route
[2022-01-13T01:28:32.952Z][RoutesResolver][info] EventHandlerController {/hasura}:
[2022-01-13T01:28:32.953Z][RouterExplorer][info] Mapped {/hasura/events, POST} route
[Nest] 5668   - 2022-01-12, 5:28:32 p.m.   [HasuraModule] Initializing Hasura Module
[Nest] 5668   - 2022-01-12, 5:28:32 p.m.   [HasuraModule] Discovered 3 hasura event handlers
[Nest] 5668   - 2022-01-12, 5:28:32 p.m.   [HasuraModule] Registering hasura event handlers from ProvidersService
[Nest] 5668   - 2022-01-12, 5:28:32 p.m.   [HasuraModule] Registering hasura event handlers from MembershipsService
[2022-01-13T01:28:32.977Z][NestApplication][info] Nest application successfully started
[2022-01-13T01:28:32.980Z][NestApplication][info] Application ready on port 4004

@foxted
Copy link
Contributor Author

foxted commented Jan 13, 2022

For some reason, I installed @golevelup/nestjs-modules explicitly and now the logging works as expected :)

@foxted
Copy link
Contributor Author

foxted commented Jan 13, 2022

Confirmed, installing @golevelup/nestjs-modules explicitly did the trick, it works as expected:

{"context":"NestFactory","level":"info","message":"Starting Nest application...","timestamp":"2022-01-13T02:16:10.672Z"}
{"context":"InstanceLoader","level":"info","message":"ConfigHostModule dependencies initialized","timestamp":"2022-01-13T02:16:10.693Z"}
{"context":"InstanceLoader","level":"info","message":"DiscoveryModule dependencies initialized","timestamp":"2022-01-13T02:16:10.693Z"}
{"context":"InstanceLoader","level":"info","message":"WinstonModule dependencies initialized","timestamp":"2022-01-13T02:16:10.693Z"}
{"context":"InstanceLoader","level":"info","message":"TerminusModule dependencies initialized","timestamp":"2022-01-13T02:16:10.693Z"}
{"context":"InstanceLoader","level":"info","message":"StripeModule dependencies initialized","timestamp":"2022-01-13T02:16:10.773Z"}
{"context":"InstanceLoader","level":"info","message":"ConfigModule dependencies initialized","timestamp":"2022-01-13T02:16:10.773Z"}
{"context":"InstanceLoader","level":"info","message":"ConfigModule dependencies initialized","timestamp":"2022-01-13T02:16:10.774Z"}
{"context":"InstanceLoader","level":"info","message":"FirebaseModule dependencies initialized","timestamp":"2022-01-13T02:16:10.774Z"}
{"context":"InstanceLoader","level":"info","message":"AppModule dependencies initialized","timestamp":"2022-01-13T02:16:10.774Z"}
{"context":"InstanceLoader","level":"info","message":"HasuraModule dependencies initialized","timestamp":"2022-01-13T02:16:10.775Z"}
{"context":"InstanceLoader","level":"info","message":"HealthchecksModule dependencies initialized","timestamp":"2022-01-13T02:16:10.775Z"}
{"context":"RoutesResolver","level":"info","message":"HealthchecksController {/healthcheck}:","timestamp":"2022-01-13T02:16:11.238Z"}
{"context":"RouterExplorer","level":"info","message":"Mapped {/healthcheck, GET} route","timestamp":"2022-01-13T02:16:11.240Z"}
{"context":"RouterExplorer","level":"info","message":"Mapped {/healthcheck/auth, GET} route","timestamp":"2022-01-13T02:16:11.240Z"}
{"context":"RoutesResolver","level":"info","message":"EventHandlerController {/hasura}:","timestamp":"2022-01-13T02:16:11.240Z"}
{"context":"RouterExplorer","level":"info","message":"Mapped {/hasura/events, POST} route","timestamp":"2022-01-13T02:16:11.241Z"}
{"context":"HasuraModule","level":"info","message":"Initializing Hasura Module","timestamp":"2022-01-13T02:16:11.242Z"}
{"context":"HasuraModule","level":"info","message":"Discovered 3 hasura event handlers","timestamp":"2022-01-13T02:16:11.249Z"}
{"context":"HasuraModule","level":"info","message":"Registering hasura event handlers from ProvidersService","timestamp":"2022-01-13T02:16:11.249Z"}
{"context":"HasuraModule","level":"info","message":"Registering hasura event handlers from MembershipsService","timestamp":"2022-01-13T02:16:11.250Z"}
{"context":"NestApplication","level":"info","message":"Nest application successfully started","timestamp":"2022-01-13T02:16:11.251Z"}
{"context":"NestApplication","level":"info","message":"Application ready on port 4004","timestamp":"2022-01-13T02:16:11.253Z"}

I'm gonna close this for now 👍🏻

@foxted foxted closed this as completed Jan 13, 2022
@foxted
Copy link
Contributor Author

foxted commented Feb 3, 2022

The issue is back after updating to the latest versions:

├── @golevelup/nestjs-hasura@2.0.1
├── @golevelup/nestjs-modules@0.5.0

@foxted foxted reopened this Feb 3, 2022
@underfisk
Copy link
Contributor

@foxted I have merged a PR that reverts the change from using Logger -> ConsoleLogger and we just need to do a new version so that its reflected in every package
@WonderPanda When you can produce a new minor, that would be great

@foxted
Copy link
Contributor Author

foxted commented Feb 3, 2022

Thanks @underfisk, appreciate it 🙂

@WonderPanda
Copy link
Collaborator

@foxted Can you try out the new release @golevelup/nestjs-hasura@2.0.2 and let us know if your issue is fixed?

@foxted
Copy link
Contributor Author

foxted commented Feb 5, 2022

Just updated to the new release and it works again :) Thank you @WonderPanda 🚀

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