Java slf4j Logger stacktrace(Logger log, Throwable t)

Here you can find the source of stacktrace(Logger log, Throwable t)

Description

Print the stacktrace to the log.

License

Apache License

Parameter

Parameter Description
log - Logger handle
t - Throwable

Declaration

public static void stacktrace(Logger log, Throwable t) 

Method Source Code


//package com.java2s;
/*//from w  ww  . j a va 2  s  .c om
 * Copyright 2014 Subhabrata Ghosh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.slf4j.Logger;

public class Main {
    /**
     * Print the stacktrace to the log. This method will recursively print the inner exceptions' stacktrace as well.
     * Only prints if in debug mode.
     *
     * @param log - Logger handle
     * @param t   - Throwable
     */
    public static void stacktrace(Logger log, Throwable t) {
        try {
            if (log.isDebugEnabled()) {
                if (t != null) {
                    log.debug("*****************************<<stacktrace>>*******************************");
                    log.debug(t.getLocalizedMessage());
                    StackTraceElement[] ste = t.getStackTrace();
                    if (ste != null && ste.length > 0) {
                        for (StackTraceElement st : ste) {
                            log.debug("[" + st.getClassName() + "." + st.getMethodName() + "()]:" + st.getFileName()
                                    + ":" + st.getLineNumber() + "");
                        }
                    }
                }
                if (t.getCause() != null) {
                    stacktrace(log, t.getCause());
                }
            }
        } catch (Throwable ex) {
            // Ignore logging exceptions.
        }
    }

    /**
     * Print the stacktrace to the stderr. This method will recursively print the inner exceptions' stacktrace as well.
     * Only prints if in debug mode.
     *
     * @param t - Throwable
     */
    public static void stacktrace(Throwable t) {
        try {
            if (t != null) {
                System.err.println("*****************************<<stacktrace>>*******************************");
                System.err.println(t.getLocalizedMessage());
                StackTraceElement[] ste = t.getStackTrace();
                if (ste != null && ste.length > 0) {
                    for (StackTraceElement st : ste) {
                        System.err.println("[" + st.getClassName() + "." + st.getMethodName() + "()]:"
                                + st.getFileName() + ":" + st.getLineNumber() + "");
                    }
                }
            }
            if (t.getCause() != null) {
                stacktrace(t.getCause());
            }

        } catch (Throwable ex) {
            // Ignore logging exceptions.
        }
    }

    /**
     * Utility method to print a formatted debug message to the log.
     *
     * @param log  - Logger handle.
     * @param fmt  - String print format.
     * @param args - String message arguments.
     */
    public static void debug(Logger log, String fmt, String... args) {
        try {
            log.debug(String.format(fmt, args));
        } catch (Throwable ex) {
            // Ignore logging exceptions.
        }
    }
}

Related

  1. setLogger(Logger logger)
  2. setOutputFile(String filename)
  3. setRequestId(String requestId)
  4. sleep(int milliseconds)
  5. sleepMs(Logger logger, long timeMs)
  6. startTestLogging(String name)
  7. stopTestLogging()
  8. stringifyStackTrace(Exception e)
  9. testLogError(String errorMessage, Exception ex)