-
Notifications
You must be signed in to change notification settings - Fork 55
feat: Migrate Logger to TypeScript #1055
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
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,32 @@ | ||
| function Logger(config) { | ||
| import { SDKInitConfig } from "./sdkRuntimeModels"; | ||
|
|
||
| interface Logger { | ||
| verbose: (msg: string) => void; | ||
| warning: (msg: string) => void; | ||
| error: (msg: string) => void; | ||
| setLogLevel: (msg: string) => void; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, you should create a type definition for log level. For example: |
||
| logLevel: string; | ||
| logger: this; | ||
| } | ||
|
|
||
| function Logger(this: Logger, config: SDKInitConfig): void { | ||
| var self = this; | ||
| var logLevel = config.logLevel || 'warning'; | ||
| var logLevel: string = config.logLevel || 'warning'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use |
||
| if (config.hasOwnProperty('logger')) { | ||
| this.logger = config.logger; | ||
| this.logger = config.logger as Logger; | ||
| } else { | ||
| this.logger = new ConsoleLogger(); | ||
| } | ||
|
|
||
| this.verbose = function(msg) { | ||
| this.verbose = function(msg: string) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try replacing this with arrow functions? |
||
| if (logLevel !== 'none') { | ||
| if (self.logger.verbose && logLevel === 'verbose') { | ||
| self.logger.verbose(msg); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| this.warning = function(msg) { | ||
| this.warning = function(msg: string) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we trying using arrow function here too? |
||
| if (logLevel !== 'none') { | ||
| if ( | ||
| self.logger.warning && | ||
|
|
@@ -26,36 +37,37 @@ function Logger(config) { | |
| } | ||
| }; | ||
|
|
||
| this.error = function(msg) { | ||
| this.error = function(msg: string) { | ||
| if (logLevel !== 'none') { | ||
| if (self.logger.error) { | ||
| self.logger.error(msg); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| this.setLogLevel = function(newLogLevel) { | ||
| this.setLogLevel = function(newLogLevel: string) { | ||
| logLevel = newLogLevel; | ||
| this.logLevel = logLevel; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should be able to get rid of the redundant assignment and simplify this: |
||
| }; | ||
| } | ||
|
|
||
| function ConsoleLogger() { | ||
| this.verbose = function(msg) { | ||
| function ConsoleLogger(this: Logger): void { | ||
| this.verbose = function(msg: string) { | ||
| if (console && console.info) { | ||
| console.info(msg); | ||
| } | ||
| }; | ||
|
|
||
| this.error = function(msg) { | ||
| this.error = function(msg: string) { | ||
| if (console && console.error) { | ||
| console.error(msg); | ||
| } | ||
| }; | ||
| this.warning = function(msg) { | ||
| this.warning = function(msg: string) { | ||
| if (console && console.warn) { | ||
| console.warn(msg); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export default Logger; | ||
| export default Logger; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import Logger from "../../src/logger"; | ||
| import { SDKInitConfig } from "../../src/sdkRuntimeModels"; | ||
|
|
||
| describe('ConsoleLogger', () => { | ||
| it('should set logger level to warning', () => { | ||
| // create a dummy config | ||
| let sampleConfig = { | ||
| appName: 'Store Test', | ||
| appVersion: '1.x', | ||
| package: 'com.mparticle.test', | ||
| flags: {}, | ||
| } as SDKInitConfig; | ||
|
|
||
| // create a new Logger instance | ||
| const logger = new Logger(sampleConfig); | ||
|
|
||
| // setLogLevel to warning | ||
| logger.setLogLevel('warning'); | ||
|
|
||
| // insure the logLevel is set to warning | ||
| expect(logger.logLevel).toBe('warning') | ||
| }); | ||
|
|
||
| it('should set logger level to verbose', () => { | ||
| // create a dummy config | ||
| let sampleConfig = { | ||
| appName: 'Store Test', | ||
| appVersion: '1.x', | ||
| package: 'com.mparticle.test', | ||
| flags: {}, | ||
| } as SDKInitConfig; | ||
|
|
||
| // create a new Logger instance | ||
| const logger = new Logger(sampleConfig); | ||
|
|
||
| // setLogLevel to verbose | ||
| logger.setLogLevel('verbose'); | ||
|
|
||
| // insure the logLevel is set to verbose | ||
| expect(logger.logLevel).toBe('verbose') | ||
| }) | ||
|
|
||
| it('should set logger level to error', () => { | ||
| // create a dummy config | ||
| let sampleConfig = { | ||
| appName: 'Store Test', | ||
| appVersion: '1.x', | ||
| package: 'com.mparticle.test', | ||
| flags: {}, | ||
| } as SDKInitConfig; | ||
|
|
||
| // create a new Logger instance | ||
| const logger = new Logger(sampleConfig); | ||
|
|
||
| // setLogLevel to error | ||
| logger.setLogLevel('error'); | ||
|
|
||
| // insure the logLevel is set to error | ||
| expect(logger.logLevel).toBe('error') | ||
| }) | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.