publicvoidlogMessage(int level, String message) { // if no ability to handle, hand up it to successor if (this.level == level) { write(message); } else { if (nextLogger != null) { nextLogger.logMessage(level, message); } } }
/** * @author mystic */ publicclassTester { publicstaticvoidmain(String[] args) { AbstractLoggerdebugLogger=newDebugLogger(); System.out.println("**************************"); debugLogger.logMessage(1, "Here is a debug message."); System.out.println("=========================="); debugLogger.logMessage(2, "Here is a info message."); System.out.println("=========================="); debugLogger.logMessage(3, "Here is a warn message."); System.out.println("=========================="); debugLogger.logMessage(4, "Here is a error message."); System.out.println("=========================="); } }
1 2 3 4 5 6 7 8 9
************************** DebugLogger.write: Here is a debug message. ========================== InfoLogger.write: Here is a info message. ========================== WarnLogger.write: Here is a warn message. ========================== ErrorLogger.write: Here is a error message. ==========================