下面列出了java.util.logging.LogRecord#getSourceMethodName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public String format(final LogRecord record) {
final StringBuilder builder = new StringBuilder();
EcsJsonSerializer.serializeObjectStart(builder, record.getMillis());
EcsJsonSerializer.serializeLogLevel(builder, record.getLevel().getName());
EcsJsonSerializer.serializeFormattedMessage(builder, record.getMessage());
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadId(builder, record.getThreadID());
EcsJsonSerializer.serializeLoggerName(builder, record.getLoggerName());
if (includeOrigin && record.getSourceClassName() != null && record.getSourceMethodName() != null) {
EcsJsonSerializer.serializeOrigin(builder, buildFileName(record.getSourceClassName()), record.getSourceMethodName(), -1);
}
final Throwable throwableInformation = record.getThrown();
if (throwableInformation != null) {
EcsJsonSerializer.serializeException(builder, throwableInformation, stackTraceAsArray);
}
EcsJsonSerializer.serializeObjectEnd(builder);
return builder.toString();
}
/**
* Accumulates log records which will be used to produce the final output.
* The output is generated using the {@link #getTail} method which also
* resets this formatter back to its original state.
*
* @param record the record to store.
* @return an empty string.
* @throws NullPointerException if the given record is null.
*/
@Override
public String format(final LogRecord record) {
if (record == null) {
throw new NullPointerException();
}
boolean accepted;
do {
final LogRecord peek = peek();
//The self compare of the first record acts like a type check.
LogRecord update = apply(peek != null ? peek : record, record);
if (peek != update) { //Not identical.
update.getSourceMethodName(); //Infer caller, null check.
accepted = acceptAndUpdate(peek, update);
} else {
accepted = accept(peek, record);
}
} while (!accepted);
return "";
}
protected void appendThreadAndCaller(LogRecord record, StringBuffer sb) {
if (showThread || showCaller) {
sb.append(" [");
if (showThread)
sb.append(getThreadName(record));
if (showThread && showCaller) sb.append(", ");
if (showCaller) {
if (record.getSourceClassName() != null) {
sb.append(record.getSourceClassName());
} else {
sb.append(record.getLoggerName());
}
if (record.getSourceMethodName() != null) {
sb.append(" ");
sb.append(record.getSourceMethodName());
}
}
sb.append("]");
}
}
@Override
public synchronized String formatMessage(LogRecord record) {
StringBuilder sb = new StringBuilder();
if (record.getSourceClassName() != null) {
sb.append(record.getSourceClassName());
} else {
sb.append(record.getLoggerName());
}
if (record.getSourceMethodName() != null) {
sb.append(' ');
sb.append(record.getSourceMethodName());
}
sb.append(": ");
sb.append(super.formatMessage(record));
Throwable thrown = record.getThrown();
if (thrown != null) {
StringWriter sw = new StringWriter();
try (PrintWriter pw = new PrintWriter(sw);) {
sb.append("\n");
thrown.printStackTrace(pw);
}
sb.append(sw.getBuffer());
}
return sb.toString();
}
@Override
public void publish(LogRecord record) {
if (!isLoggable(record)) {
return;
}
// fill source entries, before we hand the record over to another
// thread with another class loader
record.getSourceMethodName();
LogEntry entry = new LogEntry(record, this);
boolean added = false;
try {
while (!added && !queue.offer(entry)) {
switch (OVERFLOW_DROP_TYPE) {
case OVERFLOW_DROP_LAST: {
//remove the last added element
queue.pollLast();
break;
}
case OVERFLOW_DROP_FIRST: {
//remove the first element in the queue
queue.pollFirst();
break;
}
case OVERFLOW_DROP_FLUSH: {
added = queue.offer(entry, 1000, TimeUnit.MILLISECONDS);
break;
}
case OVERFLOW_DROP_CURRENT: {
added = true;
break;
}
}//switch
}//while
} catch (InterruptedException x) {
// Allow thread to be interrupted and back out of the publish
// operation. No further action required.
}
}
@Override
public void publish(LogRecord record) {
String msg = record.getMessage();
String method = record.getSourceMethodName();
String className = record.getSourceClassName();
if (msg.contains(illegal)) {
testFailed = true;
}
if (msg.contains("attribute names=")) {
System.err.println("LOG: " + className + "." + method + ": " + msg);
}
}
/**
* Format the given LogRecord.
*
* @param record
* the log record to be formatted.
* @return a formatted log record
*/
public synchronized String format(LogRecord record) {
StringBuffer sb = new StringBuffer();
// Minimize memory allocations here.
Timestamp ts = new Timestamp(record.getMillis());
String text = ts.toString();
sb.append("JUL ");
sb.append(text);
sb.append(" ");
if (record.getSourceClassName() != null) {
sb.append(record.getSourceClassName());
} else {
sb.append(record.getLoggerName());
}
if (record.getSourceMethodName() != null) {
sb.append(" ");
sb.append(record.getSourceMethodName());
}
sb.append(lineSeparator);
String message = formatMessage(record);
sb.append(record.getLevel().getLocalizedName());
sb.append(": ");
sb.append(message);
sb.append(lineSeparator);
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) {
//do nothing?
}
}
return sb.toString();
}
@Override
public String format(final LogRecord record) {
final Date date = this.date.get();
date.setTime(record.getMillis());
String source;
final String sourceClassName = record.getSourceClassName();
final String sourceMethodName = record.getSourceMethodName();
if (sourceClassName != null) {
source = sourceClassName;
if (sourceMethodName != null) {
source += " " + sourceMethodName;
}
} else {
source = record.getLoggerName();
}
final String message = formatMessage(record);
String throwable = "";
final Throwable thrown = record.getThrown();
if (thrown != null) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
pw.println();
thrown.printStackTrace(pw);
pw.close();
throwable = sw.toString();
}
return String.format(
locale, format,
date, source,
record.getLoggerName(),
Locale.ENGLISH == locale ? record.getLevel().getName() : record.getLevel().getLocalizedName(),
message, throwable,
sourceClassName == null ? source : sourceClassName,
sourceMethodName == null ? source : sourceMethodName);
}
/**
* Formats the source from the given log record.
*
* @param record the record.
* @return the formatted source of the log record.
* @throws NullPointerException if the given record is null.
*/
public String formatSource(final LogRecord record) {
String source = record.getSourceClassName();
if (source != null) {
if (record.getSourceMethodName() != null) {
source = simpleClassName(source) + " "
+ record.getSourceMethodName();
} else {
source = simpleClassName(source);
}
} else {
source = simpleClassName(record.getLoggerName());
}
return source;
}
@Override
public void publish(LogRecord record) {
String msg = record.getMessage();
String method = record.getSourceMethodName();
String className = record.getSourceClassName();
if (msg.contains(illegal)) {
testFailed = true;
}
if (msg.contains("attribute names=")) {
System.err.println("LOG: " + className + "." + method + ": " + msg);
}
}
@Override
public void publish(LogRecord record) {
String msg = record.getMessage();
String method = record.getSourceMethodName();
String className = record.getSourceClassName();
if (msg.contains(illegal)) {
testFailed = true;
}
if (msg.contains("attribute names=")) {
System.err.println("LOG: " + className + "." + method + ": " + msg);
}
}
@Override
public void publish(LogRecord record) {
String msg = record.getMessage();
String method = record.getSourceMethodName();
String className = record.getSourceClassName();
if (msg.contains(illegal)) {
testFailed = true;
}
if (msg.contains("attribute names=")) {
System.err.println("LOG: " + className + "." + method + ": " + msg);
}
}
@Override
public String format(LogRecord record) {
dat.setTime(record.getMillis());
String source;
if (record.getSourceClassName() != null) {
source = record.getSourceClassName();
if (record.getSourceMethodName() != null) {
source += " " + record.getSourceMethodName();
}
} else {
source = record.getLoggerName();
}
String message = formatMessage(record);
String throwable = "";
if (record.getThrown() != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println();
record.getThrown().printStackTrace(pw);
pw.close();
throwable = sw.toString();
}
return String.format(FORMAT,
dat,
source,
record.getLoggerName(),
record.getLevel().getLocalizedName(),
message,
throwable,
componentName,
this.getThreadName(record.getThreadID())
);
}
@Override
public String format(LogRecord record) {
dat.setTime(record.getMillis());
String source;
if (record.getSourceClassName() != null) {
source = record.getSourceClassName().substring(record.getSourceClassName().lastIndexOf('.') + 1);
if (record.getSourceMethodName() != null) {
source += "." + record.getSourceMethodName();
}
} else {
source = record.getLoggerName();
}
String message = formatMessage(record);
String throwable = "";
if (record.getThrown() != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println();
record.getThrown().printStackTrace(pw);
pw.close();
throwable = sw.toString();
}
return String.format(format,
dat,
source,
record.getLoggerName(),
record.getLevel(),
message,
throwable);
}
@Override
public void publish(LogRecord record) {
String msg = record.getMessage();
String method = record.getSourceMethodName();
String className = record.getSourceClassName();
if (msg.contains(illegal)) {
testFailed = true;
}
if (msg.contains("attribute names=")) {
System.err.println("LOG: " + className + "." + method + ": " + msg);
}
}
@Override
public void publish(LogRecord record) {
String msg = record.getMessage();
String method = record.getSourceMethodName();
String className = record.getSourceClassName();
if (msg.contains(illegal)) {
testFailed = true;
}
if (msg.contains("attribute names=")) {
System.err.println("LOG: " + className + "." + method + ": " + msg);
}
}
private String source(LogRecord record) {
return record.getSourceClassName() != null
? record.getSourceClassName().replaceAll("\\B\\w+(\\.[a-z])", "$1") + "." + record.getSourceMethodName() + "()"
: record.getLoggerName();
}
@Override
public void publish(LogRecord record) {
record.getSourceClassName(); record.getSourceMethodName();
records.add(record);
}
public static void main(String[] args) {
Logger logger = Logger.getLogger("some.logger");
TestHandler handler = new TestHandler();
logger.setUseParentHandlers(false);
handler.setLevel(Level.ALL);
logger.setLevel(Level.FINEST);
logger.addHandler(handler);
// Auto-detect the line termination used by SimpleFormatter - (CR vs CRLF)
logger.entering("test", "test");
final String test = handler.bytes.toString();
System.out.println(test);
final String lineEnd = test.substring(test.indexOf("ENTRY")+"ENTRY".length());
System.out.print("Line termination is " + lineEnd.length() + " char(s) long:");
for (int i=0; i<lineEnd.length(); i++) {
System.out.print(" code="+(int)lineEnd.charAt(i));
}
System.out.println();
handler.reset();
for (int i=0 ; i<=data.length; i++) {
final StringBuilder b = new StringBuilder("ENTRY");
final StringBuilder f = new StringBuilder("ENTRY");
final Object[] params = new Object[i];
for (int k=0; k<i; k++) {
params[k] = data[k];
b.append(' ').append(String.valueOf(params[k]));
f.append(" {").append(String.valueOf(k)).append('}');
}
final String expected = b.toString();
final String format = f.toString();
final String className = "class"+i;
final String methodName = "method"+i;
logger.entering(className, methodName, params);
final String logged = handler.bytes.toString();
System.out.println("got:\n" + logged);
if (!logged.contains(className)) {
throw new RuntimeException("Marker for " + className
+ " not found."
+ "\n\tgot: <<\n" + logged + "\t >>");
}
if (!logged.contains(methodName)) {
throw new RuntimeException("Marker for " + methodName
+ " not found."
+ "\n\tgot: <<\n" + logged + "\t >>");
}
if (!logged.contains(expected)) {
throw new RuntimeException("Marker for parameters[size="
+ i + "] not found"
+ "\n\tgot: <<\n" + logged + "\t >>");
}
if (!logged.contains(expected+lineEnd)) {
throw new RuntimeException("Marker for parameters[size="
+ i + "] has extra characters"
+ "\n\tgot: <<\n" + logged + "\t >>");
}
LogRecord record = handler.events.remove(0);
if (!handler.events.isEmpty()) {
throw new RuntimeException("Handler has more log records: "
+ handler.events.toString());
}
if (!className.equals(record.getSourceClassName())) {
throw new RuntimeException("Unexpected class name in LogRecord."
+ "\n\texpected: " + className
+ "\n\tgot: " + record.getSourceClassName());
}
if (!methodName.equals(record.getSourceMethodName())) {
throw new RuntimeException("Unexpected method name in LogRecord."
+ "\n\texpected: " + methodName
+ "\n\tgot: " + record.getSourceMethodName());
}
if (!format.equals(record.getMessage())) {
throw new RuntimeException("Unexpected message format in LogRecord."
+ "\n\texpected: " + format
+ "\n\tgot: " + record.getMessage());
}
if (!Arrays.deepEquals(params, record.getParameters())) {
throw new RuntimeException("Unexpected parameter array in LogRecord."
+ "\n\texpected: " + Arrays.toString(params)
+ "\n\tgot: " + Arrays.toString(record.getParameters()));
}
handler.reset();
}
}
public synchronized String format(LogRecord record) {
// if record is null, return an empty String
if (record == null) {
return "";
}
StringBuffer buffer = new StringBuffer(100);
// create timestamp
Date timestamp = new Date(record.getMillis());
String timestampStr = tsFormatter.format(timestamp);
// append timestamp to the output string
buffer.append(timestampStr);
// append source thread ID
buffer.append(" - ");
buffer.append(record.getThreadID());
// append source class if logrb() method was used, otherwise append logger name
buffer.append(": ");
if (record.getSourceClassName() == null || record.getSourceClassName().equals("")) {
buffer.append(record.getLoggerName());
} else {
buffer.append(record.getSourceClassName());
}
// append source method if logrb() was used
if (record.getSourceMethodName() != null) {
buffer.append('.');
buffer.append(record.getSourceMethodName());
}
// append message level
buffer.append(": ");
buffer.append(record.getLevel().getLocalizedName());
// append message
buffer.append(": ");
buffer.append(record.getMessage());
// append exception if avaialble
if (record.getThrown() != null) {
buffer.append(CRLF);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
record.getThrown().printStackTrace(printWriter);
printWriter.close();
buffer.append(stringWriter.toString());
}
// append new line at the end
buffer.append(CRLF);
// return log entry
return buffer.toString();
}