Last active
July 21, 2022 05:15
-
-
Save abbaspour/be355e06c7dba89c63cebc7bcf2a3283 to your computer and use it in GitHub Desktop.
Auth0 Rules Caching Logger
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
function boostrapRule(user, context, callback) { | |
/** | |
* Create a new Logger instance | |
* | |
* @returns {{log: (function(*=): number), commitLogs: (function(): void)}} | |
*/ | |
const loggerFactory = () => { | |
const logs = []; | |
const commitLogs = () => { | |
if (logs.length === 0) | |
return; | |
console.log(logs); // or SEND logs to your choice of enables; sumo, cloudwatch, etc | |
}; | |
return { | |
/** | |
* Submit the log cache to sumo | |
* | |
* @return {(function(): void)} | |
*/ | |
commitLogs, | |
/** | |
* log a single message to the cache | |
* | |
* @param message - mesage to send to sumo | |
* @returns {number} | |
*/ | |
log: (message) => logs.push(message), | |
resetLogs: () => logs.length = 0, | |
}; | |
}; | |
global.loggerFactory = loggerFactory; | |
} |
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
function mybusinesslogic(user, context, callback) { | |
const { log, commitLogs, resetLogs } = global.loggerFactory(); | |
try { | |
// do business logic | |
log("step 1 for user: " + user.email); | |
// more logic | |
log("step 2 for user: " + user.email); | |
log("finished success"); | |
if(random % 100 <= tracinglevel) { | |
commitLogs(); | |
} | |
} catch { | |
log("facing exception", e); | |
commitLogs(); | |
} finally { | |
resetLogs(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment