The following code installs a LogListener object for all error messages and then writes the information to a file.
class ErrorLogListener implements LogListener {
FileOutputStream _outFile;
public ErrorLogListener(FileOutputStream outFile) {
_outFile = outFile;
}
public void messageLogged(ServerContext sc, LogMessage msg) {
String user;
try {
if (msg.getType() != LogMessage.ERROR) {
//this class deals exclusively with errors
return;
}
user = msg.getUser();
if (user == null) {
user = "NULL";
}
_outFile.write(("Caught error"
+ " user=" + user
+ " text=" + msg.getText()
+ "\n").getBytes()
);
_outFile.flush();
}
catch(Exception e) {
// Print some error output to the MobiLink log.
e.printStackTrace();
}
}
}
The following code registers an ErrorLogListener object to receive error messages. Call this code from anywhere that has access to the ServerContext such as a class constructor or synchronization script.