Example usage for java.lang StackTraceElement getClassName

List of usage examples for java.lang StackTraceElement getClassName

Introduction

In this page you can find the example usage for java.lang StackTraceElement getClassName.

Prototype

public String getClassName() 

Source Link

Document

Returns the fully qualified name of the class containing the execution point represented by this stack trace element.

Usage

From source file:net.ymate.platform.log.jcl.JCLogger.java

/**
 * ??//from w  w  w.j  a  va 2s  .  co m
 *
 * @return ??className.methodName:lineNumber?NO_STACK_TRACE:-1
 */
protected String __doMakeCallerInfo() {
    StackTraceElement[] _stacks = new Throwable().getStackTrace();
    // ???
    if (_stacks.length > 3) {
        StackTraceElement _element = _stacks[3];
        return StringUtils.substringBeforeLast(_element.getClassName(), ".").concat(".")
                .concat(_element.getMethodName()).concat(":")
                .concat(_element.getLineNumber() + StringUtils.EMPTY);
    }
    return "NO_STACK_TRACE:-1";
}

From source file:com.bstek.dorado.view.task.LongTaskThread.java

public ExceptionInfo(Exception e) {
    Throwable throwable = e;/*from  w  w  w  .  j  av a2s  .  c  o  m*/
    // while (throwable.getCause() != null) {
    // throwable = throwable.getCause();
    // }

    message = throwable.getMessage();
    if (message == null) {
        message = throwable.getClass().getSimpleName();
    }

    StackTraceElement[] stackTraceElements = throwable.getStackTrace();
    stackTrace = new String[stackTraceElements.length];
    int i = 0;
    for (StackTraceElement stackTraceElement : stackTraceElements) {
        stackTrace[i] = stackTraceElement.getClassName() + '.' + stackTraceElement.getMethodName() + '('
                + stackTraceElement.getFileName() + ':' + stackTraceElement.getLineNumber() + ')';
        i++;
    }
}

From source file:jext2.DataStructureAccessProvider.java

private StackTraceElement[] filterStrackTraceForLog(StackTraceElement[] stack) {
    LinkedList<StackTraceElement> interresting = new LinkedList<StackTraceElement>();

    for (StackTraceElement element : stack) {
        if (element.getClassName().contains("jext2"))
            interresting.add(element);/*w  w  w  .  j a va2  s.c o  m*/
    }

    return interresting.toArray(new StackTraceElement[0]);
}

From source file:be.wegenenverkeer.common.resteasy.exception.ExceptionUtil.java

/**
 * Get the stack trace as a string. Filters framework lines from the trace.
 *
 * @return stack trace/*from w ww . j  a  v  a2 s.  co m*/
 */
public String getStackTrace() {
    StringBuilder sb = new StringBuilder();
    Throwable exception = originalException;

    while (null != exception) {
        int filteredCount = 0;
        sb.append(exception.getMessage()).append('\n');
        for (StackTraceElement ste : exception.getStackTrace()) {
            if (shouldDisplay(ste.getClassName())) {
                sb.append("    ");
                while (filteredCount > 0) {
                    sb.append(',');
                    filteredCount--;
                }
                sb.append(ste.toString()).append('\n');
            } else {
                filteredCount++;
            }
        }
        exception = exception.getCause();
    }
    return sb.toString();
}

From source file:io.coala.log.CoalaLog4jLogger.java

/**
 * @return/*ww w  . ja  v  a2 s . c o m*/
 */
protected String getMethodAffix() {
    int i = 4;
    StackTraceElement elem = Thread.currentThread().getStackTrace()[i];
    while (isLoggingPackage(elem.getClassName()))
        elem = Thread.currentThread().getStackTrace()[++i];
    return String.format(METHOD_AFFIX_FORMAT, elem.getClassName(), elem.getMethodName(), elem.getFileName(),
            elem.getLineNumber());
}

From source file:com.jkoolcloud.tnt4j.utils.Utils.java

/**
 * Return calling stack frame which is right above a given class marker plus
 * the offset./*from  ww  w . jav a2  s  .c om*/
 *
 * @param classMarker
 *            class marker on the stack
 * @param offset
 *            offset on the stack from the marker
 * @return Return calling stack frame which is right above a given class
 *         marker
 */
public static StackTraceElement getStackFrame(String classMarker, int offset) {
    int index = 0;
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    StackTraceElement first = null, found = stack[stack.length - 1];
    for (StackTraceElement item : stack) {
        if (first == null && item.getClassName().startsWith(classMarker)) {
            first = item;
        } else if (first != null && !item.getClassName().startsWith(classMarker)) {
            found = stack[index + offset];
            break;
        }
        index++;
    }
    return found;
}

From source file:com.lazy.gank.logging.Logcat.java

/**
 * @param stackTraceElement//from ww w.  j  av a  2 s.  com
 * @param type
 * @param objectMsg
 * @param logFileName
 * @param tagArgs
 */
private static void fileLog(StackTraceElement stackTraceElement, int type, Object objectMsg,
        @Nullable String logFileName, @Nullable String... tagArgs) {
    String msg;
    if (m_cFileSaveLogType == OPERATION_BIT) {
        return;
    }

    String fileName = stackTraceElement.getFileName();
    String className = stackTraceElement.getClassName();
    String methodName = stackTraceElement.getMethodName();
    int lineNumber = stackTraceElement.getLineNumber();

    StringBuilder tagBuilder = new StringBuilder();
    tagBuilder.append(TAG);
    if (tagArgs == null) {
        tagBuilder.append(TAG_SEPARATOR);
        tagBuilder.append(className);
    } else {
        for (String tagArg : tagArgs) {
            tagBuilder.append(TAG_SEPARATOR);
            tagBuilder.append(tagArg);
        }
    }

    methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);

    StringBuilder stringBuilder = new StringBuilder();

    // ??

    String strDateTimeLogHead = simpleDateFormat.format(new Date());

    // ?????
    stringBuilder.append(tagBuilder.toString()).append(" ").append(strDateTimeLogHead).append(LINE_SEPARATOR)
            .append("fileName:").append(fileName).append(LINE_SEPARATOR).append("className:").append(className)
            .append(LINE_SEPARATOR).append("methodName:").append(methodName).append(LINE_SEPARATOR)
            .append("lineNumber:").append(lineNumber).append(LINE_SEPARATOR);

    if (objectMsg == null) {
        msg = "Log with null Object";
    } else {
        msg = objectMsg.toString();
    }
    if (msg != null && type != SHOW_JSON_LOG) {
        stringBuilder.append(msg);
    }
    stringBuilder.append(LINE_SEPARATOR + LINE_SEPARATOR);

    switch (type) {
    case SHOW_VERBOSE_LOG:
        if (logFileName == null) {
            saveLog2File(V + stringBuilder.toString());
        } else {
            saveLog2File(V + stringBuilder.toString(), logFileName);
        }

        break;
    case SHOW_DEBUG_LOG:
        if (logFileName == null) {
            saveLog2File(D + stringBuilder.toString());
        } else {
            saveLog2File(D + stringBuilder.toString(), logFileName);
        }
        break;
    case SHOW_INFO_LOG:
        if (logFileName == null) {
            saveLog2File(I + stringBuilder.toString());
        } else {
            saveLog2File(I + stringBuilder.toString(), logFileName);
        }

        break;
    case SHOW_WARN_LOG:
        if (logFileName == null) {
            saveLog2File(W + stringBuilder.toString());
        } else {
            saveLog2File(W + stringBuilder.toString(), logFileName);
        }
        break;
    case SHOW_ERROR_LOG:
        if (logFileName == null) {
            saveLog2File(E + stringBuilder.toString());
        } else {
            saveLog2File(E + stringBuilder.toString(), logFileName);
        }
        break;
    case SHOW_JSON_LOG: {
        if (TextUtils.isEmpty(msg)) {
            Log.d(tagBuilder.toString(), "Empty or Null json content");
            return;
        }

        String message = null;

        try {
            if (msg.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(msg);
                message = jsonObject.toString(JSON_INDENT);
            } else if (msg.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(msg);
                message = jsonArray.toString(JSON_INDENT);
            }
        } catch (JSONException e) {
            e("JSONException/" + tagBuilder.toString(), e.getCause().getMessage() + LINE_SEPARATOR + msg);
            return;
        }

        stringBuilder.append(JSON);
        stringBuilder.append(LINE_SEPARATOR);
        stringBuilder.append(
                "??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????");
        message = stringBuilder.toString() + LINE_SEPARATOR + message;
        String[] lines = message.split(LINE_SEPARATOR);
        StringBuilder jsonContent = new StringBuilder();
        for (String line : lines) {
            jsonContent.append(" ").append(line).append(LINE_SEPARATOR);
        }
        jsonContent.append(
                "????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????");
        if (logFileName == null) {
            saveLog2File(jsonContent.toString());
        } else {
            saveLog2File(stringBuilder.toString(), logFileName);
        }

    }
        break;
    }

}

From source file:net.logstash.logback.LogstashFormatter.java

private void createFields(ILoggingEvent event, Context context, ObjectNode eventNode) {
    final Marker marker = event.getMarker();

    eventNode.put("logger_name", event.getLoggerName());
    eventNode.put("thread_name", event.getThreadName());
    eventNode.put("level", event.getLevel().toString());
    eventNode.put("level_value", event.getLevel().toInt());

    if (includeCallerInfo) {
        StackTraceElement callerData = extractCallerData(event);
        eventNode.put("caller_class_name", callerData.getClassName());
        eventNode.put("caller_method_name", callerData.getMethodName());
        eventNode.put("caller_file_name", callerData.getFileName());
        eventNode.put("caller_line_number", callerData.getLineNumber());
    }//from ww w .  j  a  v a2s . c  o  m

    IThrowableProxy throwableProxy = event.getThrowableProxy();
    if (throwableProxy != null) {
        eventNode.put("stack_trace", ThrowableProxyUtil.asString(throwableProxy));
    }

    if (context != null) {
        addPropertiesAsFields(eventNode, context.getCopyOfPropertyMap());
    }
    if (marker != null && marker.contains("JSON")) {
        eventNode.put("json_message", getJsonNode(event));
    }
    addPropertiesAsFields(eventNode, event.getMDCPropertyMap());

    addCustomFields(eventNode);

}

From source file:org.hillview.utils.HillviewLogger.java

private String createMessage(String message, String format, Object... arguments) {
    message = this.checkCommas(message);
    String text = MessageFormat.format(format, arguments);
    Thread current = Thread.currentThread();
    StackTraceElement[] stackTraceElements = current.getStackTrace();
    StackTraceElement caller = stackTraceElements[3];
    String quoted = this.quote(text);
    return String.join(",", current.getName(), caller.getClassName(), caller.getMethodName(), message, quoted);
}

From source file:com.stackify.log.logback.ILoggingEventAdapter.java

/**
 * @see com.stackify.api.common.log.EventAdapter#getStackifyError(java.lang.Object, java.lang.Throwable)
 *//*from   w w w  .  jav a 2s.c om*/
@Override
public StackifyError getStackifyError(final ILoggingEvent event, final Throwable exception) {

    StackifyError.Builder builder = StackifyError.newBuilder();
    builder.environmentDetail(envDetail);
    builder.occurredEpochMillis(new Date(event.getTimeStamp()));

    if (exception != null) {
        builder.error(Throwables.toErrorItem(event.getFormattedMessage(), exception));
    } else {
        String className = null;
        String methodName = null;
        int lineNumber = 0;

        StackTraceElement[] callerData = event.getCallerData();

        if (callerData != null) {
            StackTraceElement locInfo = callerData[0];

            if (locInfo != null) {
                className = locInfo.getClassName();
                methodName = locInfo.getMethodName();
                lineNumber = locInfo.getLineNumber();
            }
        }

        builder.error(Throwables.toErrorItem(event.getFormattedMessage(), className, methodName, lineNumber));
    }

    String user = ServletLogContext.getUser();

    if (user != null) {
        builder.userName(user);
    }

    WebRequestDetail webRequest = ServletLogContext.getWebRequest();

    if (webRequest != null) {
        builder.webRequestDetail(webRequest);
    }

    builder.serverVariables(Maps.fromProperties(System.getProperties()));

    return builder.build();
}