@Override
  public String format(LogRecord record) {

    int count = Thread.activeCount();

    Thread th[] = new Thread[count];
    // returns the number of threads put into the array
    Thread.enumerate(th);

    String thrName = null;

    // prints active threads
    for (int i = 0; i < count; i++) {

      if (th[i] != null && th[i].getId() == record.getThreadID()) {

        thrName = th[i].getName();
        break;
      }
    }

    if (thrName == null) {
      thrName = String.format("id:%d", record.getThreadID());
    }

    StringBuilder sb = new StringBuilder();

    sb.append(new Date(record.getMillis()))
        .append(" ")
        .append(record.getLevel().getLocalizedName())
        .append(" ")
        .append("[")
        .append(record.getSourceClassName())
        .append("]")
        .append("{")
        .append(thrName)
        .append("}: ")
        .append(formatMessage(record))
        .append(LINE_SEPARATOR);

    if (record.getThrown() != null) {
      try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        record.getThrown().printStackTrace(pw);
        pw.close();
        sb.append(sw.toString());
      } catch (Exception ex) {
        // ignore
      }
    }

    return "RT: " + sb.toString();
  }
 @Override
 public String format(LogRecord rec) {
   StringBuilder buf = new StringBuilder(1000);
   buf.append(rightPad(rec.getLevel().getName(), 7));
   buf.append(rightPad(calcDate(rec.getMillis()), 19));
   buf.append(rec.getThreadID());
   buf.append(":");
   buf.append(formatMessage(rec));
   buf.append("\n");
   return buf.toString();
 }
  @Override
  public String format(LogRecord record) {

    String stackTrace = "";
    Throwable throwable = record.getThrown();
    if (throwable != null) {
      StringWriter sw = new StringWriter();
      throwable.printStackTrace(new PrintWriter(sw));
      stackTrace = sw.toString();
    }

    int lineNo;
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    if (throwable != null && stack.length > 7) lineNo = stack[7].getLineNumber();
    else if (stack.length > 8) lineNo = stack[8].getLineNumber();
    else lineNo = -1;

    StringBuffer b = new StringBuffer();
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_THREAD_ID)) {
      b.append(String.format("%2d", record.getThreadID())).append(" ");
    }
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_LEVEL)) {
      b.append(record.getLevel().toString()).append(" ");
    }
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_CLASS)) {
      b.append("[").append(getSimpleClassName(record.getSourceClassName())).append("]: ");
    }
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_MESSAGE)) {
      b.append(formatMessage(record));
    }
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_SOURCE)) {
      b.append(" - (")
          .append(record.getSourceClassName())
          .append(".java:")
          .append(lineNo)
          .append(") ");
    }
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_METHOD)) {
      b.append(record.getSourceMethodName()).append("()");
    }
    if (logPolicy.isEnabled(LogPolicy.LOG_POLICY_SHOW_THREAD)) {
      b.append(" in thread ").append(Thread.currentThread().getName());
    }
    if (logPolicy.dateFormat != null) {
      b.append(" at (")
          .append(logPolicy.dateFormat.format(new Date(record.getMillis())))
          .append(")");
    }
    b.append("\n").append(stackTrace);
    return b.toString();
  }
Beispiel #4
0
  // This method is called for every log records
  public String format(LogRecord rec) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(byteArrayOutputStream);

    printStream.format(
        "%-7s %-24s %-80s  %-40s Seq:%d Thread:%d\n",
        rec.getLevel(),
        SIMPLE_DATE_FORMAT.format(new Date(rec.getMillis())),
        rec.getMessage(),
        rec.getSourceClassName() + "." + rec.getSourceMethodName(),
        rec.getSequenceNumber(),
        rec.getThreadID());

    return byteArrayOutputStream.toString();
  }
Beispiel #5
0
 @Override
 public void publish(final LogRecord record) {
   if (record.getLevel().intValue() < this.getLevel().intValue()
       || record.getLoggerName().equals(getClass().getName())) {
     return;
   }
   try {
     Mailer.instance()
         .send(
             (message) -> {
               message.addRecipient(
                   Message.RecipientType.TO,
                   new InternetAddress(
                       "clash.nanobot+log."
                           + record.getLevel().getName().toLowerCase()
                           + "@gmail.com"));
               message.setSubject(record.getLevel() + ": " + record.getMessage());
               String emailBody = "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">";
               emailBody +=
                   "<tr><td>Version</td><td>" + BuildInfo.instance().getVersion() + "</td></tr>";
               emailBody +=
                   "<tr><td>BuildTime</td><td>"
                       + BuildInfo.instance().getTimestamp()
                       + "</td></tr>";
               emailBody += "<tr><td>Time</td><td>" + record.getMillis() + "</td></tr>";
               emailBody += "<tr><td>Thread</td><td>" + record.getThreadID() + "</td></tr>";
               emailBody += "<tr><td>Logger</td><td>" + record.getLoggerName() + "</td></tr>";
               emailBody += "<tr><td>Class</td><td>" + record.getSourceClassName() + "</td></tr>";
               emailBody +=
                   "<tr><td>Method</td><td>" + record.getSourceMethodName() + "</td></tr>";
               emailBody += "<tr><td>Level</td><td>" + record.getLevel() + "</td></tr>";
               emailBody += "<tr><td>Message</td><td>" + record.getMessage() + "</td></tr>";
               final Throwable e = record.getThrown();
               if (e != null) {
                 final StringWriter writer = new StringWriter();
                 e.printStackTrace(new PrintWriter(writer));
                 emailBody +=
                     "<tr><td>Exception</td><td>"
                         + writer.toString().replace("\n", "<br/>")
                         + "</td></tr>";
               }
               emailBody += "</table>";
               message.setContent(emailBody, "text/html");
             });
   } catch (final Exception e) {
     logger.log(Level.SEVERE, e.getMessage(), e);
   }
 }
  public void publish(LogRecord record, int nLogType) {
    // first see if this entry should be filtered out
    try {
      if (record != null) {

        // MARKED by zhaoxy@20101119
        /*
           if (getFilter() != null) {
        	if (!getFilter().isLoggable(record))
        		return;
        }
        */

        if (isLoggable(record)) {
          String HmuImei = null;
          String Message = record.getMessage();
          try {
            int HmuImeiPos = Message.indexOf("HMU:");
            if (HmuImeiPos >= 0) {
              HmuImei = Message.substring(HmuImeiPos + 4);
              Message = Message.substring(0, HmuImeiPos);
            }
          } catch (Exception e) {
          }

          // now store the log entry into the table
          if (activeConnection()) {
            try {
              prepInsert.setInt(1, record.getLevel().intValue());
              prepInsert.setString(2, truncate(record.getLoggerName(), 63));
              prepInsert.setString(3, truncate(Message, 255));
              prepInsert.setLong(4, record.getSequenceNumber());
              prepInsert.setString(5, truncate(record.getSourceClassName(), 63));
              prepInsert.setString(6, truncate(record.getSourceMethodName(), 31));
              prepInsert.setInt(7, record.getThreadID());
              prepInsert.setTimestamp(8, new Timestamp(System.currentTimeMillis()));
              prepInsert.setString(9, truncate(HmuImei, 15));
              prepInsert.setInt(10, nLogType);
              prepInsert.executeUpdate();
            } catch (SQLException e) {
              System.err.println("Exception on publish(record,nLogType)." + e);
            }
          }
        }
      }
    } catch (Exception e) {
    }
  }
 @Override
 public String format(LogRecord rec) {
   StringWriter w1 = new StringWriter();
   PrintWriter w2 = new PrintWriter(w1);
   w2.printf(
       messageFormat,
       dateFormat.format(new Date(rec.getMillis())),
       rec.getThreadID(),
       String.valueOf(rec.getLevel()),
       fixClsName(rec.getSourceClassName()),
       fixMtdName(rec.getSourceMethodName()),
       rec.getMessage());
   w2.println();
   w2.close();
   return w1.toString();
 }
Beispiel #8
0
 @Override
 public String format(final LogRecord record) {
   final StringBuilder logEntry = new StringBuilder();
   final Calendar cal = Calendar.getInstance();
   cal.setTimeInMillis(record.getMillis());
   logEntry.append(DateHelper.formatDate(cal));
   logEntry.append(" [");
   logEntry.append(record.getThreadID());
   logEntry.append("] ");
   logEntry.append(record.getLevel().getName());
   logEntry.append(" ");
   logEntry.append(record.getSourceClassName());
   logEntry.append(" ");
   logEntry.append(record.getSourceMethodName());
   logEntry.append(" - ");
   logEntry.append(record.getMessage());
   logEntry.append("\n");
   return logEntry.toString();
 }
 @Override
 public String format(LogRecord logRecord) {
   Object[] arguments = new Object[6];
   arguments[0] = logRecord.getThreadID();
   String fullClassName = logRecord.getSourceClassName();
   int lastDot = fullClassName.lastIndexOf('.');
   String className = fullClassName.substring(lastDot + 1);
   arguments[1] = className;
   arguments[2] = logRecord.getSourceMethodName();
   arguments[3] = new Date(logRecord.getMillis());
   arguments[4] = logRecord.getMessage();
   if (logRecord.getThrown() != null) {
     Writer result = new StringWriter();
     logRecord.getThrown().printStackTrace(new PrintWriter(result));
     arguments[5] = result.toString();
   } else {
     arguments[5] = "";
   }
   return messageFormat.format(arguments);
 }
Beispiel #10
0
  /* (non-Javadoc)
   * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
   */
  @Override
  public String format(LogRecord record) {
    StringBuffer sb = new StringBuffer();
    sb.append("<tr>");

    sb.append("<td>");
    sb.append(dateToString(new Date(record.getMillis()), "yyyy/MM/dd"));
    sb.append("</td>");

    sb.append("<td>");
    sb.append(dateToString(new Date(record.getMillis()), "hh:mm:ss,SSS"));
    sb.append("</td>");

    sb.append("<td>");
    if (record.getParameters() != null) {
      sb.append(record.getParameters()[0]);
    } else {
      sb.append(record.getThreadID());
    }
    sb.append("</td>");

    sb.append("<td>");
    sb.append(record.getSourceClassName());
    sb.append("</td>");

    sb.append("<td>");
    sb.append(record.getSourceMethodName());
    sb.append("</td>");

    sb.append("<td>");
    sb.append(record.getLevel());
    sb.append("</td>");

    sb.append("<td>");
    sb.append(formatMessage(record));
    sb.append("</td>");

    sb.append("</tr>");

    return sb.toString();
  }
  @SuppressWarnings("unchecked")
  public String format(final LogRecord record) {
    // Reset working buffer. If the buffer is too large, then we need a new
    // one in order to avoid the penalty of creating a large array.
    if (buf.capacity() > UPPER_LIMIT) {
      buf = new StringBuffer(DEFAULT_SIZE);
    } else {
      buf.setLength(0);
    }
    buf.append("<log4j:event logger=\"");
    buf.append(Transform.escapeTags(record.getLoggerName()));
    buf.append("\" timestamp=\"");
    buf.append(record.getMillis());
    buf.append("\" level=\"");

    buf.append(Transform.escapeTags(record.getLevel().getName()));
    buf.append("\" thread=\"");
    buf.append(String.valueOf(record.getThreadID()));
    buf.append("\">\r\n");

    buf.append("<log4j:message><![CDATA[");
    // Append the rendered message. Also make sure to escape any
    // existing CDATA sections.
    Transform.appendEscapingCDATA(buf, record.getMessage());
    buf.append("]]></log4j:message>\r\n");

    if (record.getThrown() != null) {
      String[] s = Transform.getThrowableStrRep(record.getThrown());
      if (s != null) {
        buf.append("<log4j:throwable><![CDATA[");
        for (String value : s) {
          Transform.appendEscapingCDATA(buf, value);
          buf.append("\r\n");
        }
        buf.append("]]></log4j:throwable>\r\n");
      }
    }

    if (locationInfo) {
      buf.append("<log4j:locationInfo class=\"");
      buf.append(Transform.escapeTags(record.getSourceClassName()));
      buf.append("\" method=\"");
      buf.append(Transform.escapeTags(record.getSourceMethodName()));
      buf.append("\" file=\"?\" line=\"?\"/>\r\n");
    }

    if (properties) {
      if (MDC.getMDCAdapter() instanceof BasicMDCAdapter) {
        BasicMDCAdapter mdcAdapter = (BasicMDCAdapter) MDC.getMDCAdapter();
        Set keySet = mdcAdapter.getKeys();
        if (keySet != null && keySet.size() > 0) {
          buf.append("<log4j:properties>\r\n");
          Object[] keys = keySet.toArray();
          Arrays.sort(keys);
          for (Object key1 : keys) {
            String key = key1.toString();
            Object val = mdcAdapter.get(key);
            if (val != null) {
              buf.append("<log4j:data name=\"");
              buf.append(Transform.escapeTags(key));
              buf.append("\" value=\"");
              buf.append(Transform.escapeTags(String.valueOf(val)));
              buf.append("\"/>\r\n");
            }
          }
          buf.append("</log4j:properties>\r\n");
        }
      }
    }
    buf.append("</log4j:event>\r\n\r\n");

    return buf.toString();
  }