Last active
March 26, 2020 06:05
-
-
Save sandeepneerarambham/ce9fb336457a4297b8da35b8059f1591 to your computer and use it in GitHub Desktop.
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
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