Application Logging

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

PeteM
Ebase User
Posts: 31
Joined: Thu Apr 21, 2016 1:04 pm

Application Logging

#1

Postby PeteM » Mon Nov 27, 2017 3:07 pm

Hi,

Is there anyway from within scripts that I can elegantly log the script/function?

Ideally, I'd like to abstract out a logging function that can reflect back to the callstack.

So - say a script "SHAREDFUNCTIONS" had a method "saveTable" in -

Code: Select all

importPackage(org.apache.log4j);
var logger = Logger.getLogger("MyLog");

.....
function saveTable(param)
{
   myLog("Saving stuff");
}

function myLog(message)
{
//some magic here to reflect the call stack
//function = this.callstack.pop
//script = this.name;
   logger.info(function + script + message);
}
would result in a log entry

Code: Select all

[Date time] INFO  SHAREDFUNCTIONS saveTable Saving Stuff
....

I've looked at the various parameters for log4j, but note it's the depreciated 1.2 release, and I can't seem to get this information out?

Cheers,

Peter
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

Re: Application Logging

#2

Postby Jon » Mon Nov 27, 2017 5:08 pm

If there is a Java error, Rhino creates a RhinoException which is the only thing I know that has the intelligence to do what you want. I think this would fail your "elegant" requirement but you can make it work by forcing an exception, then querying the exception object. Something like this:

Code: Select all

function logit()
{
	try
	{
		java.lang.Class.forName("NonExistingClass");
	}
	catch (e)
	{
		log("Script: " + e.rhinoException.sourceName());
		log(e.rhinoException.getScriptStackTrace());
	}
}
I don't know what the overhead might be of constantly throwing exceptions and catching them - probably not very much.

Some useful methods on RhinoException: (see javadoc http://mozilla.github.io/rhino/javadoc/index.html)

sourceName() - script name
details()
getMessage()
getScriptStack() - returns an array of stack elements
getScriptStackTrace() - returns a string of all stack elements
0 x

PeteM
Ebase User
Posts: 31
Joined: Thu Apr 21, 2016 1:04 pm

Re: Application Logging

#3

Postby PeteM » Tue Nov 28, 2017 8:13 am

Fantasic! Thanks Jon - elegance is a state of mind :-)
0 x


Who is online

Users browsing this forum: No registered users and 13 guests