package jp.co.imtt;

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

public class ImwareFormatter extends Formatter{
    private String format;
    public ImwareFormatter() {
        //format = LogManager.getLogManager().getProperty(this.getClass().getName()+".format");
      // ログフォーマット文字列
      // 1:日付 2:タイプ 3:クラス 4:メソッド 5:メッセージ   
      //format = "%1$tF %1$tT [%2$s] - %5$s %n";
      format = "%1$tF %1$tT\t[%2$s]\t%5$s %n";
    }

    @Override
    public String format(LogRecord record) {
        return String.format(format , 
                record.getMillis() ,
                log4jLevel(record.getLevel().getName()),
                record.getSourceClassName(),
                record.getSourceMethodName(),
                record.getMessage()
                );
    }
    
    private String log4jLevel(String loggingLevel){
      if("SEVERE".equals(loggingLevel)){
        return "ERROR";
      } else if("CONFIG".equals(loggingLevel)){
        return "DEBUG";
      } else if("WARNING".equals(loggingLevel)){
        return "WARN";
      } else {
        return loggingLevel;
      }
    }

}
