This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support logging an item on response by decorating request object (
#34) This is a similar functionality to the one we have in auth0-server and allows as to add properties to the response log, this is useful to log the state of rate limits and things like those without having to add an extra log.
- Loading branch information
Showing
5 changed files
with
130 additions
and
9 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
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,39 @@ | ||
const _ = require('lodash'); | ||
|
||
const MAX_LOG_ITEMS = 20; | ||
|
||
module.exports = (logger) => { | ||
let items = []; | ||
|
||
function addValue(key, value) { | ||
if (items.length > MAX_LOG_ITEMS) { | ||
// This prevents memory leaks by not allowing an infinite amount | ||
// of objects to be collected as part of log items | ||
logger.error({ | ||
log_type: 'too_many_log_on_response', | ||
items | ||
}, | ||
'The maximum amount of data to log has been exceed. We will ' + | ||
'log existing items as part of this error and cleanup items'); | ||
|
||
items = []; | ||
} | ||
|
||
items.push({ key, value }); | ||
} | ||
|
||
function injectOn(carrier) { | ||
if (typeof carrier !== 'object' || carrier === null) { | ||
return; | ||
} | ||
|
||
for (let item of items) { | ||
_.set(carrier, item.key, item.value); | ||
} | ||
} | ||
|
||
return { | ||
addValue, | ||
injectOn | ||
}; | ||
}; |
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