Example usage for java.lang StackTraceElement getFileName

List of usage examples for java.lang StackTraceElement getFileName

Introduction

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

Prototype

public String getFileName() 

Source Link

Document

Returns the name of the source file containing the execution point represented by this stack trace element.

Usage

From source file:com.ikon.util.StackTraceUtils.java

/**
 * Return the method who make the call./*from w  w  w.  ja v a  2  s  . co m*/
 */
public static void logTrace(Logger log) {
    // The constructor for Throwable has a native function that fills the stack trace.
    StackTraceElement[] trace = (new Throwable()).getStackTrace();

    // Once you have the trace you can pick out information you need.
    if (trace.length >= 2) {
        for (int i = 2; i < trace.length; i++) {
            if (trace[i].getClassName().startsWith("com.ikon")) {
                StackTraceElement sse = trace[i];
                log.warn("{} -> {} ({}:{})", new Object[] { sse.getClassName(), sse.getMethodName(),
                        sse.getFileName(), sse.getLineNumber() });
            }
        }
    }
}

From source file:com.openkm.util.StackTraceUtils.java

/**
 * Return the method who make the call./* w  w  w  .  ja va  2 s  .co  m*/
 */
public static void logTrace(Logger log) {
    // The constructor for Throwable has a native function that fills the stack trace.
    StackTraceElement[] trace = (new Throwable()).getStackTrace();

    // Once you have the trace you can pick out information you need.
    if (trace.length >= 2) {
        for (int i = 2; i < trace.length; i++) {
            if (trace[i].getClassName().startsWith("com.openkm")) {
                StackTraceElement sse = trace[i];
                log.warn("{} -> {} ({}:{})", new Object[] { sse.getClassName(), sse.getMethodName(),
                        sse.getFileName(), sse.getLineNumber() });
            }
        }
    }
}

From source file:Main.java

public static String getLogInfo(StackTraceElement stackTraceElement) {
    StringBuilder logInfoStringBuilder = new StringBuilder();
    String threadName = Thread.currentThread().getName();
    long threadID = Thread.currentThread().getId();
    String fileName = stackTraceElement.getFileName();
    String className = stackTraceElement.getClassName();
    String methodName = stackTraceElement.getMethodName();
    int lineNumber = stackTraceElement.getLineNumber();
    logInfoStringBuilder.append("[ ");
    logInfoStringBuilder.append("threadID=" + threadID).append(SEPARATOR);
    logInfoStringBuilder.append("threadName=" + threadName).append(SEPARATOR);
    logInfoStringBuilder.append("fileName=" + fileName).append(SEPARATOR);
    logInfoStringBuilder.append("className=" + className).append(SEPARATOR);
    logInfoStringBuilder.append("methodName=" + methodName).append(SEPARATOR);
    logInfoStringBuilder.append("lineNumber=" + lineNumber);
    logInfoStringBuilder.append(" ] ");
    return logInfoStringBuilder.toString();
}

From source file:Main.java

private static String getFunctionName() {
    StackTraceElement[] sts = Thread.currentThread().getStackTrace();
    if (sts == null) {
        return null;
    }//from   w ww .j a v  a  2 s .  c o  m
    for (StackTraceElement st : sts) {
        if (st.isNativeMethod()) {
            continue;
        }
        if (st.getClassName().equals(Thread.class.getName())) {
            continue;
        }
        if (st.getFileName().equals("LogUtils.java")) {
            continue;
        }
        return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + "): "
                + st.getFileName() + ":" + st.getLineNumber() + "]";
    }
    return null;
}

From source file:Main.java

private static void print(StackTraceElement element, Object message, Throwable error) {
    String className = element.getClassName();
    className = className.substring(className.lastIndexOf(".") + 1);
    String tag = className + '.' + element.getMethodName() + '(' + element.getFileName() + ':'
            + element.getLineNumber() + ')';
    String text = toString(message);

    if (error != null) {
        Log.e("[KakaCache]", tag + "\n\t" + text, error);
    } else {/* w  ww.  j a va 2s  . c  o m*/
        Log.e("[KakaCache]", tag + "\n\t" + text);
    }
}

From source file:org.apache.samza.logging.log4j2.serializers.LoggingEventJsonSerde.java

/**
 * Encodes a LoggingEvent into a HashMap using the logstash JSON format.
 *
 * @param loggingEvent/* w w w .j  a v a2s  .co  m*/
 *          The LoggingEvent to encode.
 * @param includeLocationInfo
 *          Whether to include LocationInfo in the map, or not.
 * @return A Map representing the LoggingEvent, which is suitable to be
 *         serialized by a JSON encoder such as Jackson.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> encodeToMap(LogEvent loggingEvent, boolean includeLocationInfo) {
    Map<String, Object> logstashEvent = new LoggingEventJsonSerde.LoggingEventMap();
    String threadName = loggingEvent.getThreadName();
    long timestamp = loggingEvent.getTimeMillis();
    HashMap<String, Object> exceptionInformation = new HashMap<String, Object>();
    Map mdc = loggingEvent.getContextData().toMap();
    ThreadContext.ContextStack ndc = loggingEvent.getContextStack();

    logstashEvent.put("@version", VERSION);
    logstashEvent.put("@timestamp", dateFormat(timestamp));
    logstashEvent.put("source_host", getHostname());
    logstashEvent.put("message", loggingEvent.getMessage());

    if (loggingEvent.getThrown() != null) {
        final Throwable throwableInformation = loggingEvent.getThrown();
        if (throwableInformation.getClass().getCanonicalName() != null) {
            exceptionInformation.put("exception_class", throwableInformation.getClass().getCanonicalName());
        }
        if (throwableInformation.getMessage() != null) {
            exceptionInformation.put("exception_message", throwableInformation.getMessage());
        }
        if (throwableInformation.getMessage() != null) {
            StringBuilder stackTrace = new StringBuilder(ExceptionUtils.getStackTrace(throwableInformation));
            exceptionInformation.put("stacktrace", stackTrace);
        }
        logstashEvent.put("exception", exceptionInformation);
    }

    if (includeLocationInfo) {
        StackTraceElement info = loggingEvent.getSource();
        logstashEvent.put("file", info.getFileName());
        logstashEvent.put("line_number", info.getLineNumber());
        logstashEvent.put("class", info.getClassName());
        logstashEvent.put("method", info.getMethodName());
    }

    logstashEvent.put("logger_name", loggingEvent.getLoggerName());
    logstashEvent.put("mdc", mdc);
    logstashEvent.put("ndc", ndc);
    logstashEvent.put("level", loggingEvent.getLevel().toString());
    logstashEvent.put("thread_name", threadName);

    return logstashEvent;
}

From source file:com.snicesoft.basekit.LogKit.java

private static String generateTag(StackTraceElement caller) {
    //        String tag = "%s.%s(L:%d)";
    String tag = "%s.%s(" + caller.getFileName() + ":%d)";
    String callerClazzName = caller.getClassName();
    callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
    tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
    tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
    return tag;/*from  w ww  .  jav a  2  s  . c o  m*/
}

From source file:com.dmitrymalkovich.android.githubanalytics.Utils.java

public static void openFeedback(Activity activity) {
    try {//ww  w .j  a v  a 2  s .c  om
        throw new IOException();
    } catch (IOException e) {
        ApplicationErrorReport report = new ApplicationErrorReport();
        report.packageName = report.processName = activity.getApplication().getPackageName();
        report.time = System.currentTimeMillis();
        report.type = ApplicationErrorReport.TYPE_CRASH;
        report.systemApp = false;
        ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
        crash.exceptionClassName = e.getClass().getSimpleName();
        crash.exceptionMessage = e.getMessage();
        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        e.printStackTrace(printer);
        crash.stackTrace = writer.toString();
        StackTraceElement stack = e.getStackTrace()[0];
        crash.throwClassName = stack.getClassName();
        crash.throwFileName = stack.getFileName();
        crash.throwLineNumber = stack.getLineNumber();
        crash.throwMethodName = stack.getMethodName();
        report.crashInfo = crash;
        Intent intent = new Intent(Intent.ACTION_APP_ERROR);
        intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
        activity.startActivity(intent);
    }
}

From source file:org.apache.tajo.exception.ExceptionUtil.java

/**
 * Return the string about the exception line ; e.g.,)
 * <code>Line 195 in JdbcTablespace.java</code>
 *
 * @return A string representing the line number and source file name at which the exception occurs.
 *//*  w  w  w  .  jav  a 2s .c o m*/
@SuppressWarnings("unused")
public static String getExceptionLine() {
    StackTraceElement stack = Thread.currentThread().getStackTrace()[3];
    return "Line " + stack.getLineNumber() + " in " + stack.getFileName();
}

From source file:de.micromata.genome.util.runtime.AssertUtils.java

/**
 * Gets the code line.//from   w w w  . ja  va 2s.c  o  m
 *
 * @param se the se
 * @return the code line
 */
public static String getCodeLine(final StackTraceElement se) {
    if (se == null) {
        return "<stack not found>";
    }
    final String fname = se.getFileName();
    final int cl = se.getLineNumber();
    final String clsName = se.getClassName();
    final String codeLine = getCodeLine(clsName, fname, cl);
    return StringUtils.trim(codeLine);
}