Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.
指示日志消息属于错误。
final int LogMessage.ERROR
下面的代码为所有错误消息安装 [LogListener] 对象,然后将信息写到一个文件中。
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(); } } }
以下代码注册 [ErrorLogListener] 对象来接收错误消息。可以从有权访问 [ServerContext] 的任何位置(例如类构造函数或同步脚本)调用此代码。
ServerContext serv_context; FileOutputStream outFile serv_context.addErrorListener(new ErrorLogListener(outFile));