Example usage for java.lang StackTraceElement getMethodName

List of usage examples for java.lang StackTraceElement getMethodName

Introduction

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

Prototype

public String getMethodName() 

Source Link

Document

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

Usage

From source file:io.stallion.services.Log.java

public static void warning(String message, Object... args) {
    if (getLogLevel().intValue() > Level.WARNING.intValue()) {
        return;/*from w w w  .  j  a va  2s.c  om*/
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.WARNING, clz, method, message, args);

}

From source file:io.stallion.services.Log.java

public static void warn(String message, Object... args) {
    if (getLogLevel().intValue() > Level.WARNING.intValue()) {
        return;/*from   ww  w .j av a 2 s.com*/
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.WARNING, clz, method, message, args);

}

From source file:io.stallion.services.Log.java

public static void warn(Throwable ex, String message, Object... args) {
    if (getLogLevel().intValue() > Level.WARNING.intValue()) {
        return;// ww w  .j a  v  a  2 s  .c o  m
    }
    if (args.length > 0) {
        message = MessageFormat.format(message, args);
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.WARNING, clz, method, message, ex);
}

From source file:io.stallion.services.Log.java

/**
 * Logs a message, setting the class, method and source line number using the stack frame index passed in.
 * This is useful if you are wrapping a logging class, and want to include the line number from where
 * the wrapper method is called./*from www. j a va2s .  c  o m*/
 *
 * @param frame - the stack frame to use. Use '2' to log to one level above the caller
 * @param level
 * @param message
 * @param args
 */
public static void logForFrame(int frame, Level level, String message, Object... args) {
    if (getLogLevel().intValue() > level.intValue()) {
        return;
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[frame];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[frame].getLineNumber();
    logger.logp(level, clz, method, message, args);
}

From source file:io.stallion.services.Log.java

public static void exception(Throwable ex, String message, Object... args) {
    HealthTracker.instance().logException(ex);
    if (getLogLevel().intValue() > Level.SEVERE.intValue()) {
        return;//from   w  w w.j  a v  a2  s  .  co  m
    }
    if (args.length > 0) {
        message = MessageFormat.format(message, args);
    }
    // TODO: WTF -- figure out how the hell the handler got removed;
    if (logger.getHandlers().length == 0) {
        logger.addHandler(handler);
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.SEVERE, clz, method, message, ex);
}

From source file:griffon.util.GriffonUtil.java

public static void printSanitizedStackTrace(Throwable t, PrintWriter p) {
    t = sanitize(t);//from  w  w  w  .  j a  v  a 2  s.c  o m

    StackTraceElement[] trace = t.getStackTrace();
    for (int i = 0; i < trace.length; i++) {
        StackTraceElement stackTraceElement = trace[i];
        p.println("at " + stackTraceElement.getClassName() + "(" + stackTraceElement.getMethodName() + ":"
                + stackTraceElement.getLineNumber() + ")");
    }
}

From source file:io.stallion.services.Log.java

public static void info(String message, Object... args) {
    if (getLogLevel().intValue() > Level.INFO.intValue()) {
        return;// ww  w.j  a  va2 s.c  o  m
    }
    // Info statements don't include the class and line number, since that kills performance
    //System.out.println(message);
    if (alwaysIncludeLineNumber) {
        Throwable t = new Throwable();
        StackTraceElement stackTraceElement = t.getStackTrace()[1];
        String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
        String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
        logger.logp(Level.INFO, clz, method, message, args);
    } else {
        logger.logp(Level.INFO, "", "", message, args);
    }

}

From source file:fr.opensagres.xdocreport.core.logging.LogUtils.java

private static void doLog(Logger log, Level level, String msg, Throwable t) {
    LogRecord record = new LogRecord(level, msg);

    record.setLoggerName(log.getName());
    record.setResourceBundleName(log.getResourceBundleName());
    record.setResourceBundle(log.getResourceBundle());

    if (t != null) {
        record.setThrown(t);/*ww w .  j a  va  2 s  . c o m*/
    }

    // try to get the right class name/method name - just trace
    // back the stack till we get out of this class
    StackTraceElement stack[] = (new Throwable()).getStackTrace();
    String cname = LogUtils.class.getName();
    for (int x = 0; x < stack.length; x++) {
        StackTraceElement frame = stack[x];
        if (!frame.getClassName().equals(cname)) {
            record.setSourceClassName(frame.getClassName());
            record.setSourceMethodName(frame.getMethodName());
            break;
        }
    }
    log.log(record);
}

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

public static void openFeedback(Activity activity) {
    try {/*from   w ww .j  a  va2s .c  o m*/
        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:com.cloud.utils.StringUtils.java

public static String getExceptionStackInfo(final Throwable e) {
    final StringBuffer sb = new StringBuffer();

    sb.append(e.toString()).append("\n");
    final StackTraceElement[] elemnents = e.getStackTrace();
    for (final StackTraceElement element : elemnents) {
        sb.append(element.getClassName()).append(".");
        sb.append(element.getMethodName()).append("(");
        sb.append(element.getFileName()).append(":");
        sb.append(element.getLineNumber()).append(")");
        sb.append("\n");
    }//from   w w  w .  ja va 2  s. c  om

    return sb.toString();
}