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

Making logging optional #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

doctorrokter
Copy link

  • get rid of ConsoleLogger inheritance:
    • use SentryService implements LoggerService instead of SentryService extends ConsoleLogger;
  • add an ability to provide custom logger;
  • add an ability to disable logging entirely;
  • default logger is ConsoleLogger for backward compatibility;

   - use `SentryService implements LoggerService` instead of `SentryService extends ConsoleLogger`;
- add an ability to provide custom logger;
- add an ability to disable logging entirely;
- default logger is `ConsoleLogger` for backward compatibility;
Copy link
Contributor

@holm holm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I added a few comments just based on my perspective.

@@ -37,7 +38,7 @@ export class SentryCoreModule {
const provider: Provider = {
inject: [SENTRY_MODULE_OPTIONS],
provide: SENTRY_TOKEN,
useFactory: (options: SentryModuleOptions) => new SentryService(options),
useFactory: (options: SentryModuleOptions) => createSentryService(options),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused on the injection of the options. It both seems to be injected here via the provider config, as well as in the constructor of the class via the inject-decorator. I would think just the inject-decorator would be infer, and this can be changed to just use useClass instead of useFactory and remove the inject from here.

import { SentryService } from './sentry.service';
import { ConsoleLogger } from '@nestjs/common';

export function createSentryService(options: SentryModuleOptions): SentryService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to the constructor of SentryService, so the defaulting is done there. That makes it easier in general to use this service I think, without having to rely on this factory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the users of that lib should not have an access to that low level implementation. All what other devs have to do - use @InjectSentry() decorator in their projects without caring about what happenes under the hood.

But yeah, maybe for internal usage it makes sense to re-think the instantiation of SentryService.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, you wanted to move default logger instantiation to SentryService's constructor! Yep, makes sense!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Sentry.captureMessage(message, Sentry.Severity.Debug);
} catch (err) {}
}

verbose(message: string, context?: string) {
message = `${this.app} ${message}`;
try {
super.verbose(message, context);
if (this.opts && this.opts.logger && this.opts.logger.verbose) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any change verbose is not defined on the logger? Isn't that part of the interface?

(same for debug above)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see the NestJS's LoggerService interface declares debug and verbose methods as optional. ConsoleLogger implements all methods, but some custom loggers may omit these optional methods. This is just to be safe. Also, TS compiler suggested to check the availability of these methods.

export interface LoggerService {
    /**
     * Write a 'log' level log.
     */
    log(message: any, ...optionalParams: any[]): any;
    /**
     * Write an 'error' level log.
     */
    error(message: any, ...optionalParams: any[]): any;
    /**
     * Write a 'warn' level log.
     */
    warn(message: any, ...optionalParams: any[]): any;
    /**
     * Write a 'debug' level log.
     */
    debug?(message: any, ...optionalParams: any[]): any;
    /**
     * Write a 'verbose' level log.
     */
    verbose?(message: any, ...optionalParams: any[]): any;
    /**
     * Set log levels.
     * @param levels log levels
     */
    setLogLevels?(levels: LogLevel[]): any;
}```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that, makes sense!

- instantiate default `ConsoleLogger` in `SentryService` constructor
Copy link
Contributor

@holm holm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. This looks good from my perspective.

Thanks again for doing it.

@ntegral
Copy link
Owner

ntegral commented Oct 25, 2021

This currently has conflicts with the master branch...

@doctorrokter
Copy link
Author

@ntegral merged!

@nkovacic
Copy link

@holm @doctorrokter any chance of merging this? Would enable custom logger usage in combination with nestjs-sentry.

@ntegral ntegral self-assigned this Sep 16, 2022
Copy link
Owner

@ntegral ntegral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the changes to resolve conflicts. Thanks

@CalMlynarczyk
Copy link

@doctorrokter I wanted to ping this one more time to see if it's feasible to get the merge conflicts resolved and merge this PR.

I understand the OP was pretty responsive to feedback and yet the PR still didn't get merged, which can be frustrating. I'm hoping the repo maintainers can jump on this if the merge conflicts get resolved again. If the merge conflicts don't get addressed soon, I might re-fork these changes and resubmit a matching PR, because this change would really help my team deal with messy logs.

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

Successfully merging this pull request may close these issues.

None yet

5 participants