Skip to content

Commit bbbfdf3

Browse files
authored
Merge pull request #145 from proxyco/issue-102
Allow disabling of automatic error logging #102
2 parents 30183b2 + 2e962aa commit bbbfdf3

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ Logging can be enabled by setting the `logger` option to `true` when creating th
856856
| Property | Type | Description | Default |
857857
| -------- | ---- | ----------- | ------- |
858858
| access | `boolean` or `string` | Enables/disables automatic access log generation for each request. See [Access Logs](#access-logs). | `false` |
859+
| errorLogging | `boolean` | Enables/disables automatic error logging. | `true` |
859860
| customKey | `string` | Sets the JSON property name for custom data passed to logs. | `custom` |
860861
| detail | `boolean` | Enables/disables adding `REQUEST` and `RESPONSE` data to all log entries. | `false` |
861862
| level | `string` | Minimum logging level to send logs for. See [Logging Levels](#logging-levels). | `info` |

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export declare interface SamplingOptions {
6565
export declare interface LoggerOptions {
6666
access?: boolean | string;
6767
customKey?: string;
68+
errorLogging?: boolean;
6869
detail?: boolean;
6970
level?: string;
7071
levels?: {

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,14 @@ class API {
280280

281281
if (e instanceof Error) {
282282
message = e.message
283-
this.log.fatal(message,info)
283+
if (this._logger.errorLogging) {
284+
this.log.fatal(message, info)
285+
}
284286
} else {
285287
message = e
286-
this.log.error(message,info)
288+
if (this._logger.errorLogging) {
289+
this.log.error(message, info)
290+
}
287291
}
288292

289293
// If first time through, process error middleware

lib/logger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ exports.config = (config,levels) => {
144144
format,
145145
access,
146146
detail,
147-
sampling
147+
sampling,
148+
errorLogging: cfg.errorLogging !== false
148149
}
149150
}
150151

test/errorHandling.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const api4 = require('../index')({ version: 'v1.0' })
1111
const api5 = require('../index')({ version: 'v1.0', logger: { access: 'never' }})
1212
const api_errors = require('../index')({ version: 'v1.0' })
1313
const api6 = require('../index')() // no props
14+
const api7 = require('../index')({ version: 'v1.0', logger: { errorLogging: false }})
15+
const api8 = require('../index')({ version: 'v1.0', logger: { access: 'never', errorLogging: true }})
1416

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

182+
api7.get('/testErrorThrow', function(req,res) {
183+
throw new Error('This is a test thrown error')
184+
})
185+
186+
api8.get('/testErrorThrow', function(req,res) {
187+
throw new Error('This is a test thrown error')
188+
})
180189

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

348+
it('Should not log error if option logger.errorLogging is false', async function() {
349+
let _log
350+
let _event = Object.assign({},event,{ path: '/testErrorThrow'})
351+
let logger = console.log
352+
console.log = log => { try { _log = JSON.parse(log) } catch(e) { _log = log } }
353+
let result = await new Promise(r => api7.run(_event,{},(e,res) => { r(res) }))
354+
console.log = logger
355+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"This is a test thrown error"}', isBase64Encoded: false })
356+
expect(_log).to.equal(undefined)
357+
})
358+
359+
it('Should log error if option logger.errorLogging is true', async function() {
360+
let _log
361+
let _event = Object.assign({},event,{ path: '/testErrorThrow'})
362+
let logger = console.log
363+
console.log = log => { try { _log = JSON.parse(log) } catch(e) { _log = log } }
364+
let result = await new Promise(r => api8.run(_event,{},(e,res) => { r(res) }))
365+
console.log = logger
366+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"This is a test thrown error"}', isBase64Encoded: false })
367+
expect(_log.level).to.equal('fatal')
368+
expect(_log.msg).to.equal('This is a test thrown error')
369+
})
370+
339371
})
340372

341373
}) // end ERROR HANDLING tests

0 commit comments

Comments
 (0)