Java Code Examples for java.util.logging.LogRecord#getSourceMethodName()

The following examples show how to use java.util.logging.LogRecord#getSourceMethodName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EcsFormatter.java    From ecs-logging-java with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source File: CollectorFormatter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 "";
}
 
Example 3
Source File: JsonFormatter.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: SimpleOneLineLogFormatter.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
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("]");
    }
}
 
Example 5
Source File: TestLogHandler.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: AsyncFileHandler.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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.
    }

}
 
Example 7
Source File: TestLogHandler.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: LogFormatter.java    From cplus-libparser with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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);
}
 
Example 9
Source File: Twister2LogFormatter.java    From twister2 with Apache License 2.0 5 votes vote down vote up
@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())
  );
}
 
Example 10
Source File: TestLogHandler.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 11
Source File: TestLogHandler.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: TestLogHandler.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 13
Source File: CompactFormatter.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: LocalFileHandler.java    From tomee with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: MyLogFormatter.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 16
Source File: TestLogHandler.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 17
Source File: LoggerFinderBackendTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void publish(LogRecord record) {
    record.getSourceClassName(); record.getSourceMethodName();
    records.add(record);
}
 
Example 18
Source File: LoggerEnteringWithParams.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 19
Source File: UIMALogFormatter.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
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();
}
 
Example 20
Source File: SingleLineFormatter.java    From openAGV with Apache License 2.0 4 votes vote down vote up
private String source(LogRecord record) {
  return record.getSourceClassName() != null
      ? record.getSourceClassName().replaceAll("\\B\\w+(\\.[a-z])", "$1") + "." + record.getSourceMethodName() + "()"
      : record.getLoggerName();
}