| « Installing Cross-Platform Java Applications on a Budget | Simplifying Logging for Blackberry Development, Part 1 » |
Simplifying Logging for Blackberry Development, Part 2
In Part 1, I talked about how to simplify logging to System.out, by creating a set of convenience methods. In Part 2, I will discuss logging to Blackberry's Event Log. The Event Log is a log on the Blackberry device itself and is the place, as the name indicates, where applications can write event logs. This is the place applications should write error logs that can help in troubleshooting problems in the field where the device is not attached to the debugger. Users can access the Event Log by going to the Home screen then holding down ALT and typing "LGLG"
Before an application can write to the event log, it needs to register itself using a GUID and an Application Name. The GUID must be unique for the device, and the Application Name will be used in the log. I suggest setting up some constants for these:
Code:
public static final long GUID = 0x2051fd67b72d11L; | |
public static final String APP_NAME = "PWMinder"; |
To register your application use the following method, typically in the main method of your application:
Code:
EventLogger.register(Constants.GUID, Constants.APP_NAME, EventLogger.VIEWER_STRING); |
EventLogger.VIEWER_STRING indicates that you want to log String messages. This can be changed later to log other message types.
To log an event, use the logEvent() method of EventLogger as follows:
Code:
String msg = "PWMinder has started"; | |
EventLogger.logEvent(Constants.GUID, msg.getBytes(), EventLogger.ALWAYS_LOG); |
EventLogger.ALWAYS_LOG sets the logging level. The following levels are available:DEBUG_INFO
INFORMATION
WARNING
ERROR
SEVERE_ERROR
ALWAYS_LOG
By default, only events at WARNING or lower are logged, but the default level can be changed by calling the setMinimumLevel() method. See the EventLogger API for more details.
To simplify logging to the event log, I have added the following static methods (modelled after log4j) to the Logger class from Part 1:
Code:
private static void logEvent(String msg, int level) { | |
EventLogger.logEvent(Constants.GUID, msg.getBytes(), level); | |
} | |
| |
public static void logDebugEvent(String msg) { | |
logEvent(msg, EventLogger.DEBUG_INFO); | |
} | |
| |
public static void logInformationEvent(String msg) { | |
logEvent(msg, EventLogger.INFORMATION); | |
} | |
| |
public static void logWarningEvent(String msg) { | |
logEvent(msg, EventLogger.WARNING); | |
} | |
| |
public static void logErrorEvent(String msg) { | |
logEvent(msg, EventLogger.ERROR); | |
} | |
| |
public static void logSevereErrorEvent(String msg) { | |
logEvent(msg, EventLogger.SEVERE_ERROR); | |
} | |
| |
public static void logAlwaysEvent(String msg) { | |
logEvent(msg, EventLogger.ALWAYS_LOG); | |
} |
So to log an information message, in my code, I use:
Code:
Logger.logInformationEvent("This is an information method"); |
Much simpler!
In the future, I might look into incorporating changing the viewer type, but so far I mainly just use the STRING_VIEWER.
Hope this is helpful, and happy coding!
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)

