Forked from rogernolan/gist:95ea615164e343b3bc54
Last active
August 29, 2015 14:10
-
-
Save m0wfo/759ce554750df5b136d2 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
/// | |
/// Parse + Logentries integration- based on this gist: https://gist.github.com/rogernolan/95ea615164e343b3bc54 | |
/// proof-of-concept; might have the odd typo | |
/// | |
var DEBUG = false; | |
var logger = (function(){ | |
// Rely on Parse.com using node module caching to not duplicate this stuff. | |
var logToken = 'not-set'; | |
var urlSet = false; | |
var endpoint; | |
var appName = "CloudCode"; | |
var alsoLogToConsole = true; | |
console.log("setting up logger"); | |
function setupURL(){ | |
endpoint = 'https://js.logentries.com/v1/logs/' + logToken | |
urlSet = true; | |
} | |
// Return from the clouse executed as main entry point to this module. | |
// effectively the 'public' methods for this 'singleton' | |
return { | |
setToken: function (token, newAppName){ | |
console.log("Setting log token to " + token); | |
logToken = token; | |
appName = newAppName; | |
urlSet = false; | |
}, | |
setConsoleLogging: function (shouldLog){ | |
alsoLogToConsole = shouldLog; | |
}, | |
log: function(logString, extraLogData){ | |
if(!urlSet) | |
setupURL(); | |
var date = new Date(); | |
// Build the body to send to Logentries :) | |
var body; | |
if(typeof(logString) === 'string') { | |
if(alsoLogToConsole) | |
console.log(logString); | |
var date = new Date(); | |
body = {'rawlogmessage' : logString, | |
'devicename' : 'cloudcode', | |
'appname': appName, | |
'timestamp' : date | |
}; | |
if(typeof (extraLogData) !== 'undefined' ) { | |
for (var attribute in extraLogData) { | |
obj1[attribute] = extraLogData[attribute]; | |
} | |
} | |
} | |
else { | |
// We have an object as our first paramater so simply send that | |
// without any extra adornment | |
if(alsoLogToConsole) | |
console.log(JSON.stringify(logString)); | |
body = logString; | |
} | |
if(DEBUG) | |
console.log("logging " + JSON.stringify(body) + " to : " + endpoint); | |
Parse.Cloud.httpRequest({ | |
'method': 'POST', | |
'url': endpoint, | |
'headers': { | |
'Content-Type': 'application/json' | |
}, | |
'body': {event: body}, | |
success: function(response) { | |
if(DEBUG) | |
console.log('successfully logged: ' + response.text) | |
}, | |
error: function(httpResponse) { | |
if(DEBUG) | |
console.log('Logentries request failed with response code ' + httpResponse.status + ' url was ' + endpoint) | |
} | |
}); | |
} | |
} | |
})(); // Trailing () causes the closure to execute. We export the return object. | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment