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

The following examples show how to use java.util.logging.LogRecord#getThreadID() . 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: JulLogEvent.java    From xian with Apache License 2.0 6 votes vote down vote up
private String getThreadName(LogRecord record) {

        String cacheKey = "" + record.getThreadID();
        if (threadNameCache.containsKey(cacheKey)) {
            return threadNameCache.get(cacheKey);
        }

        long threadId = record.getThreadID();
        String threadName = "" + record.getThreadID();
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        for (long id : threadMXBean.getAllThreadIds()) {
            if (id == threadId) {
                ThreadInfo threadInfo = threadMXBean.getThreadInfo(id);
                if (threadInfo != null) {
                    threadName = threadInfo.getThreadName();
                }
                break;
            }
        }

        threadNameCache.put(cacheKey, threadName);

        return threadName;
    }
 
Example 2
Source File: EnvironmentSetup.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
@Override
public String format(final LogRecord record) {
    final ZonedDateTime zdt =
            ZonedDateTime.ofInstant(Instant.ofEpochMilli(record.getMillis()), zone).withZoneSameInstant(utc);
    final String date = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zdt);
    final String thread =
            record.getThreadID() == Thread.currentThread().getId() ? Thread.currentThread().getName()
                    : ("thread-" + record.getThreadID());
    final String base = '[' + date + "][" + thread + "][" + record.getLevel().getName() + "]["
            + record.getLoggerName() + "] " + record.getMessage() + System.lineSeparator();
    final String throwable;
    if (record.getThrown() != null) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        pw.println();
        record.getThrown().printStackTrace(pw);
        pw.close();
        throwable = sw.toString();
    } else {
        throwable = null;
    }
    return base + (throwable == null ? "" : throwable);
}
 
Example 3
Source File: ConsoleHandler.java    From buck with Apache License 2.0 6 votes vote down vote up
private boolean isLoggableWithRegisteredLogLevel(LogRecord record) {
  long recordThreadId = record.getThreadID();
  String logRecordCommandId = state.threadIdToCommandId(recordThreadId);
  if (logRecordCommandId == null) {
    // An unregistered thread created this LogRecord, so we don't want to force logging it.
    return false;
  }
  Level commandIdLogLevel = state.getLogLevel(logRecordCommandId);
  if (commandIdLogLevel == null) {
    // No log level override registered for this command ID. Don't force logging it.
    return false;
  }

  // Level.ALL.intValue() is Integer.MIN_VALUE, so have to compare it explicitly.
  return commandIdLogLevel.equals(Level.ALL)
      || commandIdLogLevel.intValue() >= record.getLevel().intValue();
}
 
Example 4
Source File: ConsoleHandler.java    From buck with Apache License 2.0 6 votes vote down vote up
private Iterable<ConsoleHandlerState.Writer> getOutputStreamWritersForRecord(LogRecord record) {
  long recordThreadId = record.getThreadID();
  String logRecordCommandId = state.threadIdToCommandId(recordThreadId);
  if (logRecordCommandId != null) {
    ConsoleHandlerState.Writer consoleWriter = state.getWriter(logRecordCommandId);
    if (consoleWriter != null) {
      return ImmutableSet.of(consoleWriter);
    } else {
      return ImmutableSet.of(defaultOutputStreamWriter);
    }
  } else {
    Iterable<ConsoleHandlerState.Writer> allConsoleWriters = state.getAllAvailableWriters();
    if (Iterables.isEmpty(allConsoleWriters)) {
      return ImmutableSet.of(defaultOutputStreamWriter);
    } else {
      ImmutableSet.Builder<ConsoleHandlerState.Writer> builder = ImmutableSet.builder();
      builder.addAll(allConsoleWriters);
      return builder.build();
    }
  }
}
 
Example 5
Source File: MyFormatter.java    From journaldev with MIT License 5 votes vote down vote up
@Override
public String format(LogRecord record) {
    return record.getThreadID()+"::"+record.getSourceClassName()+"::"
            +record.getSourceMethodName()+"::"
            +new Date(record.getMillis())+"::"
            +record.getMessage()+"\n";
}
 
Example 6
Source File: LogSetupTestCase.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    System.setProperty("config.id", "my-test-config-id");
    System.setProperty("vespa.log.target", "file:test3");

    zookeeperLogRecord = new LogRecord(Level.WARNING, "zookeeper log record");
    zookeeperLogRecord.setLoggerName("org.apache.zookeeper.server.NIOServerCnxn");
    zookeeperLogRecord.setInstant(Instant.ofEpochMilli(1107011348029L));

    curatorLogRecord = new LogRecord(Level.WARNING, "curator log record");
    curatorLogRecord.setLoggerName("org.apache.curator.utils.DefaultTracerDriver");
    curatorLogRecord.setInstant(Instant.ofEpochMilli(1107011348029L));

    hostname = Util.getHostName();
    pid = Util.getPID();

    zookeeperLogRecordString = "1107011348.029000\t"
            + hostname
            + "\t"
            + pid
            + "/" + zookeeperLogRecord.getThreadID() + "\t-\t.org.apache.zookeeper.server.NIOServerCnxn"
            + "\twarning\tzookeeper log record";

    zookeeperLogRecordError = new LogRecord(Level.SEVERE, "zookeeper error");
    zookeeperLogRecordError.setLoggerName("org.apache.zookeeper.server.NIOServerCnxn");
    zookeeperLogRecordError.setInstant(Instant.ofEpochMilli(1107011348029L));

    curatorLogRecordError = new LogRecord(Level.SEVERE, "curator log record");
    curatorLogRecordError.setLoggerName("org.apache.curator.utils.DefaultTracerDriver");
    curatorLogRecordError.setInstant(Instant.ofEpochMilli(1107011348029L));

    notzookeeperLogRecord = new LogRecord(Level.WARNING, "not zookeeper log record");
    notzookeeperLogRecord.setLoggerName("org.apache.foo.Bar");
    notzookeeperLogRecord.setInstant(Instant.ofEpochMilli(1107011348029L));
}
 
Example 7
Source File: SimpleOneLineLogFormatter.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected String getThreadName(LogRecord record) {
    //try to get the thread's name
    //only possible if we are the thread (unless do something messy like cache or access private fields)
    //fortunately we typically are the thread
    LogRecord lr = new LogRecord(Level.INFO, "");
    if (lr.getThreadID()==record.getThreadID())
        return Thread.currentThread().getName() + " ("+record.getThreadID()+")";
    //otherwise just say the number
    return "thread ("+record.getThreadID()+")";
}
 
Example 8
Source File: LogFormatter.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LogRecord record) {
  String timestamp = simpleDateFormat.get().format(new Date(record.getMillis()));

  // We explicitly don't use String.format here because this code is very
  // performance-critical: http://stackoverflow.com/a/1281651
  long tid = record.getThreadID();
  @Nullable String command = mapper.threadIdToCommandId(tid);
  StringBuilder sb =
      new StringBuilder(255)
          .append(timestamp)
          .append(formatRecordLevel(record.getLevel()))
          .append("[command:")
          .append(command)
          .append("][tid:");
  // Zero-pad on the left. We're currently assuming we have less than 100 threads.
  if (tid < 10) {
    sb.append("0").append(tid);
  } else {
    sb.append(tid);
  }
  sb.append("][").append(record.getLoggerName()).append("] ");
  if (record instanceof AppendableLogRecord) {
    // Avoid allocating then throwing away the formatted message and
    // params; just format directly to the StringBuilder.
    ((AppendableLogRecord) record).appendFormattedMessage(sb);
  } else {
    sb.append(formatMessage(record));
  }
  sb.append("\n");
  Throwable t = record.getThrown();
  if (t != null) {
    sb.append(Throwables.getStackTraceAsString(t)).append("\n");
  }
  return sb.toString();
}
 
Example 9
Source File: JulLogFormatter.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LogRecord record) {
  String timestamp = DATE_FORMAT.get().format(new Date(record.getMillis()));

  // We explicitly don't use String.format here because this code is very
  // performance-critical: http://stackoverflow.com/a/1281651
  long tid = record.getThreadID();
  StringBuilder sb =
      new StringBuilder(timestamp).append(formatRecordLevel(record.getLevel())).append("[tid:");
  // Zero-pad on the left. We're currently assuming we have less than 100 threads.
  if (tid < 10) {
    sb.append("0").append(tid);
  } else {
    sb.append(tid);
  }
  sb.append("][").append(record.getLoggerName()).append("] ");
  if (record instanceof AppendableLogRecord) {
    ((AppendableLogRecord) record).appendFormattedMessage(sb);
  } else {
    sb.append(formatMessage(record));
  }
  sb.append("\n");
  Throwable t = record.getThrown();
  if (t != null) {
    // Closing a StringWriter has no effect, so we don't need to do it in a
    // try-with-resources (but oddly, it throws IOException, so we couldn't
    // use it in a try-with-resources if we wanted to).
    StringWriter sw = new StringWriter();
    try (PrintWriter pw = new PrintWriter(sw)) { // NOPMD can't depend on Guava
      t.printStackTrace(pw);
      sb.append(sw).append("\n");
    }
  }
  return sb.toString();
}
 
Example 10
Source File: PLogger.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Override
public String format(LogRecord record) {

    return "[" + dateFormatter.format(new Date()) + "]	" + record.getThreadID() + " "
            + getLogLevelStr(record.getLevel()) + "	" + record.getMessage() + JVMToolHelper.getLineSeperator();
}
 
Example 11
Source File: LogUtil.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public String format(LogRecord record) {
  StringBuilder line = new StringBuilder();
  line.append(dateFormat.format(new Date()));
  if (Level.FINE.equals(record.getLevel())) {
    line.append(" FIN ");
  } else if (Level.FINEST.equals(record.getLevel())) {
    line.append(" FST ");
  } else if (Level.INFO.equals(record.getLevel())) {
    line.append(" INF ");
  } else if (Level.SEVERE.equals(record.getLevel())) {
    line.append(" SEV ");
  } else if (Level.WARNING.equals(record.getLevel())) {
    line.append(" WRN ");
  } else if (Level.FINER.equals(record.getLevel())) {
    line.append(" FNR ");
  } else if (Level.CONFIG.equals(record.getLevel())) {
    line.append(" CFG ");
  }

  int threadId = record.getThreadID();
  String threadIndent = getThreadIndent(threadId);

  line.append(threadIndent);
  line.append(" | ");
  line.append(record.getMessage());

  if (record.getThrown() != null) {
    line.append(LINE_SEPARATOR);

    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    record.getThrown().printStackTrace(printWriter);
    line.append(stringWriter.toString());
  }

  line.append("  [");
  line.append(record.getLoggerName());
  line.append("]");

  line.append(LINE_SEPARATOR);

  return line.toString();
}
 
Example 12
Source File: CompactFormatter.java    From FairEmail with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Formats the thread id property of the given log record. By default this
 * is formatted as a {@code long} by an unsigned conversion.
 *
 * @param record the record.
 * @return the formatted thread id as a number.
 * @throws NullPointerException if the given record is null.
 * @since JavaMail 1.5.4
 */
public Number formatThreadID(final LogRecord record) {
    /**
     * Thread.getID is defined as long and LogRecord.getThreadID is defined
     * as int. Convert to unsigned as a means to better map the two types of
     * thread identifiers.
     */
    return (((long) record.getThreadID()) & 0xffffffffL);
}