Skip to content

Instantly share code, notes, and snippets.

@abbaspour
Last active July 21, 2022 05:15
Show Gist options
  • Save abbaspour/be355e06c7dba89c63cebc7bcf2a3283 to your computer and use it in GitHub Desktop.
Save abbaspour/be355e06c7dba89c63cebc7bcf2a3283 to your computer and use it in GitHub Desktop.
Auth0 Rules Caching Logger
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;
}
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