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

net.sf.log4jdbc.log.log4j2.message.ExceptionOccuredMessage Maven / Gradle / Ivy

package net.sf.log4jdbc.log.log4j2.message;

import net.sf.log4jdbc.sql.Spy;

import org.apache.logging.log4j.message.Message;

/**
 * SqlMessage related to the logging of Exceptions.
 * 
 * @author Frederic Bastian
 * @see net.sf.log4jdbc.log4j2.Log4j2SpyLogDelegator#exceptionOccured(Spy, String, Exception, String, long)
 * @version 1.0
 * @since 1.0
 */
public class ExceptionOccuredMessage extends SqlMessage implements Message
{
    private static final long serialVersionUID = 4033892630843448750L;
    /**
     * the Spy wrapping the class that threw an Exception. 
     * Will be used to build the message, only when needed.
     * @see #message
     * @see #buildMessage()
     */
	private Spy spy;
	/**
     * a String describing the name and call parameters 
     * of the method generated the Exception. 
     * Will be used to build the message, only when needed.
     * @see #message
     * @see #buildMessage()
     */
	private String methodCall;
	/**
     * long representing the amount of time 
     * that passed before an Exception was thrown when sql was being executed.
     * Optional and should be set to -1 if not used. 
     * Will be used to build the message, only when needed.
     * @see #message
     * @see #buildMessage()
     */
	private long execTime;
	/**
     * String representing the sql that occurred 
     * just before the exception occurred. 
     * Optional and null if not used.
     * Will be used to build the message, only when needed.
     * @see #message
     * @see #buildMessage()
     */
	private String sql;
    
    /**
     * Default constructor
     */
    public ExceptionOccuredMessage()
    {
    	this(null, null, null, -1, false);
    }
    /**
     * 
     * @param spy        	the Spy wrapping the class that threw an Exception.
     * @param methodCall 	a String describing the name and call parameters 
     * 						of the method generated the Exception.
     * @param e          	the Exception that was thrown.
     * @param sql        	optional String representing the sql that occurred 
     * 						just before the exception occurred.
     * @param execTime   	optional long representing the amount of time 
     * 						that passed before an Exception was thrown when sql was being executed.
     *                   	caller should pass -1 if not used.
     * @param isDebugEnabled A boolean to define whether debugInfo should be displayed.
     */
    public ExceptionOccuredMessage(Spy spy, String methodCall, 
	        String sql, long execTime, boolean isdebugEnabled) {
    	
		super(isdebugEnabled);
		
		this.spy = spy;
		this.methodCall = methodCall;
		this.sql = sql;
		this.execTime = execTime;
    }
    
    /**
     * Populate the message attribute 
     * using the attributes of this class.
     * This method is called only when this Message is actually logged, 
     * avoiding useless concatenation costs, etc.
     * 
     * @see #message
     */
    protected void buildMessage()
    {
    	String tempMessage = "";
    	
    	String classType = this.spy.getClassType();
    	Integer spyNo = this.spy.getConnectionNumber();
    	String header = spyNo + ". " + classType + "." + this.methodCall;

    	if (this.sql == null) {
    		tempMessage = header;

    	} else {
    		String tempSql = this.processSql(this.sql);

    		// if at debug level, display debug info to error log
    		if (this.isDebugEnabled()) {
    			tempMessage = SqlMessage.getDebugInfo() + SqlMessage.nl + spyNo + ". " + tempSql;
    		} else {
    			tempMessage = header + " FAILED! " + tempSql;
    		}
    		if (this.execTime != -1) {
    			tempMessage += " {FAILED after " + execTime + " ms}";
    		}
    	}
    	
    	this.setMessage(tempMessage);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy