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

Fabiangosebrink/adding abstract services #1422

Merged
merged 9 commits into from
Apr 11, 2022

Conversation

FabianGosebrink
Copy link
Collaborator

@FabianGosebrink
Copy link
Collaborator Author

@timdeschryver if you have the time to review, feel free :)

@github-actions
Copy link

github-actions bot commented Apr 9, 2022

Azure Static Web Apps: Your stage site is ready! Visit it here: https://nice-hill-002425310-1422.centralus.azurestaticapps.net

@timdeschryver
Copy link
Contributor

@FabianGosebrink I'll make some time for this next week.

Copy link
Contributor

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

I think that a couple of changes already were pushed to main (ad4b090).

This PR looks good to me and I also like the changes to the storage, this makes things consistent and flexible.
But I had something else in mind for the logger.

import { Injectable } from '@angular/core';
import { OpenIdConfiguration } from '../config/openid-configuration';
import { AbstractLoggerService } from './abstract-logger.service';
import { LogLevel } from './log-level';

@Injectable()
export class LoggerService  {

+  constructor(private log: AbstractLoggerService) {}

  logError(configuration: OpenIdConfiguration, message: any, ...args: any[]): void {
    if (this.loggingIsTurnedOff(configuration)) {
      return;
    }

    const { configId } = configuration;

    if (!!args && !!args.length) {
-      console.error(`[ERROR] ${configId} - ${message}`, ...args);
+      this.log.logError(`[ERROR] ${configId} - ${message}`, ...args);
    } else {
-      console.error(`[ERROR] ${configId} - ${message}`);
+      this.log.logError(`[ERROR] ${configId} - ${message}`);
    }
  }

  logWarning(configuration: OpenIdConfiguration, message: any, ...args: any[]): void {
    if (!this.logLevelIsSet(configuration)) {
      return;
    }

    if (this.loggingIsTurnedOff(configuration)) {
      return;
    }

    if (!this.currentLogLevelIsEqualOrSmallerThan(configuration, LogLevel.Warn)) {
      return;
    }

    const { configId } = configuration;

    if (!!args && !!args.length) {
-      console.warn(`[WARN] ${configId} - ${message}`, ...args);
+      this.log.logWarning(`[WARN] ${configId} - ${message}`, ...args);
    } else {
-      console.warn(`[WARN] ${configId} - ${message}`);
+      this.log.logWarning(`[WARN] ${configId} - ${message}`);
    }
  }

  logDebug(configuration: OpenIdConfiguration, message: any, ...args: any[]): void {
    if (!this.logLevelIsSet(configuration)) {
      return;
    }

    if (this.loggingIsTurnedOff(configuration)) {
      return;
    }

    if (!this.currentLogLevelIsEqualOrSmallerThan(configuration, LogLevel.Debug)) {
      return;
    }

    const { configId } = configuration;

    if (!!args && !!args.length) {
-      console.log(`[DEBUG] ${configId} - ${message}`, ...args);
+      this.log.logDebug(`[DEBUG] ${configId} - ${message}`, ...args);
    } else {
-      console.log(`[DEBUG] ${configId} - ${message}`);
+      this.log.logDebug(`[DEBUG] ${configId} - ${message}`);
    }
  }

  private currentLogLevelIsEqualOrSmallerThan(configuration: OpenIdConfiguration, logLevelToCompare: LogLevel): boolean {
    const { logLevel } = configuration || {};

    return logLevel <= logLevelToCompare;
  }

  private logLevelIsSet(configuration: OpenIdConfiguration): boolean {
    const { logLevel } = configuration || {};

    if (logLevel === null) {
      return false;
    }

    if (logLevel === undefined) {
      return false;
    }

    return true;
  }

  private loggingIsTurnedOff(configuration: OpenIdConfiguration): boolean {
    const { logLevel } = configuration || {};

    return logLevel === LogLevel.None;
  }
}

The reasoning behind this, is that the logger service already contains some useful checks whether that it should log or not, and these are fine. From a consumers view, I just want to use another logger implementation than console.log.

With the current implementation we would have to rewrite that logic, which is fine if that's intended.
Thoughts?

@FabianGosebrink
Copy link
Collaborator Author

FabianGosebrink commented Apr 11, 2022

I like that idea. That of course makes sense. With this, the user can use the checks which are already there. Will implement this and push. But with this we would need another logger service, like an ConsoleLoggerService only having console.logs in it which implements the AbstractLoggerService. Or am I mixing something here? @timdeschryver . IDK why some things are already on main tbh...

@github-actions
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://nice-hill-002425310-1422.centralus.azurestaticapps.net

@github-actions
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://nice-hill-002425310-1422.centralus.azurestaticapps.net

Copy link
Contributor

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

@FabianGosebrink Exactly, this is perfect! 🤩
Thanks for implementing this so quickly.

@FabianGosebrink
Copy link
Collaborator Author

Thank you for providing such a valuable and good feedback so quickly! Thanks :)

@FabianGosebrink FabianGosebrink merged commit 89913ff into main Apr 11, 2022
@damienbod damienbod deleted the fabiangosebrink/Adding-abstract-services branch April 17, 2022 18:50
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

2 participants