All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.maizegenetics.util.ExceptionUtils Maven / Gradle / Ivy

/*
 * ExceptionUtils.java
 *
 * Created on May 25, 2003, 2:11 AM
 */

package net.maizegenetics.util;


import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;


/**
 *
 * @author  terryc
 */
public final class ExceptionUtils {
    
    /**
     * Default maximun number of entries.
     */
    public static final int DEFAULT_MAX_NUMBER_ENTRIES = 50;
    
    
    /**
     * ExceptionUtils Constructor.
     */
    private ExceptionUtils() {
    }
    
    
    /**
     * This sends an exception's stack trace to the specified
     * logger.  The max parameter defines the maximun number of
     * entries to send to the logger.  This logs the default number
     * of entries defined by DEFAULT_MAX_NUMBER_ENTRIES at the
     * default priorty of debug.
     *
     * @param exception exception to log
     * @param logger logger to send entries
     */
    public static void logStackTrace(Exception exception, Logger logger) {
        logStackTrace(exception, DEFAULT_MAX_NUMBER_ENTRIES, logger, Level.DEBUG);
    }
    
    
    /**
     * This sends an exception's stack trace to the specified
     * logger.  The max parameter defines the maximun number of
     * entries to send to the logger.  Setting max to a negative
     * number sends all the entries in the stack track to the logger.
     *
     * @param exception exception to log
     * @param max maximun number of entries to log
     * @param logger logger to send entries
     * @param priorty priorty of log message
     */
    public static void logStackTrace(Exception exception, int max, Logger logger, Level priorty) {
        logger.log(priorty, getStackTrace(exception, max));
    }
    
    
    /**
     * This sends an exception's stack trace to the specified
     * logger.  The max parameter defines the maximun number of
     * entries to send to the logger.  Setting max to a negative
     * number sends all the entries in the stack track to the logger.
     *
     * @param exception exception to log
     * @param max maximun number of entries to log
     *
     * @return stack trace as string
     */
    public static String getStackTrace(Exception exception, int max) {
        
        StackTraceElement [] elements = exception.getStackTrace();
        
        int numEntries = elements.length;
        
        if ((max >= 0) && (max < numEntries)) {
            numEntries = max;
        }
        
        StringBuffer buffer = new StringBuffer();
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy