예제 #1
0
  /**
   * Publish
   *
   * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
   * @param record log record
   */
  public void publish(LogRecord record) {
    checkContext();

    LinkedList<LogRecord> m_logs = (LinkedList<LogRecord>) Env.getCtx().get(LOGS_KEY);
    if (!isLoggable(record) || m_logs == null) return;

    //	Output
    synchronized (m_logs) {
      if (m_logs.size() >= LOG_SIZE) m_logs.removeFirst();
      m_logs.add(record);
    }

    //	We have an error
    if (record.getLevel() == Level.SEVERE) {
      LinkedList<LogRecord> m_errors = (LinkedList<LogRecord>) Env.getCtx().get(ERRORS_KEY);
      LinkedList<LogRecord[]> m_history = (LinkedList<LogRecord[]>) Env.getCtx().get(HISTORY_KEY);
      if (m_errors.size() >= ERROR_SIZE) {
        m_errors.removeFirst();
        m_history.removeFirst();
      }
      //	Add Error
      m_errors.add(record);
      record.getSourceClassName(); // 	forces Class Name eval

      //	Create History
      ArrayList<LogRecord> history = new ArrayList<LogRecord>();
      for (int i = m_logs.size() - 1; i >= 0; i--) {
        LogRecord rec = (LogRecord) m_logs.get(i);
        if (rec.getLevel() == Level.SEVERE) {
          if (history.size() == 0) history.add(rec);
          else break; // 	don't include previous error
        } else {
          history.add(rec);
          if (history.size() > 10) break; // 	no more then 10 history records
        }
      }
      LogRecord[] historyArray = new LogRecord[history.size()];
      int no = 0;
      for (int i = history.size() - 1; i >= 0; i--) historyArray[no++] = (LogRecord) history.get(i);
      m_history.add(historyArray);
      //	Issue Reporting
      if (isIssueError()) {
        String loggerName = record.getLoggerName(); // 	class name
        if (loggerName == null) loggerName = "";
        String className = record.getSourceClassName(); // 	physical class
        String methodName = record.getSourceMethodName(); //
        if (DB.isConnected(false)
            && !methodName.equals("saveError")
            && !methodName.equals("get_Value")
            && !methodName.equals("dataSave")
            && loggerName.indexOf("Issue") == -1
            && loggerName.indexOf("CConnection") == -1) {
          setIssueError(false);
          try {
            MIssue.create(record);
            setIssueError(true);
          } catch (Throwable e) {
            // failed to save exception to db, print to console
            System.err.println(getFormatter().format(record));
            setIssueError(false);
          }
        } else {
          // display to user if database connection not available
          if (!methodName.equals("saveError")
              && !methodName.equals("get_Value")
              && !methodName.equals("dataSave")
              && loggerName.indexOf("Issue") == -1
              && loggerName.indexOf("CConnection") == -1) {
            System.err.println(getFormatter().format(record));
          }
        }
      }
    }
  } // publish