Skip to content

Add support to inject BI logger to our LogService #2434

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

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/services/LogService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
function warn(message: string) {
if (__DEV__) {
console.warn(message);
}
interface BILogger {
log: (event: any) => void;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this to be any?
We can make it generic with the following:

interface BILogger<T> {
  log: (event: T) => void;
}
class LogService<T = any> {
  biLogger: BILogger<T> | undefined;

  injectBILogger = (biLogger: BILogger<T>) => {
    this.biLogger = biLogger;
  };

  biLog = (event: T) => {
    this.biLogger?.log(event);
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How would that work? Since I export this class as a singleton there's no where to set the LogService generic type. No?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Very true, did not find a different way to do it...

}
class LogService {
biLogger: BILogger | undefined;

injectBILogger = (biLogger: BILogger) => {
this.biLogger = biLogger;
};

function error(message: string) {
if (__DEV__) {
console.error(message);
biLog = (event: any) => {
this.biLogger?.log(event);
}

warn = (message: string) => {
if (__DEV__) {
console.warn(message);
}
};

error = (message: string) => {
if (__DEV__) {
console.error(message);
}
};

deprecationWarn = ({component, oldProp, newProp}: {component: string; oldProp: string; newProp?: string}) => {
this.warn(getDeprecationMessage({component, oldProp, newProp}));
};

componentDeprecationWarn = ({oldComponent, newComponent}: {oldComponent: string; newComponent: string}) => {
this.warn(getComponentDeprecationMessage({oldComponent, newComponent}));
};

deprecationError = ({component, oldProp, newProp}: {component: string; oldProp: string; newProp?: string}) => {
this.error(getDeprecationMessage({component, oldProp, newProp}));
};

componentDeprecationError = ({oldComponent, newComponent}: {oldComponent: string; newComponent: string}) => {
this.error(getComponentDeprecationMessage({oldComponent, newComponent}));
};
}

function getDeprecationMessage({component, oldProp, newProp}: {component: string; oldProp: string; newProp?: string}) {
Expand All @@ -23,27 +54,4 @@ function getComponentDeprecationMessage({oldComponent, newComponent}: {oldCompon
return message;
}

function deprecationWarn({component, oldProp, newProp}: {component: string; oldProp: string; newProp?: string}) {
warn(getDeprecationMessage({component, oldProp, newProp}));
}

function componentDeprecationWarn({oldComponent, newComponent}: {oldComponent: string; newComponent: string}) {
warn(getComponentDeprecationMessage({oldComponent, newComponent}));
}

function deprecationError({component, oldProp, newProp}: {component: string; oldProp: string; newProp?: string}) {
error(getDeprecationMessage({component, oldProp, newProp}));
}

function componentDeprecationError({oldComponent, newComponent}: {oldComponent: string; newComponent: string}) {
error(getComponentDeprecationMessage({oldComponent, newComponent}));
}

export default {
warn,
error,
deprecationWarn,
componentDeprecationWarn,
deprecationError,
componentDeprecationError
};
export default new LogService();