Skip to content

Instantly share code, notes, and snippets.

@sandeepneerarambham
Last active March 26, 2020 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandeepneerarambham/ce9fb336457a4297b8da35b8059f1591 to your computer and use it in GitHub Desktop.
Save sandeepneerarambham/ce9fb336457a4297b8da35b8059f1591 to your computer and use it in GitHub Desktop.
const LOG_TAG = '[Login]: ';
const start = process.hrtime();
const timeTaken = (start) => {
if (!start) {
return '';
}
const precision = 3; // 3 decimal places
const elapsed = process.hrtime(start)[1] / 1000000; // divide by a million to get nano to milli
return elapsed.toFixed(precision);
};
const sumoEndpoint = configuration.SUMO_COLLECTOR_ENDPOINT;
const logLevel = parseInt(configuration.DB_LOG_LEVEL, 10) || 0;
var sLogger = undefined;
if (sumoEndpoint) {
const SumoLogger = require('sumo-logger');
const hostName = configuration.HOST_NAME;
const options = {
raw: true,
endpoint: sumoEndpoint,
sourceName: 'Auth0',
sourceCategory: 'auth0-db-scripts-logs',
interval: 9000,
hostName,
};
sLogger = new SumoLogger(options);
}
const sLog = (logType, ...text) => {
const formattedLog = text.reduce((acc, value) => {
return acc + ((typeof value === 'object') ? JSON.stringify(value) : value);
});
if (sumoEndpoint) {
return sLogger.log('Time:' + new Date().getTime() + ' [' + logType + '] ' + formattedLog);
} else {
return console.log('Time:' + new Date().getTime() + ' [' + logType + '] ' + formattedLog);
}
};
const sumoLog = {
error: (...logs) => { logLevel >= 1 ? sLog('ERROR', ...logs) : ''; },
info: (...logs) => { logLevel >= 2 ? sLog('INFO', ...logs) : ''; }
};
const flushLogs = () => {
if (sLogger) {
return sLogger.flushLogs();
} else {
return true;
}
};
sumoLog.info(LOG_TAG, `${email} : entry - ${timeTaken(start)}ms`);
sumoLog.info(LOG_TAG, `${email}, ${auth0User.user_id}: exit - user successfully migrated, ${timeTaken(start)}ms`);
flushLogs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment