Logging to GroveStreams in lieu of system.log

One of the capabilities I believe I’ll want before moving to the Operations side is the ability to still have access to a system logging function. With that in mind, I considered creating my own logging database, but realized I didn’t need to go to that trouble, since I’m already using GroveStreams and their platform provides functionality to support such a need.

So…today I hacked up a function that’s accessible from both the agent and device and I can now easily “log” data to any of my GroveStreams components. It’s also nice in that I can manually create log entries via my GS account if it’s of value–such as when we move a device in a building. The logs are permanent and could actually be monitored by a GroveStreams event and take action based upon the contents of the logged information.

The intent was to create a function that could be pretty much a direct replacement for “system.log()”

I’m sure there are far more experienced programmers than I who can improve upon the following, but I’m offering this Q&D code in case it can be of benefit to others. I have benefitted so much from what others have shared.

In my environment, the globals along with other device configuration information is retrieved via a call to a MySQL configuration database.

The agent code:
const gsURL = “http://grovestreams.com/api/feed?compid=

//Function to log data to GroveStreams
//Requires the following globals to be set: gsApiKey, compid (component ID)
//deviceID is the device ID and it’s sent from the device to the agent when it starts up (I also use this for other purposes)
//The GroveStreams component should have a stream with an ID of “logfile”. If it doesn’t already exist, GroveStreams will
//automatically create one in the specified component.
function gs_log (stringToLog){
if (gsApiKey==""){
server.log(“I don’t have an apikey and can’t send log data to GroveStreams”);
local atable = {};
atable[“logfile”] <- stringToLog;
local fred = http.urlencode(atable);
local url = gsURL+compid+"&api_key="+gsApiKey+"&"+fred;
local request = http.put(url, {“Host”:“grovestream.com”, “X-Forwarded-For”:deviceID},"");
local response = request.sendsync();
//server.log("HTTP response: "+response.statuscode);
device.on(“gs_log”, gs_log );

//Logging data in the agent
gs_log(“The agent has started”);

The device code: (obviously, it includes an example of how special characters are acceptable)
fred <- 1769.1234;

function gs_log(stuff){

//Logging in the device
gs_log("this is $/{)fred: "+ fred);