当前位置: 首页>>代码示例>>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;未经允许,请勿转载。