Skip to content

Commit ff72770

Browse files
Merge pull request #238 from benheymink/master
feat: added support for setting debug logging within CorrelationIds
2 parents d870d55 + 1887e9b commit ff72770

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

packages/lambda-powertools-correlation-ids/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Main features:
88

99
* respects convention for correlation IDs - i.e. `x-correlation-`
1010

11+
* Manually enable/disable debug logging (`debug-log-enabled`) to be picked up by other/downstream middleware
12+
1113
* allows you to store more than one correlation IDs, which allows you to *correlate* logs on multiple dimensions (e.g. by `x-correlation-user-id`, or `x-correlation-order-id`, etc.)
1214

1315
## Getting Started
@@ -23,17 +25,24 @@ const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')
2325
CorrelationIds.set('id', '12345678') // records id as x-correlation-id
2426
CorrelationIds.set('x-correlation-username', 'theburningmonk') // records as x-correlation-username
2527

28+
// Manully enable debug logging (debug-log-enabled)
29+
CorrelationIds.debugLoggingEnabled = true
30+
2631
const myCorrelationIds = CorrelationIds.get()
2732
// {
2833
// 'x-correlation-id': '12345678',
29-
// 'x-correlation-username': 'theburningmonk'
34+
// 'x-correlation-username': 'theburningmonk',
35+
// 'debug-log-enabled': 'true'
3036
// }
3137

3238
CorrelationIds.clearAll() // removes all recorded correlation IDs
3339
CorrelationIds.replaceAllWith({ // bypasses the 'x-correlation-' convention
34-
'DEBUG-LOG-ENABLED': 'true',
40+
'debug-log-enabled': 'true',
3541
'User-Agent': 'jest test'
3642
})
43+
44+
// Disable debug logging
45+
CorrelationIds.debugLoggingEnabled = false
3746
```
3847

3948
In practice, you're likely to only need `set` when you want to record correlation IDs from your function.

packages/lambda-powertools-correlation-ids/__tests__/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ const suite = (correlationIds) => () => {
4343
})
4444
})
4545

46+
describe('get/set debug logging', () => {
47+
it('GETS and SETS state of debug logging', () => {
48+
correlationIds.clearAll()
49+
expect(correlationIds.debugLoggingEnabled).toBe(false)
50+
correlationIds.debugLoggingEnabled = true
51+
expect(correlationIds.debugLoggingEnabled).toBe(true)
52+
expect(correlationIds.get()).toEqual({
53+
'debug-log-enabled': 'true'
54+
})
55+
})
56+
})
57+
4658
describe('.replaceAllWith', () => {
4759
beforeEach(() => {
4860
correlationIds.set('x-correlation-id', 'this should be replaced')

packages/lambda-powertools-correlation-ids/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ class CorrelationIds {
2525
return this.context
2626
}
2727

28-
get debugEnabled () {
28+
get debugLoggingEnabled () {
2929
return this.context[DEBUG_LOG_ENABLED] === 'true'
3030
}
3131

32+
set debugLoggingEnabled (enabled) {
33+
this.context[DEBUG_LOG_ENABLED] = enabled ? 'true' : 'false'
34+
}
35+
3236
static clearAll () {
3337
globalCorrelationIds.clearAll()
3438
}
@@ -44,7 +48,15 @@ class CorrelationIds {
4448
static get () {
4549
return globalCorrelationIds.get()
4650
}
47-
};
51+
52+
static get debugLoggingEnabled () {
53+
return globalCorrelationIds.debugLoggingEnabled
54+
}
55+
56+
static set debugLoggingEnabled (enabled) {
57+
globalCorrelationIds.debugLoggingEnabled = enabled
58+
}
59+
}
4860

4961
if (!global.CORRELATION_IDS) {
5062
global.CORRELATION_IDS = new CorrelationIds()

0 commit comments

Comments
 (0)