Error messages, warnings or just informations can be send to the logger from own classes. In addition additional handlers can be added to and removed from the logger.
To send a message to the logger, first the eva/3 Logger, to send the message to, must be recieved. After this a message can be send to the logger. Das The example shows the sending of an error message, that occours after an exception. Very important is the indication of the exception referenz, when present, and the classname that gan be get by the call
this.getClass().getName()
Likewise also the class with class path can be given as String.
import java.util.logging.Level; import java.util.logging.Logger; import com.odc.eva3.rt.command.Command; import com.odc.eva3.rt.macro.Macro; public class LoggerExample implements Command { /** * Logger to protocol errors and messages */ protected static Logger logger = Logger.getLogger("com.odc.eva3.rt); /** * Runs the command. <br> * This is the entry point for eva/3 <code>Commands</code>. An executing < code>macro</code> calls <code>action</code> with the parameters defined in * the eva/3 properties editor. <br> * <br> * * @param args the parameter values. The parameter values are defined in the eva/3 Property-Editor in the Report- or Form-Editor. * @param macro the calling macro */ public Object action(Object[] args, Macro macro) { try { /* * do something which can cause an exception */ int i = 0 / 0; } catch (Throwable t) { /* * Log a message, specifying source class and method, * with associated Throwable information. * (Log level, Class name, Method name, Message, Exception object) */ logger.logp(Level.SEVERE, this.getClass().getName(), "action", "An Error has occured", t); } return null; } }
To send an information message, the more compact method
logger.log(Level.INFO, "Message");
can be used.
In order to add a Handler from an own class, the eva/3 logger is to be gotten. Subsequently, a further Handler can be added to the logger:
public Object action(Object[] args, Macro macro) {
MessageboxHandler handler = new MessageboxHandler();
logger.addHandler(handler);
return null;
}