Example usage for java.util.logging LogRecord getThreadID

List of usage examples for java.util.logging LogRecord getThreadID

Introduction

In this page you can find the example usage for java.util.logging LogRecord getThreadID.

Prototype

public int getThreadID() 

Source Link

Document

Get an identifier for the thread where the message originated.

Usage

From source file:at.bitfire.davdroid.log.PlainTextFormatter.java

@Override
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public String format(LogRecord r) {
    StringBuilder builder = new StringBuilder();

    if (!logcat)//from   w  w  w. j a  va  2  s  . c om
        builder.append(DateFormatUtils.format(r.getMillis(), "yyyy-MM-dd HH:mm:ss")).append(" ")
                .append(r.getThreadID()).append(" ");

    builder.append(String.format("[%s] %s", shortClassName(r.getSourceClassName()), r.getMessage()));

    if (r.getThrown() != null)
        builder.append("\nEXCEPTION ").append(ExceptionUtils.getStackTrace(r.getThrown()));

    if (r.getParameters() != null) {
        int idx = 1;
        for (Object param : r.getParameters())
            builder.append("\n\tPARAMETER #").append(idx++).append(" = ").append(param);
    }

    if (!logcat)
        builder.append("\n");

    return builder.toString();
}

From source file:com.cloudbees.jenkins.support.SupportLogFormatter.java

@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = {
        "DE_MIGHT_IGNORE" }, justification = "The exception wasn't thrown on our stack frame")
public String format(LogRecord record) {
    StringBuilder builder = new StringBuilder();
    builder.append(fdf.format(new Date(record.getMillis())));
    builder.append(" [id=").append(record.getThreadID()).append("]");

    builder.append("\t").append(record.getLevel().getName()).append("\t");

    if (record.getSourceMethodName() != null) {
        String sourceClass;/*ww w .  java 2s .c  om*/
        if (record.getSourceClassName() == null) {
            sourceClass = record.getLoggerName();
        } else {
            sourceClass = record.getSourceClassName();
        }

        builder.append(abbreviateClassName(sourceClass, 32)).append("#").append(record.getSourceMethodName());
    } else {
        String sourceClass;
        if (record.getSourceClassName() == null) {
            sourceClass = record.getLoggerName();
        } else {
            sourceClass = record.getSourceClassName();
        }
        builder.append(abbreviateClassName(sourceClass, 40));
    }

    builder.append(": ").append(formatMessage(record));

    if (record.getThrown() != null) {
        try {
            StringWriter writer = new StringWriter();
            PrintWriter out = new PrintWriter(writer);
            record.getThrown().printStackTrace(out);
            out.close();
            builder.append(writer.toString());
        } catch (Exception e) {
            // ignore
        }
    }

    builder.append("\n");
    return builder.toString();
}

From source file:majordodo.task.BrokerTestUtils.java

@Before
public void brokerTestUtilsBefore() throws Exception {
    // Setup exception handler
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override/*from  ww w.java  2  s. c  o  m*/
        public void uncaughtException(Thread t, Throwable e) {
            System.err.println("uncaughtException from thread " + t.getName() + ": " + e);
            e.printStackTrace();
            unhandledExceptions.add(e);
        }
    });

    // Setup Logger
    System.out.println("Setup logger to level " + logLevel.getName());
    java.util.logging.LogManager.getLogManager().reset();
    ConsoleHandler ch = new ConsoleHandler();
    ch.setLevel(logLevel);
    ch.setFormatter(new Formatter() {
        @Override
        public String format(LogRecord record) {
            return "" + new java.sql.Timestamp(record.getMillis()) + " " + record.getLevel() + " ["
                    + getThreadName(record.getThreadID()) + "<" + record.getThreadID() + ">] "
                    + record.getLoggerName() + ": " + formatMessage(record) + "\n";
        }
    });
    java.util.logging.Logger.getLogger("").setLevel(logLevel);
    java.util.logging.Logger.getLogger("").addHandler(ch);

    // Initialize groupsMap
    groupsMap.clear();
    groupsMap.put(userId, group);

    // Setup workdir
    Path mavenTargetDir = Paths.get("target").toAbsolutePath();
    workDir = Files.createTempDirectory(mavenTargetDir, "test" + System.nanoTime());

    if (startBroker) {
        broker = new Broker(brokerConfig, new FileCommitLog(workDir, workDir, 1024 * 1024),
                new TasksHeap(1000, createTaskPropertiesMapperFunction()));
        broker.startAsWritable();

        server = new NettyChannelAcceptor(broker.getAcceptor());
        server.start();
    }
    if (startReplicatedBrokers) {
        zkServer = new ZKTestEnv(folderZk.getRoot().toPath());
        zkServer.startBookie();

        // Broker 1
        broker1 = new Broker(broker1Config,
                new ReplicatedCommitLog(zkServer.getAddress(), zkServer.getTimeout(), zkServer.getPath(),
                        folderSnapshots.newFolder().toPath(),
                        BrokerHostData.formatHostdata(
                                new BrokerHostData(broker1Host, broker1Port, "", false, null)),
                        false),
                new TasksHeap(1000, createTaskPropertiesMapperFunction()));

        broker1.startAsWritable();

        server1 = new NettyChannelAcceptor(broker1.getAcceptor(), broker1Host, broker1Port);
        server1.start();

        // Broker 2
        broker2 = new Broker(broker2Config,
                new ReplicatedCommitLog(zkServer.getAddress(), zkServer.getTimeout(), zkServer.getPath(),
                        folderSnapshots.newFolder().toPath(),
                        BrokerHostData.formatHostdata(
                                new BrokerHostData(broker2Host, broker2Port, "", false, null)),
                        false),
                new TasksHeap(1000, createTaskPropertiesMapperFunction()));

        broker2.start();

        server2 = new NettyChannelAcceptor(broker2.getAcceptor(), broker2Host, broker2Port);
        server2.start();

        // Broker locator
        brokerLocator = new ZKBrokerLocator(zkServer.getAddress(), zkServer.getTimeout(), zkServer.getPath());
    }

}

From source file:NemaLogFormatter.java

/** Formats the record.
 * /*from www.ja  v a  2 s .  co m*/
 * @param record The log record to format
 * @return The formated record
 */
@Override
public String format(LogRecord record) {
    String className = record.getSourceClassName();

    String threadName = Thread.currentThread().getName();
    if (threadName != null && threadName.length() > MAX_THREAD_NAME_LENGTH) {
        threadName = threadName.substring(threadName.length() - MAX_THREAD_NAME_LENGTH);
    }

    String sTimeStamp = FORMATER.format(new Date(record.getMillis()));

    return sTimeStamp + "::" + record.getLevel() + ":  " + record.getMessage() + "  "
            + ((bClass) ? " [" + className + "." + record.getSourceMethodName() + "]" : "")
            + ((bClass) ? " <" + threadName + ":" + record.getThreadID() + ">" : "") + NEW_LINE;
}

From source file:org.fornax.cartridges.sculptor.smartclient.server.util.UnifiedFormatter.java

@Override
public String format(LogRecord record) {
    String username = "ANONYMOUS";
    if (SecurityContextHolder.getContext() != null
            && SecurityContextHolder.getContext().getAuthentication() != null
            && SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof User) {
            username = ((User) principal).getUsername();
        } else {/*from   ww  w.j av a2 s  . c  o m*/
            username = principal.toString();
        }
    }

    int dotIndex = record.getSourceClassName().lastIndexOf(".");
    String className = record.getSourceClassName().substring(dotIndex != -1 ? dotIndex + 1 : 0);
    String msg = record.getMessage();
    if (record.getParameters() != null && record.getParameters().length > 0) {
        msg = MessageFormat.format(record.getMessage(), record.getParameters());
    }
    if (record.getThrown() != null) {
        Throwable thrown = record.getThrown();
        StringWriter result = new StringWriter();
        thrown.printStackTrace(new PrintWriter(result));
        result.flush();
        msg += "\n" + result.getBuffer();
    }
    return FST + dateFormat.format(record.getMillis()) + FET + FSEP + RST + FST + record.getLevel() + FET + FSEP
            + FST + className + "." + record.getSourceMethodName() + FET + FSEP + FST + username + FET + FSEP
            + FST + record.getThreadID() + FET + FSEP + FST + msg + FET + RET;
}

From source file:pl.otros.logview.logging.GuiJulHandler.java

@Override
public void publish(LogRecord lr) {
    if (isIgnoringLogRecord(lr)) {
        return;/*w w w  .ja  va  2  s. c o m*/
    }

    LogData ld = new LogData();
    ld.setMessage(lr.getMessage());
    ld.setDate(new Date(lr.getMillis()));
    ld.setFile("Olv-internal");
    ld.setClazz(lr.getSourceClassName());
    ld.setMethod(lr.getSourceMethodName());
    ld.setThread(Integer.toString(lr.getThreadID()));
    ld.setLevel(lr.getLevel());
    if (bufferingLogDataCollectorProxy != null) {
        bufferingLogDataCollectorProxy.add(ld);
    }
}