Package com.tagtraum.perf.gcviewer.util

Source Code of com.tagtraum.perf.gcviewer.util.LoggerHelper

package com.tagtraum.perf.gcviewer.util;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

* This class contains convenience methods to support logging.
* @author <a href="">Joerg Wuethrich</a>
* <p> created on: 26.02.2012</p>
public class LoggerHelper {

     * Logs a given exception using the given <code>logger</code>.
     * @param logger logger of the class where the exception occurred
     * @param level level to be used
     * @param msg message to be logged
     * @param t exception thrown
    public static void logException(Logger logger, Level level, String msg, Throwable t) {
        StackTraceElement callerStackTraceElement = getCallerStackTraceElement();
        if (callerStackTraceElement != null) {
            // using this stackTraceElements allows to log the message using the caller's class / method name
        else {
            // we haven't found the caller, so just log what we have
            LogRecord logRecord = new LogRecord(level, msg);

     * Finds the first StackTraceElement outside this loggerHelper.
    private static StackTraceElement getCallerStackTraceElement() {
        Throwable throwable = new Throwable();

        String logClassName = LoggerHelper.class.getName();
        for (StackTraceElement stackTraceElement : throwable.getStackTrace()) {
            String cname = stackTraceElement.getClassName();
            if (!cname.equals(logClassName)) {
                return stackTraceElement;

        // should not happen, but means that we haven't found a stackTraceElement outside this class
        return null;

Related Classes of com.tagtraum.perf.gcviewer.util.LoggerHelper

Copyright © 2018 All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact