69. XPages里的Java日志器(二)
ivate String logName;
protected String dbPath;
protected Database logDb;
private Document logDoc;
private RichTextItem logItem;
private boolean written = false;
/**
* @param db A NotesDatabase used for logging
* @throws NotesException
*/
public NotesLogger(Database db) throws NotesException{
logLevel = LEVEL_DEBUG;
logDb=db;
if (!logDb.isOpen()){
if (!logDb.open()){
throw new NotesException(NotesError.NOTES_ERR_DATABASE_NOTOPEN, "Cannot open the log database.");
}
}
}
/**
* @param s notes session
* @param dbPath path of the log database
* @throws NotesException
*/
public NotesLogger(Session s, String dbPath) throws NotesException {
//Database db=s.getDatabase(null, dbPath);
this(s.getDatabase(null, dbPath));
}
/**
* @param s notes session
* @throws NotesException
*/
public NotesLogger(Session s) throws NotesException{
this(s.getCurrentDatabase());
}
public void setLogLevel(int level) {
logLevel = level;
}
public void setLogName(String name) {
logName = name;
}
public void info(Object message) throws NotesException {
log(LEVEL_INFO, message);
}
public void warn(Object message) throws NotesException {
log(LEVEL_WARN, message);
}
public void debug(Object message) throws NotesException {
log(LEVEL_DEBUG, message);
}
public void error(Object message) throws NotesException {
log(LEVEL_ERROR, message);
}
public void fatal(Object message) throws NotesException {
log(LEVEL_FATAL, message);
}
public void log (int level, Object message) throws NotesException {
if (level>logLevel) return;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss aa", Locale.US);
String text = new String();
Date theCurrentDate = new Date();
Stri
ng theDate = dateFormat.format(theCurrentDate);
String theLevel = new String();
theLevel = theLevel+getLevelString(level);
//System.out.println("Level :"+level+" - logLevel "+logLevel);
text = theDate+" ["+theLevel+"]: "+message;
if (logDoc == null) {
// create document and a RTF for the log
logDoc = logDb.createDocument();
logDoc.replaceItemValue("Form", "log");
logDoc.replaceItemValue("LogName", logName);
if (logDb.getParent().isOnServer()){
logDoc.replaceItemValue("ScriptRunOn", "Server");
}else{
logDoc.replaceItemValue("ScriptRunOn", "Local");
}
logItem = logDoc.createRichTextItem("logBody");
}
// write log entry
logItem.appendText(text);
logItem.addNewLine();
written = true;
}
//convert from level numbers into strings
private String getLevelString(int level) {
String levelString="";
switch (level) {
case LEVEL_DEBUG: levelString=LEVEL_DEBUG_STRING ; break;
case LEVEL_INFO: levelString=LEVEL_INFO_STRING ; break;
case LEVEL_WARN: levelString=LEVEL_WARN_STRING ; break;
case LEVEL_ERROR: levelString=LEVEL_ERROR_STRING ; break;
case LEVEL_FATAL: levelString=LEVEL_FATAL_STRING ; break;
default : levelString=levelString+"LEVEL "+level; break;
} // end switch
return levelString;
}
/**
* save the log document
* @throws NotesException
*/
public void flush() throws NotesException{
if (written) {
logDoc.save();
written=false;
}
}
/**
* close the log via recycling the underlying resource.
* @throws NotesException
*/
public void close() throws NotesException {
flush();
logDoc.recycle();
logDoc=null;
if (this.dbPath!=""){
//If the log db is not the current one, rec