-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature(cls): Move from cls-hooked to AsyncLocalStorage (#69) * fix(middleware): Fix addLogMetadata function and update dependencies (#72) * Add node version + experimental features in the readme (#74)
- Loading branch information
1 parent
bfe9ad7
commit cbf8a89
Showing
9 changed files
with
765 additions
and
699 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { AsyncLocalStorage, AsyncResource } = require('async_hooks'); | ||
|
||
const appMetadataKey = 'logMetadata'; | ||
const appKey = 'express-wolox-logger'; | ||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
|
||
const getLogStore = () => asyncLocalStorage && asyncLocalStorage.getStore(); | ||
|
||
const getLogMetadata = () => { | ||
const logStore = getLogStore(); | ||
return (logStore && logStore.get(appMetadataKey)) || {}; | ||
}; | ||
|
||
const addLogMetadata = newMetadata => { | ||
const actualStore = getLogStore(); | ||
const actualLogMetadata = getLogMetadata(); | ||
if (actualStore) { | ||
actualStore.set(appMetadataKey, { ...actualLogMetadata, ...newMetadata }); | ||
} | ||
}; | ||
|
||
const getRequestId = () => getLogMetadata().requestId || null; | ||
|
||
const setRequestId = id => { | ||
addLogMetadata({ requestId: id }); | ||
return id; | ||
}; | ||
|
||
const runWithContext = callback => { | ||
const resource = new AsyncResource(appKey); | ||
return (...args) => resource.runInAsyncScope(callback, null, ...args); | ||
}; | ||
|
||
const getInitialContext = ({ requestId }) => { | ||
const context = new Map(); | ||
context.set(appMetadataKey, { requestId }); | ||
return context; | ||
}; | ||
|
||
module.exports = { | ||
runWithContext, | ||
getInitialContext, | ||
asyncLocalStorage, | ||
getRequestId, | ||
setRequestId, | ||
getLogStore, | ||
getLogMetadata, | ||
addLogMetadata | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
const loggers = require('./loggers'); | ||
const middlewares = require('./middlewares'); | ||
const namespace = require('./namespace'); | ||
const context = require('./context'); | ||
|
||
module.exports = { | ||
...loggers, | ||
...middlewares, | ||
...namespace | ||
...context | ||
}; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.