69. XPages里的Java日志器(三)

2014-11-24 08:20:07 · 作者: · 浏览: 2
ycle it. //Even recycling the current db doesn't cause any error. this.logDb.recycle(); } } }
package starrow;

import starrow.xsp.XSPUtil;
import lotus.domino.NotesException;

public class BeanLogger extends NotesLogger implements java.io.Serializable{

    private static final long serialVersionUID = 1L;

    public BeanLogger(String dbPath) throws NotesException {
        super(XSPUtil.getSession());
        this.dbPath=dbPath;
        logLevel = LEVEL_DEBUG;
    }

    /**
     * Add a constructor with no parameter for bean creation
     * @throws NotesException
     */
    public BeanLogger() throws NotesException{
        this("");
    }
    
    public void log(int level, Object message) throws NotesException{
        //get the current db freshly        
        if (this.dbPath==""){
            logDb=XSPUtil.getDatabase();
        }else{
            logDb=XSPUtil.getDatabase(this.dbPath);
        }
        super.log(level, message);
    }
}
BeanLogger里不寻常的一点是在重载的log方法里,获取全新的日志数据库实例,这是因为XPages引擎会频繁地自动清理 Java里Lotus Domino对象用到的后端对象(参看42. Lotus Notes中的垃圾回收之Java),BeanLogger用到的日志数据库和文档很快会变成无效状态,调用它会引发以下异常:
NotesException: Object has been removed or recycled
at lotus.domino.local.NotesBase.CheckObject(Unknown Source)
at lotus.domino.local.Document.save(Unknown Source)
at lotus.domino.local.Document.save(Unknown Source)