2013-02-03 2 views
0

다음 코드에서 MyCustomeFormatter 클래스는 Formatter까지 확장됩니다. 또한 format 메시지보다 우선합니다. 이 메서드는 언제 호출됩니까? 예 :형식 메서드는 언제 호출됩니까?

logger.log(Level.INFO,"This is an info message") 

문은 지정된 처리기에 메시지를 기록합니다. 그러나 overriden 메서드는 언제 호출됩니까? 핸들러가 StreamHandler에서 상속 경우

import java.util.Date; 
import java.util.logging.Formatter; 
import java.util.logging.LogRecord; 


public class MyCustomFormatter extends Formatter { 

public MyCustomFormatter() { 
    super(); 
} 

public String format(LogRecord record) { 

    // Create a StringBuffer to contain the formatted record 
    // start with the date. 
    StringBuffer sb = new StringBuffer(); 

    // Get the date from the LogRecord and add it to the buffer 
    Date date = new Date(record.getMillis()); 
    sb.append(date.toString()); 
    sb.append(" "); 

    // Get the level name and add it to the buffer 
    sb.append(record.getLevel().getName()); 
    sb.append(" "); 

    // Get the formatted message (includes localization 
    // and substitution of paramters) and add it to the buffer 
    sb.append(formatMessage(record)); 
    sb.append("\n"); 

    return sb.toString(); 
} 
} 

답변

0

Logger.log() 전화 Handler.publish()

:

public synchronized void publish(LogRecord record) { 
    if (!isLoggable(record)) { 
     return; 
    } 
    String msg; 
    try { 
     msg = getFormatter().format(record); 
    } catch (Exception ex) { 
     // We don't want to throw an exception here, but we 
     // report the exception to any registered ErrorManager. 
     reportError(null, ex, ErrorManager.FORMAT_FAILURE); 
     return; 
    } 

    try { 
     if (!doneHeader) { 
      writer.write(getFormatter().getHead(this)); 
      doneHeader = true; 
     } 
     writer.write(msg); 
    } catch (Exception ex) { 
     // We don't want to throw an exception here, but we 
     // report the exception to any registered ErrorManager. 
     reportError(null, ex, ErrorManager.WRITE_FAILURE); 
    } 
} 
관련 문제