當前位置: 首頁>>代碼示例>>Java>>正文


Java Formatter.format方法代碼示例

本文整理匯總了Java中java.util.logging.Formatter.format方法的典型用法代碼示例。如果您正苦於以下問題:Java Formatter.format方法的具體用法?Java Formatter.format怎麽用?Java Formatter.format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.logging.Formatter的用法示例。


在下文中一共展示了Formatter.format方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testUnknownLevels

import java.util.logging.Formatter; //導入方法依賴的package包/類
public void testUnknownLevels() throws IOException {
    TestLevel level = new TestLevel("WARN", 233);
    LogRecord r = new LogRecord(level, "Custom level test");
    Formatter formatter = new LogFormatter();
    String s = formatter.format(r);
    cleanKnownLevels();
    final LogRecord[] rPtr = new LogRecord[] { null };
    Handler h = new Handler() {
        @Override
        public void publish(LogRecord record) {
            rPtr[0] = record;
        }
        @Override
        public void flush() {}
        @Override
        public void close() throws SecurityException {}
    };
    LogRecords.scan(new ReaderInputStream(new StringReader(s)), h);
    assertEquals("level", r.getLevel(), rPtr[0].getLevel());
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:21,代碼來源:LogFormatterTest.java

示例2: publish

import java.util.logging.Formatter; //導入方法依賴的package包/類
@Override
public void publish(LogRecord record) {
	Formatter actualFormatter = formatter;
	if (actualFormatter != null) {
		String message = actualFormatter.format(record); 
		notification.sendNotification(
			new JMXLogRecordNotification(
				objectName,
				sequenceNumber++, 
				record.getMillis(),
				message,
				record
			)
		);
	}
}
 
開發者ID:kefik,項目名稱:Pogamut3,代碼行數:17,代碼來源:JMXLogPublisher.java

示例3: formatNullThrown

import java.util.logging.Formatter; //導入方法依賴的package包/類
@Test
public void formatNullThrown() throws Exception {
    final String previousLineSeparatorProperty = System.getProperty(LINE_SEPARATOR_KEY);
    try {
        final String lineSeparatorValue = "\n";
        final String logMessage = "An example log record";
        final Level level = Level.FINEST;

        System.setProperty(LINE_SEPARATOR_KEY, lineSeparatorValue);
        final LogRecord logRecordInput = new LogRecord(level, logMessage);
        logRecordInput.setThrown(null);

        final Formatter formatter = new SimpleTomEEFormatter();
        final String actualFormatOutput = formatter.format(logRecordInput);

        final String expectedFormatOutput = level.getLocalizedName() + " - " + logMessage + "\n";

        assertEquals(expectedFormatOutput, actualFormatOutput);
    } finally {
        System.setProperty(LINE_SEPARATOR_KEY, previousLineSeparatorProperty);
    }
}
 
開發者ID:apache,項目名稱:tomee,代碼行數:23,代碼來源:SimpleTomEEFormatterTest.java

示例4: formatNotNullThrown

import java.util.logging.Formatter; //導入方法依賴的package包/類
@Test
public void formatNotNullThrown() throws Exception {
    final String previousLineSeparatorProperty = System.getProperty(LINE_SEPARATOR_KEY);

    try {
        final String lineSeparatorValue = "\n";
        final String logMessage = "An example log record";
        final Level level = Level.CONFIG;
        final String exceptionMessage = "An example exception";
        final Throwable thrown = new Exception(exceptionMessage);

        System.setProperty(LINE_SEPARATOR_KEY, lineSeparatorValue);
        final LogRecord logRecordInput = new LogRecord(level, logMessage);
        logRecordInput.setThrown(thrown);

        final Formatter formatter = new SimpleTomEEFormatter();
        final String actualFormatOutput = formatter.format(logRecordInput);

        final String expectedFormatOutput = level.getLocalizedName() + " - " + logMessage + lineSeparatorValue + ExceptionUtils.getStackTrace(thrown);

        assertEquals(expectedFormatOutput, actualFormatOutput);
    } finally {
        System.setProperty(LINE_SEPARATOR_KEY, previousLineSeparatorProperty);
    }
}
 
開發者ID:apache,項目名稱:tomee,代碼行數:26,代碼來源:SimpleTomEEFormatterTest.java

示例5: testFormatterDoesNotIncludeHashOnEditor

import java.util.logging.Formatter; //導入方法依賴的package包/類
public void testFormatterDoesNotIncludeHashOnEditor() throws ClassNotFoundException {
    LogRecord r = new LogRecord(Level.INFO, "EDIT");
    JEditorPane ep = new javax.swing.JEditorPane();
    ep.setName("SomeName");
    r.setParameters(new Object[] { ep });
    Formatter formatter = new LogFormatter();
    String s = formatter.format(r);
    assertEquals("No @\n" + s, -1, s.indexOf("@"));
    if (s.indexOf("SomeName") == -1) {
        fail("SomeName should be there:\n" + s);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:13,代碼來源:LogFormatterTest.java

示例6: format

import java.util.logging.Formatter; //導入方法依賴的package包/類
/**
 * Creates the formatted log record or reports a formatting error.
 * @param f the formatter.
 * @param r the log record.
 * @return the formatted string or an empty string.
 */
private String format(final Formatter f, final LogRecord r) {
    try {
        return f.format(r);
    } catch (final RuntimeException RE) {
        reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
        return "";
    }
}
 
開發者ID:konradrenner,項目名稱:kore-javamail,代碼行數:15,代碼來源:MailHandler.java

示例7: createFormattedMessage

import java.util.logging.Formatter; //導入方法依賴的package包/類
private String createFormattedMessage(final ExtLogRecord record) {
  final Formatter formatter = getFormatter();
  try {
    return formatter.format(record);
  } catch (Exception e) {
    reportError("Could not format message", e, ErrorManager.FORMAT_FAILURE);
    return null;
  }
}
 
開發者ID:kifj,項目名稱:wildfly-logstash,代碼行數:10,代碼來源:SocketHandler.java

示例8: testFormatterDoesNotIncludeHashOnButton

import java.util.logging.Formatter; //導入方法依賴的package包/類
public void testFormatterDoesNotIncludeHashOnButton() throws ClassNotFoundException {
    LogRecord r = new LogRecord(Level.INFO, "BUTTON");
    r.setParameters(new Object[] { new JButton("kuk") });
    Formatter formatter = new LogFormatter();
    String s = formatter.format(r);
    assertEquals("No @\n" + s, -1, s.indexOf("@"));
    if (s.indexOf("kuk") == -1) {
        fail("kuk should be there:\n" + s);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:11,代碼來源:LogFormatterTest.java

示例9: testFormatterDoesNotIncludeHashOnActions

import java.util.logging.Formatter; //導入方法依賴的package包/類
public void testFormatterDoesNotIncludeHashOnActions() throws ClassNotFoundException {
    LogRecord r = new LogRecord(Level.INFO, "ACTION");
    SA sa = SA.get(SA.class);
    r.setParameters(new Object[] { sa });
    Formatter formatter = new LogFormatter();
    String s = formatter.format(r);
    assertEquals("No @\n" + s, -1, s.indexOf("@"));
    if (s.indexOf("SomeName") == -1) {
        fail("SomeName should be there:\n" + s);
    }
    if (s.indexOf("LogFormatterTest$SA") == -1) {
        fail("LogFormatterTest$SA should be there:\n" + s);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:15,代碼來源:LogFormatterTest.java

示例10: testFormatterDoesNotIncludeHashOnMenu

import java.util.logging.Formatter; //導入方法依賴的package包/類
public void testFormatterDoesNotIncludeHashOnMenu() throws ClassNotFoundException {
    LogRecord r = new LogRecord(Level.INFO, "MENU");
    SA sa = SA.get(SA.class);
    r.setParameters(new Object[] { new JMenuItem(sa) });
    Formatter formatter = new LogFormatter();
    String s = formatter.format(r);
    assertEquals("No @\n" + s, -1, s.indexOf("@"));
    if (s.indexOf("SomeName") == -1) {
        fail("SomeName should be there:\n" + s);
    }
    if (s.indexOf("LogFormatterTest$SA") == -1) {
        fail("LogFormatterTest$SA should be there:\n" + s);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:15,代碼來源:LogFormatterTest.java

示例11: format

import java.util.logging.Formatter; //導入方法依賴的package包/類
protected String format(LogRecord record)
{
  if (! isLoggable(record)) {
    return null;
  }
  
  StringBuilder sb = new StringBuilder();
  
  if (record == null) {
    sb.append("no record");
        
    if (isNullDelimited()) {
      sb.append('\0');
    }

    return sb.toString();
  }

  try {
    Formatter formatter = getFormatter();
    
    if (formatter != null) {
      String value = formatter.format(record);

      sb.append(value).append('\n');
      
      if (isNullDelimited()) {
        sb.append('\0');
      }
        
      return sb.toString();
    }
      
    String message = record.getMessage();
    Throwable thrown = record.getThrown();
      
    if (thrown == null
        && message != null
        && message.indexOf("java.lang.NullPointerException") >= 0) {
      thrown = new IllegalStateException();
      thrown.fillInStackTrace();
    }
          
    if (thrown != null) {
      if (message != null
          && ! message.equals(thrown.toString()) 
          && ! message.equals(thrown.getMessage())) {
        printMessage(sb, message, record.getParameters());
      }
        
      StringWriter writer = new StringWriter();
      PrintWriter pw = new PrintWriter(writer);
      thrown.printStackTrace(pw);
      pw.close();
      
      sb.append(writer.toString());
    }
    else {
      printMessage(sb, message, record.getParameters());
    }

    /*
    TimestampFilter timestamp = getTimestampFilter();
    if (timestamp != null) {
      sb = timestamp.format(sb);
    }
    */
      
    if (isNullDelimited()) {
      sb.append('\0');
    }
  } catch (Throwable e) {
    e.printStackTrace();
  }

  return sb.toString();
}
 
開發者ID:baratine,項目名稱:baratine,代碼行數:78,代碼來源:LogHandlerBase.java

示例12: publish

import java.util.logging.Formatter; //導入方法依賴的package包/類
/**
 * Publish a log record to the logging pane.
 * 
 * @param record
 *        Log record
 * @throws Exception
 */
protected synchronized void publish(LogRecord record) throws BadLocationException {
  // choose an appropriate formatter
  final Formatter fmt;
  final Style style;
  // always format progress messages using the progress formatter.
  if(record.getLevel().intValue() >= Level.WARNING.intValue()) {
    // format errors using the error formatter
    fmt = errformat;
    style = errStyle;
  }
  else if(record.getLevel().intValue() <= Level.FINE.intValue()) {
    // format debug statements using the debug formatter.
    fmt = debugformat;
    style = dbgStyle;
  }
  else {
    // default to the message formatter.
    fmt = msgformat;
    style = msgStyle;
  }
  // format
  final String m;
  m = fmt.format(record);
  StyledDocument doc = getStyledDocument();
  if(record instanceof ProgressLogRecord) {
    if(lastNewlinePos < doc.getLength()) {
      doc.remove(lastNewlinePos, doc.getLength() - lastNewlinePos);
    }
  }
  else {
    // insert a newline, if we didn't see one yet.
    if(lastNewlinePos < doc.getLength()) {
      doc.insertString(doc.getLength(), "\n", style);
      lastNewlinePos = doc.getLength();
    }
  }
  int tail = tailingNonNewline(m, 0, m.length());
  int headlen = m.length() - tail;
  if(headlen > 0) {
    String pre = m.substring(0, headlen);
    doc.insertString(doc.getLength(), pre, style);
  }
  lastNewlinePos = doc.getLength();
  if(tail > 0) {
    String post = m.substring(m.length() - tail);
    doc.insertString(lastNewlinePos, post, style);
  }
}
 
開發者ID:elki-project,項目名稱:elki,代碼行數:56,代碼來源:LogPane.java

示例13: formatRecord

import java.util.logging.Formatter; //導入方法依賴的package包/類
/**
 * Formats the given record with the head and tail.
 *
 * @param h the Handler or null.
 * @param reset true if the summary statistics and LogRecord should be reset
 * back to initial values.
 * @return the formatted string.
 * @see #getTail(java.util.logging.Handler)
 */
private String formatRecord(final Handler h, final boolean reset) {
    final LogRecord record;
    final long c;
    final long t;
    long msl;
    long msh;
    synchronized (this) {
        record = last;
        c = count;
        t = thrown;
        msl = minMillis;
        msh = maxMillis;

        if (reset) { //BUG ID 6351685
            reset();
        }
    }

    if (c == 0L) {  //Use the estimated lifespan of this class.
        msl = INIT_TIME;
        msh = System.currentTimeMillis();
    }

    final String head;
    final String msg;
    final String tail;
    final Formatter f = this.formatter;
    if (f != null) {
        synchronized (f) {
            head = f.getHead(h);
            msg = record != null ? f.format(record) : "";
            tail = f.getTail(h);
        }
    } else {
        head = msg = tail = "";
    }

    Locale l = null;
    if (record != null) {
        ResourceBundle rb = record.getResourceBundle();
        l = rb == null ? null : rb.getLocale();
    }

    final MessageFormat mf;
    if (l == null) { //BUG ID 8039165
        mf = new MessageFormat(fmt);
    } else {
        mf = new MessageFormat(fmt, l);
    }

    /**
     * These arguments are described in the getTail documentation.
     */
    return mf.format(new Object[]{finish(head), finish(msg), finish(tail),
        c, (c - 1), t, (c - t), msl, msh});
}
 
開發者ID:konradrenner,項目名稱:kore-javamail,代碼行數:66,代碼來源:CollectorFormatter.java

示例14: testFormatterDoesNotIncludeHashOnActionsClone

import java.util.logging.Formatter; //導入方法依賴的package包/類
public void testFormatterDoesNotIncludeHashOnActionsClone() throws ClassNotFoundException {
    LogRecord r = new LogRecord(Level.INFO, "ACTION_CLONE");
    SA sa = SA.get(SA.class);
    r.setParameters(new Object[] { sa.createContextAwareInstance(Lookup.EMPTY) });
    Formatter formatter = new LogFormatter();
    String s = formatter.format(r);
    assertEquals("No @\n" + s, -1, s.indexOf("@"));
    if (s.indexOf("SomeName") == -1) {
        fail("SomeName should be there:\n" + s);
    }
    if (s.indexOf("LogFormatterTest$SA") == -1) {
        fail("LogFormatterTest$SA should be there:\n" + s);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:15,代碼來源:LogFormatterTest.java


注:本文中的java.util.logging.Formatter.format方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。