Exception and log have to be processed when developing any software, as does Eclipse plug-in. Fortunately, the eclipse PVDF provides a mechanism to record and display exceptions and logs: Error Log view. As part of the eclipse SDK, the penetration rate of PDIS very high, so unless you are doing RCP, you can use error log view to handle exception
And log should be your best choice. Of course, this also brings about the dependence on PDE.
Using error log view is actually very simple. Each plug-in activator class has a getlog () method, which returns an ilog object, this object records the exception and log to the error log view. The main method for ilog objects is log. As the name suggests, it receives an istatus-type object and records its status. Eclipse and many common plug-ins (such as jdt) implement many istatus, and the most common is the status class. We can simply use it or create our own
Istatus implementation. The status constructor has five parameters:
In this way, we can write a logutil class to take charge of log work,CodeAs follows:
Package demo; import Org. eclipse. core. runtime. ilog; import Org. eclipse. core. runtime. status; import XXX. pluginactivator; public class eclipseerrorlog {Private Static ilog logger = NULL; static {logger = pluginactivator. getdefault (). getlog ();} private eclipseerrorlog () {// nothing to do} public static void logcancel (string message, throwable exception) {logger. log (new status (status. cancel, pluginactivator. plugin_id, status. OK, message, exception);} public static void logerror (string message, throwable exception) {logger. log (new status (status. error, pluginactivator. plugin_id, status. OK, message, exception);} public static void loginfo (string message, throwable exception) {logger. log (new status (status. info, pluginactivator. plugin_id, status. OK, message, exception);} public static void logok (string message, throwable exception) {logger. log (new status (status. OK, pluginactivator. plugin_id, status. OK, message, exception);} public static void logwarning (string message, throwable exception) {logger. log (new status (status. warning, pluginactivator. plugin_id, status. OK, message, exception ));}}
In addition, you can use the addloglistener method of ilog and the removeloglistener method to add and delete event listeners for log actions. These listener can help us do some additional things after logging is complete. For example, if the log is recorded at the error level, an alert dialog box may pop up to tell the user that an error has occurred. However, if the log is at the info level, this is unnecessary.
Note:
This article Reprinted from: http://hintcnuie.javaeye.com/blog/787932
- int severity: log level, which can be OK, error, info, warning, or cancel. These constants are defined in the status class.
- string pluginid: ID of the current plug-in.
- int code: Status Code specified by plug-in. Generally, status. OK is used if this parameter is not required.
- string message: log information.
- throwable exception: exception of the record. If no exception exists, null is input.