-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add Aietes identifier as a prefix for each log trace: - new module to provide 'info', 'warn' and 'error' functions * Issue #33 added Aietes prefix to access logs, use central log methods consistently throughout, clean up old logging module * Issue #33 added unit test for logging Co-authored-by: benjaiser <devjaiser@gmail.com>
- Loading branch information
Showing
7 changed files
with
106 additions
and
25 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
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
const morgan = require('morgan'); | ||
|
||
const LOGGING_PREFIX = '[Aietes]'; | ||
|
||
const info = data => { | ||
if (!process.env.NO_OUTPUT) { | ||
console.log(`${LOGGING_PREFIX} ${data}`); | ||
} | ||
}; | ||
|
||
const warn = data => { | ||
if (!process.env.NO_OUTPUT) { | ||
console.warn(`${LOGGING_PREFIX} ${data}`); | ||
} | ||
}; | ||
|
||
const error = data => { | ||
console.error(`${LOGGING_PREFIX} ${data}`); | ||
}; | ||
|
||
module.exports = { | ||
log: { info, warn, error }, | ||
accessLog: morgan(`${LOGGING_PREFIX} :method :url :status :response-time[1] ms - :res[content-length] b`) | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { log } = require('../lib/logging'); | ||
|
||
describe('logging facade', () => { | ||
it('should pass info to the console.log and prepend with Aietes', () => { | ||
const infoMock = jest.fn(); | ||
console.log = infoMock; | ||
|
||
log.info('message'); | ||
|
||
expect(infoMock).toHaveBeenCalled(); | ||
expect(infoMock).toHaveBeenCalledWith('[Aietes] message'); | ||
}); | ||
|
||
it('should pass warn to the console.warn and prepend with Aietes', () => { | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
|
||
log.warn('message'); | ||
|
||
expect(warnMock).toHaveBeenCalled(); | ||
expect(warnMock).toHaveBeenCalledWith('[Aietes] message'); | ||
}); | ||
|
||
it('should pass calls to info to the console and prepend with Aietes', () => { | ||
const errorMock = jest.fn(); | ||
console.error = errorMock; | ||
|
||
log.error('message'); | ||
|
||
expect(errorMock).toHaveBeenCalled(); | ||
expect(errorMock).toHaveBeenCalledWith('[Aietes] message'); | ||
}); | ||
|
||
it('should skip console calls for info and warn if environment variable is set', () => { | ||
const OLD_ENV = process.env; | ||
jest.resetModules(); // this is important - it clears the cache | ||
process.env = { ...OLD_ENV }; | ||
delete process.env.NODE_ENV; | ||
|
||
process.env.NO_OUTPUT = true; | ||
|
||
const infoMock = jest.fn(); | ||
console.log = infoMock; | ||
const warnMock = jest.fn(); | ||
console.warn = warnMock; | ||
const errorMock = jest.fn(); | ||
console.error = errorMock; | ||
|
||
log.info('info'); | ||
log.warn('warn'); | ||
log.error('error'); | ||
|
||
expect(infoMock).toHaveBeenCalledTimes(0); | ||
expect(warnMock).toHaveBeenCalledTimes(0); | ||
expect(errorMock).toHaveBeenCalledTimes(1); | ||
|
||
process.env = OLD_ENV; | ||
}); | ||
}); |