-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from edx/robrap/ARCH-687-logging-interface
ARCH-687: feat: provide wrapping interface for logging service
- Loading branch information
Showing
7 changed files
with
192 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
coverage | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
import LoggingService from './LoggingService'; | ||
import NewRelicLoggingService from './NewRelicLoggingService'; | ||
import { | ||
configureLoggingService, | ||
logAPIErrorResponse, | ||
logInfo, | ||
logError, | ||
} from './logging'; | ||
|
||
export default LoggingService; | ||
export { | ||
configureLoggingService, | ||
logAPIErrorResponse, | ||
logInfo, | ||
logError, | ||
NewRelicLoggingService, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Provides a wrapper for any logging service implementation of the expected | ||
* logging service interface. | ||
* | ||
* This enables shared libraries or applications that want to support multiple | ||
* logging service implementations to be coded against these wrapping functions, | ||
* and allows any concrete implementation to be injected. | ||
*/ | ||
|
||
let loggingService = null; | ||
|
||
function ensureLoggingServiceAPI(newLoggingService, functionName) { | ||
if (typeof newLoggingService[functionName] !== 'function') { | ||
throw Error(`The loggingService API must have a ${functionName} function.`); | ||
} | ||
} | ||
|
||
function configureLoggingService(newLoggingService) { | ||
if (!newLoggingService) { | ||
throw Error('The loggingService is required.'); | ||
} | ||
ensureLoggingServiceAPI(newLoggingService, 'logAPIErrorResponse'); | ||
ensureLoggingServiceAPI(newLoggingService, 'logInfo'); | ||
ensureLoggingServiceAPI(newLoggingService, 'logError'); | ||
loggingService = newLoggingService; | ||
} | ||
|
||
function resetLoggingService() { | ||
loggingService = null; | ||
} | ||
|
||
function getLoggingService() { | ||
if (!loggingService) { | ||
throw Error('You must first configure the loggingService.'); | ||
} | ||
return loggingService; | ||
} | ||
|
||
function logInfo(message) { | ||
return getLoggingService().logInfo(message); | ||
} | ||
|
||
function logError(error, customAttributes) { | ||
return getLoggingService().logError(error, customAttributes); | ||
} | ||
|
||
function logAPIErrorResponse(error, customAttributes) { | ||
return getLoggingService().logAPIErrorResponse(error, customAttributes); | ||
} | ||
|
||
export { | ||
configureLoggingService, | ||
logAPIErrorResponse, | ||
logInfo, | ||
logError, | ||
resetLoggingService, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import NewRelicLoggingService from './NewRelicLoggingService'; | ||
|
||
import { | ||
configureLoggingService, | ||
logAPIErrorResponse, | ||
logInfo, | ||
logError, | ||
resetLoggingService, | ||
} from './logging'; | ||
|
||
jest.mock('./NewRelicLoggingService'); | ||
|
||
const arg1 = 'argument one'; | ||
const arg2 = 'argument two'; | ||
|
||
describe('configureLoggingService', () => { | ||
it('fails when loggingService is not supplied', () => { | ||
expect(configureLoggingService) | ||
.toThrowError(new Error('The loggingService is required.')); | ||
}); | ||
|
||
it('fails when loggingService has invalid API', () => { | ||
expect(() => configureLoggingService({})) | ||
.toThrowError(new Error('The loggingService API must have a logAPIErrorResponse function.')); | ||
}); | ||
}); | ||
|
||
describe('configured logging service', () => { | ||
beforeEach(() => { | ||
// uses NewRelicLoggingService as any example of a concrete implementation | ||
configureLoggingService(NewRelicLoggingService); | ||
}); | ||
|
||
describe('logInfo', () => { | ||
it('passes call through to NewRelicLoggingService', () => { | ||
const mockStatic = jest.fn(); | ||
NewRelicLoggingService.logInfo = mockStatic.bind(NewRelicLoggingService); | ||
|
||
logInfo(arg1); | ||
expect(mockStatic).toHaveBeenCalledWith(arg1); | ||
}); | ||
}); | ||
|
||
describe('logError', () => { | ||
it('passes call through to NewRelicLoggingService', () => { | ||
const mockStatic = jest.fn(); | ||
NewRelicLoggingService.logError = mockStatic.bind(NewRelicLoggingService); | ||
|
||
logError(arg1, arg2); | ||
expect(mockStatic).toHaveBeenCalledWith(arg1, arg2); | ||
}); | ||
}); | ||
|
||
describe('logAPIErrorResponse', () => { | ||
it('passes call through to NewRelicLoggingService', () => { | ||
const mockStatic = jest.fn(); | ||
NewRelicLoggingService.logAPIErrorResponse = mockStatic.bind(NewRelicLoggingService); | ||
|
||
logAPIErrorResponse(arg1, arg2); | ||
expect(mockStatic).toHaveBeenCalledWith(arg1, arg2); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('test failures when logging service is not configured', () => { | ||
beforeAll(() => { | ||
resetLoggingService(); | ||
}); | ||
|
||
describe('logInfo', () => { | ||
it('throws an error', () => { | ||
expect(() => logInfo(arg1)) | ||
.toThrowError(new Error('You must first configure the loggingService.')); | ||
}); | ||
}); | ||
|
||
describe('logError', () => { | ||
it('throws an error', () => { | ||
expect(() => logError(arg1, arg2)) | ||
.toThrowError(new Error('You must first configure the loggingService.')); | ||
}); | ||
}); | ||
|
||
describe('logAPIErrorResponse', () => { | ||
it('throws an error', () => { | ||
expect(() => logAPIErrorResponse(arg1, arg2)) | ||
.toThrowError(new Error('You must first configure the loggingService.')); | ||
}); | ||
}); | ||
}); |