diff --git a/README.md b/README.md index bef8063..deda86f 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/index.d.ts b/index.d.ts index 2429506..484ee1a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -65,6 +65,7 @@ export declare interface SamplingOptions { export declare interface LoggerOptions { access?: boolean | string; customKey?: string; + errorLogging?: boolean; detail?: boolean; level?: string; levels?: { diff --git a/index.js b/index.js index bb753da..cae80a3 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/lib/logger.js b/lib/logger.js index 700ded3..2a2a461 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -144,7 +144,8 @@ exports.config = (config,levels) => { format, access, detail, - sampling + sampling, + errorLogging: cfg.errorLogging !== false } } diff --git a/test/errorHandling.js b/test/errorHandling.js index 6635073..c8f6f46 100644 --- a/test/errorHandling.js +++ b/test/errorHandling.js @@ -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) { @@ -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 ***/ @@ -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