Example usage for java.util.logging LogRecord getThrown

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

Introduction

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

Prototype

public Throwable getThrown() 

Source Link

Document

Get any throwable associated with the log record.

Usage

From source file:ch.jamiete.hilda.music.MusicLogFormat.java

@Override
public final String format(final LogRecord record) {
    String log = "";

    log += this.sdf.format(new Date(record.getMillis()));
    log += " [" + record.getLevel().getLocalizedName().toUpperCase() + ']';

    if (record.getSourceClassName() != null) {
        final String[] split = record.getSourceClassName().split("\\.");
        log += " [" + split[(split.length == 1) ? 0 : (split.length - 1)] + ']';
    }/*from  w w  w .ja  v a  2  s.com*/

    if (record.getSourceMethodName() != null) {
        log += " (" + record.getSourceMethodName() + ')';
    }

    log += ' ' + record.getMessage();

    if (record.getThrown() != null) {
        /*log += "\n" + record.getThrown().getMessage();
                
        for (StackTraceElement element : record.getThrown().getStackTrace()) {
        log += "\n" + element.toString();
        }*/
        log += '\n' + ExceptionUtils.getStackTrace(record.getThrown());
    }

    log += System.getProperty("line.separator");

    return log;
}

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 w w w  .j  a  va 2s.  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: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;//from   w ww. j  a v  a  2  s  .c o m
        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:ffx.ui.LogHandler.java

/**
 * {@inheritDoc}//  w ww  .j a  v  a  2s  . c o  m
 *
 * Publish a LogRecord.
 *
 * @since 1.0.
 */
@Override
public synchronized void publish(LogRecord record) {
    if (record.getLevel() == Level.OFF) {
        if (record.getMessage().toLowerCase().contains("algorithm failure:")) {
            mainPanel.setExitType(MainPanel.ExitStatus.ALGORITHM_FAILURE);
        }
        return;
    }
    /**
     * Check if the record is loggable and that we have not already
     * encountered a fatal error.
     */
    if (!isLoggable(record) || fatal) {
        return;
    }
    String msg;
    try {
        msg = getFormatter().format(record);
    } catch (Exception e) {
        /**
         * We don't want to throw an exception here, but we report the
         * exception to any registered ErrorManager.
         */
        reportError(null, e, ErrorManager.FORMAT_FAILURE);
        return;
    }
    try {
        if (record.getLevel() == Level.SEVERE) {
            fatal = true;
            System.err.println(msg);

            Throwable throwable = record.getThrown();
            if (throwable != null) {
                System.err.println(String.format(" Exception %s logged.", throwable));
            }

            // If tryCatchSevere, and the throwable (if it exists) is not an Error, then...
            if (tryCatchSevere && (throwable == null || !(throwable instanceof Error))) {
                System.err.println(" Force Field X may not continue.");
                System.err.println(" Throwing new error...");
                fatal = false;
                if (throwable != null) {
                    throw new LoggerSevereError(throwable);
                } else {
                    throw new LoggerSevereError(" Unknown exception");
                }
            }

            System.err.println(" Force Field X will not continue.");
            System.err.println(" Shutting down...");
            flush();
            mainPanel.setExitType(MainPanel.ExitStatus.SEVERE);
            mainPanel.exit();
        }
        ModelingShell shell = null;
        if (mainPanel != null) {
            shell = mainPanel.getModelingShell();
        }

        if (!headless && shell != null) {
            shell.appendOutputNl(msg, shell.getResultStyle());
        } else {
            System.out.println(msg);
        }
    } catch (Exception e) {
        /**
         * We don't want to throw an exception here, but we report the
         * exception to any registered ErrorManager.
         */
        reportError(null, e, ErrorManager.WRITE_FAILURE);
    }
}

From source file:org.apache.geode.management.internal.cli.shell.GfshInitFileJUnitTest.java

@Test
public void testInitFile_NotProvided() throws Exception {
    /*//  ww  w .j a v a2s  . com
     * String historyFileName, String defaultPrompt, int historySize, String logDir, Level logLevel,
     * Integer logLimit, Integer logCount, String initFileName
     */
    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
            temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null, null, null, null);
    assertNull(INIT_FILE_NAME, gfshConfig.getInitFileName());

    /*
     * boolean launchShell, String[] args, GfshConfig gfshConfig
     */
    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);

    int actualStatus = gfsh.getLastExecutionStatus();
    int expectedStatus = 0;
    assertEquals("Status 0==success", expectedStatus, actualStatus);

    int expectedLogCount = BANNER_LINES;
    assertEquals("Log records written", expectedLogCount, this.junitLoggerHandler.getLog().size());
    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
        assertNull("No exceptions in log", logRecord.getThrown());
    }
}

From source file:org.apache.geode.management.internal.cli.shell.GfshInitFileJUnitTest.java

@Test
public void testInitFile_NotFound() throws Exception {
    // Construct the file name but not the file
    String initFileName = temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath() + File.separator
            + INIT_FILE_NAME;/*from w  w  w.j a va2s.  c  o m*/

    /*
     * String historyFileName, String defaultPrompt, int historySize, String logDir, Level logLevel,
     * Integer logLimit, Integer logCount, String initFileName
     */
    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
            temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null, null, null, initFileName);
    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());

    /*
     * boolean launchShell, String[] args, GfshConfig gfshConfig
     */
    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);

    int actualStatus = gfsh.getLastExecutionStatus();
    int expectedStatus = 0;
    assertNotEquals("Status <0==failure", expectedStatus, actualStatus);

    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
    assertEquals("Log records written", expectedLogCount, this.junitLoggerHandler.getLog().size());
    Throwable exception = null;
    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
        if (logRecord.getThrown() != null) {
            exception = logRecord.getThrown();
            break;
        }
    }
    assertNotNull("Exceptions in log", exception);
}

From source file:org.apache.geode.management.internal.cli.shell.GfshInitFileJUnitTest.java

@Test
public void testInitFile_Empty() throws Exception {
    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);

    /*/*from www.j ava 2s .  c o m*/
     * String historyFileName, String defaultPrompt, int historySize, String logDir, Level logLevel,
     * Integer logLimit, Integer logCount, String initFileName
     */
    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
            temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null, null, null,
            initFile.getAbsolutePath());
    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());

    /*
     * boolean launchShell, String[] args, GfshConfig gfshConfig
     */
    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);

    int actualStatus = gfsh.getLastExecutionStatus();
    int expectedStatus = 0;
    assertEquals("Status 0==success", expectedStatus, actualStatus);

    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
    assertEquals("Log records written", expectedLogCount, this.junitLoggerHandler.getLog().size());
    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
        assertNull("No exceptions in log", logRecord.getThrown());
    }
}

From source file:org.apache.geode.management.internal.cli.shell.GfshInitFileJUnitTest.java

@Test
public void testInitFile_OneGoodCommand() throws Exception {
    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
    FileUtils.writeStringToFile(initFile, "echo --string=hello" + Gfsh.LINE_SEPARATOR, APPEND);

    /*//from w  w  w  . j a v a2s . co m
     * String historyFileName, String defaultPrompt, int historySize, String logDir, Level logLevel,
     * Integer logLimit, Integer logCount, String initFileName
     */
    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
            temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null, null, null,
            initFile.getAbsolutePath());
    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());

    /*
     * boolean launchShell, String[] args, GfshConfig gfshConfig
     */
    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);

    int actualStatus = gfsh.getLastExecutionStatus();
    int expectedStatus = 0;
    assertEquals("Status 0==success", expectedStatus, actualStatus);

    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
    assertEquals("Log records written", expectedLogCount, this.junitLoggerHandler.getLog().size());
    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
        assertNull("No exceptions in log", logRecord.getThrown());
    }
}

From source file:org.apache.geode.management.internal.cli.shell.GfshInitFileJUnitTest.java

@Test
public void testInitFile_TwoGoodCommands() throws Exception {
    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
    FileUtils.writeStringToFile(initFile, "echo --string=hello" + Gfsh.LINE_SEPARATOR, APPEND);
    FileUtils.writeStringToFile(initFile, "echo --string=goodbye" + Gfsh.LINE_SEPARATOR, APPEND);

    /*//from   www .j  a va2 s  .c  o  m
     * String historyFileName, String defaultPrompt, int historySize, String logDir, Level logLevel,
     * Integer logLimit, Integer logCount, String initFileName
     */
    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
            temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null, null, null,
            initFile.getAbsolutePath());
    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());

    /*
     * boolean launchShell, String[] args, GfshConfig gfshConfig
     */
    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);

    int actualStatus = gfsh.getLastExecutionStatus();
    int expectedStatus = 0;
    assertEquals("Status 0==success", expectedStatus, actualStatus);

    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
    assertEquals("Log records written", expectedLogCount, this.junitLoggerHandler.getLog().size());
    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
        assertNull("No exceptions in log", logRecord.getThrown());
    }
}

From source file:org.apache.shindig.gadgets.oauth.OAuthRequestTest.java

private String getLogText() {
    StringBuilder logText = new StringBuilder();
    for (LogRecord record : logRecords) {
        logText.append(record.getMessage());
        if (record.getThrown() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            record.getThrown().printStackTrace(pw);
            pw.flush();//from   www  .  j av  a 2 s.  c o  m
            logText.append(sw.toString());
        }
    }
    return logText.toString();
}