| « Simplifying Logging for Blackberry Development, Part 2 | Calling Javascript from a Java Applet » |
Simplifying Logging for Blackberry Development, Part 1
I have recently started developing a version of PWMinder, for the Blackberry, using the JDE plugin for Eclipse. When developing applications, especially when starting out, developers often like to send debug/log statements to System.out, to test or check certain values. My first problem was, when I ran my programs using the simulator, I didn't see any of my System.out messages going to the console. After searching on the Blackberry Developer forums, I found that in order to see messages on the console, the program needs to be run in debug mode. That was fine, until I realized that not only my messages were going to the console but a lot of message produced by the simuator; messages like:
VM:+GC(F)w=9
VM:-GCt=3,b=1,r=0,g=1
VM:+CR
VM:TI2Rv=31
VM:-CR=1
VM:IGCSc=0
JVM: bklt @15785: timer
JVM: bklt @15787: idle 23
So the problem became finding my messages among all those others. One tip I learned was to add some unique constant value in front of all messages like: *** to make them stand out. While a good idea, this means extra work every time I want to send a log message. To get around this, I decided to make a couple methods that I could use to log messages to System.out in a standard format.
Firstly, I created a Class called Constants where I store several Global constants and I declared the following:
Code:
public static final String APP_NAME = "PWMinder"; |
Next, I created a class called Logger, which contains several static methods to used for logging messages.
Code:
public class Logger { | |
| |
//Used to format dates into a standard format | |
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); | |
| |
public static void out(String msg) { | |
System.out.println(setUpMessageString(msg)); | |
} | |
| |
public static void err(String msg, Throwable t) { | |
System.err.println(setUpMessageString(msg)); | |
t.printStackTrace(); | |
} | |
| |
private static String setUpMessageString(String msg) { | |
Date timestamp = new Date(); | |
StringBuffer sb = new StringBuffer(); | |
sb.append("***"); | |
sb.append(Constants.APP_NAME); | |
sb.append(" - "); | |
sb.append(dateFormat.format(timestamp)); | |
sb.append(": "); | |
sb.append(msg); | |
return sb.toString(); | |
} | |
} |
Now, when I'm coding, whenever I want to send a message to System.out I used the following:
Code:
Logger.out("This is my message"); |
Which when run would give:***PWMinder - 2009-12-17 10:30:46.577 America/Los_Angeles: This is my message
In the same way, I could send something to System.err:
Code:
try { | |
//come code | |
} catch (Exception e) { | |
Logger.err("Something bad happened", t); | |
} |
This makes logging to the console easier, and provides consistent output, making it easier to search for my messages among all the other message. It also allows me to easily make changes to my logging format (e.g. change my date format, etc.) by only changing the code in one place.
Hope this was helpful.
Environment:
Eclipse 3.5
Blackberry JDE plugin 4.2.1
In Part 2, I will discuss logging to the Event Logger, on the Blackberry and/or simulator
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)

