Wednesday, January 13, 2010

Quick and Dirty Logging

One of the best ways to debug your BIRT Expressions and JavaScript is logging.  I have worked with a number of different techniques for logging, and finally settled on using the script functions to create a logging utility.  This approach can be seen in the birt-functions-lib project.

But I frequently don't have the functions library, and I just want examine a value or an object.  I need quick and dirty logging, not a logging infrastructure.

As you probably know, BIRT JavaScript can interact with Java objects natively.  So I should be able to invoke

java.lang.System.out.println("message");
and see an output message.  There are just two tiny 'tricks' you need to know about to make this work.

First, the Eclipse application that BIRT runs under has a console mode.  To run eclipse in console mode run eclipsec.exe instead of eclipse.exe.  


Second, in order to access the java.lang package you need to preface your method call with the keyword Packages, as in:
Packages.java.lang.System.out.println("message");

As you can see, your message will appear in the console window, quick and dirty.



If you are going to be doing a lot of quick and dirty logging, you can use the importPackage method to clean things up:

importPackage(Packages.java.lang);
System.out.println("message with import package");

You can even create a global function that will wrap off the message generation if you are feeling motivated.

importPackage(Packages.java.lang);
function log(msg){
    System.out.println(msg);
}
reportContext.setGlobalVariable("log", log);

log("Test global function");

But my feeling is that once you have reached that level, it would be just easier to use a script function and allow people to select the function through the UI.  Did I mention that there is an open source functions library that will do this for you?

5 comments:

Anonymous said...

Thank you, this is very helpful!
DM

Anonymous said...

Your solution is quick and easy to use. Thank you!

Anonymous said...

Thanks. This helped a lot in troubleshooting and understanding Birt better.

BTW, your "C is for console" graphic makes me think "and that's good enough for me."

Anonymous said...

Thanks a ton.

airinarahmad said...

Verry thoughtful blog