Skip to content

Commit

Permalink
Allow disabling of automatic error logging #102
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Sjoberg committed Feb 14, 2020
1 parent 30183b2 commit 2e962aa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ Logging can be enabled by setting the `logger` option to `true` when creating th
| Property | Type | Description | Default |
| -------- | ---- | ----------- | ------- |
| access | `boolean` or `string` | Enables/disables automatic access log generation for each request. See [Access Logs](#access-logs). | `false` |
| errorLogging | `boolean` | Enables/disables automatic error logging. | `true` |
| customKey | `string` | Sets the JSON property name for custom data passed to logs. | `custom` |
| detail | `boolean` | Enables/disables adding `REQUEST` and `RESPONSE` data to all log entries. | `false` |
| level | `string` | Minimum logging level to send logs for. See [Logging Levels](#logging-levels). | `info` |
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export declare interface SamplingOptions {
export declare interface LoggerOptions {
access?: boolean | string;
customKey?: string;
errorLogging?: boolean;
detail?: boolean;
level?: string;
levels?: {
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,14 @@ class API {

if (e instanceof Error) {
message = e.message
this.log.fatal(message,info)
if (this._logger.errorLogging) {
this.log.fatal(message, info)
}
} else {
message = e
this.log.error(message,info)
if (this._logger.errorLogging) {
this.log.error(message, info)
}
}

// If first time through, process error middleware
Expand Down
3 changes: 2 additions & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ exports.config = (config,levels) => {
format,
access,
detail,
sampling
sampling,
errorLogging: cfg.errorLogging !== false
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/errorHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const api4 = require('../index')({ version: 'v1.0' })
const api5 = require('../index')({ version: 'v1.0', logger: { access: 'never' }})
const api_errors = require('../index')({ version: 'v1.0' })
const api6 = require('../index')() // no props
const api7 = require('../index')({ version: 'v1.0', logger: { errorLogging: false }})
const api8 = require('../index')({ version: 'v1.0', logger: { access: 'never', errorLogging: true }})

class CustomError extends Error {
constructor(message,code) {
Expand Down Expand Up @@ -177,6 +179,13 @@ api6.get('/testError', function(req,res) {
res.error('This is a test error message')
})

api7.get('/testErrorThrow', function(req,res) {
throw new Error('This is a test thrown error')
})

api8.get('/testErrorThrow', function(req,res) {
throw new Error('This is a test thrown error')
})

/******************************************************************************/
/*** BEGIN TESTS ***/
Expand Down Expand Up @@ -336,6 +345,29 @@ describe('Error Handling Tests:', function() {
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"This is a test error message"}', isBase64Encoded: false })
}) // end it

it('Should not log error if option logger.errorLogging is false', async function() {
let _log
let _event = Object.assign({},event,{ path: '/testErrorThrow'})
let logger = console.log
console.log = log => { try { _log = JSON.parse(log) } catch(e) { _log = log } }
let result = await new Promise(r => api7.run(_event,{},(e,res) => { r(res) }))
console.log = logger
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"This is a test thrown error"}', isBase64Encoded: false })
expect(_log).to.equal(undefined)
})

it('Should log error if option logger.errorLogging is true', async function() {
let _log
let _event = Object.assign({},event,{ path: '/testErrorThrow'})
let logger = console.log
console.log = log => { try { _log = JSON.parse(log) } catch(e) { _log = log } }
let result = await new Promise(r => api8.run(_event,{},(e,res) => { r(res) }))
console.log = logger
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"This is a test thrown error"}', isBase64Encoded: false })
expect(_log.level).to.equal('fatal')
expect(_log.msg).to.equal('This is a test thrown error')
})

})

}) // end ERROR HANDLING tests

0 comments on commit 2e962aa

Please sign in to comment.