Hi,
Thought I’d put this up here in case anyone is interested in using it and may save someone some work. I’m using it on a couple of my own apps now, which time stamp strings in to a state list. These can then be dynamically requested to return x rows. I can’t guarantee it’s the best way of doing it, but it works!
Basically drop it in your app, and call the “set” and “get” commands, to give you a get response like the below, usage examples in the code comments. (The screenshot shows the “length” being pulled from an input)
def updateLog(command, name, length, event){
// HOW TO USE (EXAMPLES):
// TO SET: updateLog("set", "Test", 20, "This text is added to log and time stamped")
// TO GET: updateLog("get", "Test", 20, null)
// TO DELETE: updateLog("set", "Test, 0, null)
//
// command: "set" adds and item to the log, "get" returns the log with a retun between each entry and is non destructive,
// if a shorter length requested than stored then the state value is not trimmed, unless set to '0'.
// name: What you want to call the state ("Test" will be prefixed and stored as"state.logTest"
// length: Logs will only be trimmed on set command, get request will only trim the response,
// if null then there is no length restriction, if 0 the state itself will be removed entirely from ST.
// event: The string to be stored, this will be prefixed with the time in dd MMM HH:mm format (You can change this below)
def logName = "log$name" as String // Add log prefix
if(settings?.length == null || length == 0){state.remove(logName); return "State Cleared"} // If length set to 0, delete state
if(!state."$logName"){state."$logName" = []} // If list state new, create blank list
def tempList = state."$logName" // Create a temp List
// SET OR GET
switch(command) {
case "set":
if(!length || tempList.size() < length){length = tempList.size()+1} // Get valid trim length if short
tempList.add(0,"${new Date(now()).format("dd MMM HH:mm", location.timeZone)} - ${event}") // Add to top of tempList
state."$logName" = tempList.subList(0, length)
break;
case "get":
if(!length || tempList.size() < length){length = tempList.size()} // Get valid trim length if short
def formattedList = ""
tempList = tempList.subList(0, length)
tempList.each { item ->
if(formattedList == ""){
formattedList = item
}else{
formattedList = formattedList + "\n" + item
}
}
return formattedList
break;
}
}