本文整理匯總了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());
}
示例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
)
);
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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 "";
}
}
示例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;
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例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);
}
}
示例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});
}
示例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);
}
}