-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Hapi-pino logger to clean things up (#49)
The first tweak is around config. We were pulling it into our main `app/server.js` and `HapiPinoIgnoreRequestService`. This meant we also needed to stub the config to get tests working for the `HapiPinoIgnoreRequestService`. By moving the config into the plugin we can use it where it's needed, and ensure it gets passed through as an arg. This means `LogConfig` is now only referenced in one place and we can remove the stubbing in our `HapiPinoIgnoreRequestService` tests because we're passing the critical option through. The second was a mix of conventions. We were using a separate service to contain the logic of whether a request should be logged but a function in the plugin to cover logging in tests. By moving the logging in tests logic to a service we get consistency _and_ we can write some unit tests to cover it.
- Loading branch information
1 parent
f5b1d68
commit 11632f2
Showing
6 changed files
with
112 additions
and
58 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 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,34 @@ | ||
'use strict' | ||
|
||
/** | ||
* Used by HapiPinoPlugin to determine which requests to log | ||
* @module HapiPinoLogInTestService | ||
*/ | ||
|
||
/** | ||
* Returns test configuration options for the hapi-pino logger | ||
* | ||
* When we run our unit tests we don't want the output polluted by noise from the logger. So as a default we set the | ||
* configuration to tell hapi-pino to ignore all events. | ||
* | ||
* But there will be times when trying to diagnose an issue that we will want log output. So using an env var we can | ||
* override the default and tell hapi-pino to log everything as normal. | ||
* | ||
* @returns {Object} an empty object or one containing Hapi-pino config to tell it not to log events | ||
*/ | ||
function go (logInTest) { | ||
if (process.env.NODE_ENV !== 'test' || logInTest) { | ||
return {} | ||
} | ||
|
||
return { | ||
// Don't log requests etc | ||
logEvents: false, | ||
// Don't log anything tagged with DEBUG or info, for example, req.log(['INFO'], 'User is an admin') | ||
ignoredEventTags: { log: ['DEBUG', 'INFO'], request: ['DEBUG', 'INFO'] } | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
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
61 changes: 61 additions & 0 deletions
61
test/services/plugins/hapi-pino-log-in-test.service.test.js
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,61 @@ | ||
'use strict' | ||
|
||
// Test framework dependencies | ||
const Lab = require('@hapi/lab') | ||
const Code = require('@hapi/code') | ||
const Sinon = require('sinon') | ||
|
||
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() | ||
const { expect } = Code | ||
|
||
// Thing under test | ||
const HapiPinoLogInTestService = require('../../../app/services/plugins//hapi-pino-log-in-test.service.js') | ||
|
||
describe('Hapi Pino Log In Test service', () => { | ||
afterEach(() => { | ||
Sinon.restore() | ||
}) | ||
|
||
describe('when unit tests are running', () => { | ||
describe('and we tell it to log events', () => { | ||
it('returns an empty object - hapi-pino is not silenced', () => { | ||
const result = HapiPinoLogInTestService.go(true) | ||
|
||
expect(result).to.equal({}) | ||
}) | ||
}) | ||
|
||
describe('and we tell it not to log events in test', () => { | ||
it('returns an object containing config to silence hapi-pino', () => { | ||
const result = HapiPinoLogInTestService.go(false) | ||
|
||
expect(result).to.equal({ | ||
logEvents: false, | ||
ignoredEventTags: { log: ['DEBUG', 'INFO'], request: ['DEBUG', 'INFO'] } | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when unit tests are not running', () => { | ||
beforeEach(() => { | ||
Sinon.stub(process, 'env').value({ ...process.env, NODE_ENV: 'development' }) | ||
}) | ||
|
||
describe('and we tell it not to log events in test', () => { | ||
it('returns an empty object - hapi-pino is not silenced', () => { | ||
const result = HapiPinoLogInTestService.go(false) | ||
|
||
expect(result).to.equal({}) | ||
}) | ||
}) | ||
|
||
describe('and we tell it to log events in test', () => { | ||
it('returns an empty object - hapi-pino is not silenced', () => { | ||
const result = HapiPinoLogInTestService.go(true) | ||
|
||
expect(result).to.equal({}) | ||
}) | ||
}) | ||
}) | ||
}) |