mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-20 06:00:44 +00:00
Merge pull request #20 from Kingsrook/feature/only-log-warnings-and-errors-once
initial version of attempting to downgrade logs if a warning or error…
This commit is contained in:
@ -136,7 +136,7 @@ public class QValueFormatter
|
||||
{
|
||||
return formatValue(displayFormat, ValueUtils.getValueAsBigDecimal(value));
|
||||
}
|
||||
else if(e.getMessage().equals("d != java.math.BigDecimal"))
|
||||
else if(e.getMessage().equals("d != java.math.BigDecimal") || e.getMessage().equals("d != java.lang.String"))
|
||||
{
|
||||
return formatValue(displayFormat, ValueUtils.getValueAsInteger(value));
|
||||
}
|
||||
|
@ -22,12 +22,19 @@
|
||||
package com.kingsrook.qqq.backend.core.exceptions;
|
||||
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* Base class for checked exceptions thrown in qqq.
|
||||
*
|
||||
*******************************************************************************/
|
||||
public class QException extends Exception
|
||||
{
|
||||
private boolean hasLoggedWarning;
|
||||
private boolean hasLoggedError;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Constructor of message
|
||||
@ -59,4 +66,102 @@ public class QException extends Exception
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for hasLoggedWarning
|
||||
*******************************************************************************/
|
||||
public boolean getHasLoggedWarning()
|
||||
{
|
||||
return (this.hasLoggedWarning);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for hasLoggedWarning
|
||||
*******************************************************************************/
|
||||
public void setHasLoggedWarning(boolean hasLoggedWarning)
|
||||
{
|
||||
this.hasLoggedWarning = hasLoggedWarning;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for hasLoggedWarning
|
||||
*******************************************************************************/
|
||||
public QException withHasLoggedWarning(boolean hasLoggedWarning)
|
||||
{
|
||||
this.hasLoggedWarning = hasLoggedWarning;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for hasLoggedError
|
||||
*******************************************************************************/
|
||||
public boolean getHasLoggedError()
|
||||
{
|
||||
return (this.hasLoggedError);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for hasLoggedError
|
||||
*******************************************************************************/
|
||||
public void setHasLoggedError(boolean hasLoggedError)
|
||||
{
|
||||
this.hasLoggedError = hasLoggedError;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for hasLoggedError
|
||||
*******************************************************************************/
|
||||
public QException withHasLoggedError(boolean hasLoggedError)
|
||||
{
|
||||
this.hasLoggedError = hasLoggedError;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** helper function for getting if level logged
|
||||
*******************************************************************************/
|
||||
public boolean hasLoggedLevel(Level level)
|
||||
{
|
||||
if(Level.WARN.equals(level))
|
||||
{
|
||||
return (hasLoggedWarning);
|
||||
}
|
||||
if(Level.ERROR.equals(level))
|
||||
{
|
||||
return (hasLoggedError);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** helper function for setting if level logged
|
||||
*******************************************************************************/
|
||||
public void setHasLoggedLevel(Level level)
|
||||
{
|
||||
if(Level.WARN.equals(level))
|
||||
{
|
||||
setHasLoggedWarning(true);
|
||||
}
|
||||
if(Level.ERROR.equals(level))
|
||||
{
|
||||
setHasLoggedError(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -29,10 +29,12 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.instances.QMetaDataVariableInterpreter;
|
||||
import com.kingsrook.qqq.backend.core.model.session.QSession;
|
||||
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
|
||||
import com.kingsrook.qqq.backend.core.utils.ExceptionUtils;
|
||||
import com.kingsrook.qqq.backend.core.utils.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -392,7 +394,7 @@ public class QLogger
|
||||
*******************************************************************************/
|
||||
public void warn(String message, Throwable t)
|
||||
{
|
||||
logger.warn(makeJsonString(message, t));
|
||||
logger.log(determineIfShouldDowngrade(t, Level.WARN), makeJsonString(message, t));
|
||||
}
|
||||
|
||||
|
||||
@ -402,7 +404,7 @@ public class QLogger
|
||||
*******************************************************************************/
|
||||
public void warn(String message, Throwable t, LogPair... logPairs)
|
||||
{
|
||||
logger.warn(makeJsonString(message, t, logPairs));
|
||||
logger.log(determineIfShouldDowngrade(t, Level.WARN), makeJsonString(message, t, logPairs));
|
||||
}
|
||||
|
||||
|
||||
@ -412,7 +414,7 @@ public class QLogger
|
||||
*******************************************************************************/
|
||||
public void warn(Throwable t)
|
||||
{
|
||||
logger.warn(makeJsonString(null, t));
|
||||
logger.log(determineIfShouldDowngrade(t, Level.WARN), makeJsonString(null, t));
|
||||
}
|
||||
|
||||
|
||||
@ -452,7 +454,7 @@ public class QLogger
|
||||
*******************************************************************************/
|
||||
public void error(String message, Throwable t)
|
||||
{
|
||||
logger.error(makeJsonString(message, t));
|
||||
logger.log(determineIfShouldDowngrade(t, Level.ERROR), makeJsonString(message, t));
|
||||
}
|
||||
|
||||
|
||||
@ -462,7 +464,7 @@ public class QLogger
|
||||
*******************************************************************************/
|
||||
public void error(String message, Throwable t, LogPair... logPairs)
|
||||
{
|
||||
logger.error(makeJsonString(message, t, logPairs));
|
||||
logger.log(determineIfShouldDowngrade(t, Level.ERROR), makeJsonString(message, t, logPairs));
|
||||
}
|
||||
|
||||
|
||||
@ -472,7 +474,7 @@ public class QLogger
|
||||
*******************************************************************************/
|
||||
public void error(Throwable t)
|
||||
{
|
||||
logger.error(makeJsonString(null, t));
|
||||
logger.log(determineIfShouldDowngrade(t, Level.ERROR), makeJsonString(null, t));
|
||||
}
|
||||
|
||||
|
||||
@ -532,7 +534,7 @@ public class QLogger
|
||||
|
||||
if(t != null)
|
||||
{
|
||||
logPairList.add(logPair("stackTrace", LogUtils.filterStackTrace(ExceptionUtils.getStackTrace(t))));
|
||||
logPairList.add(logPair("stackTrace", LogUtils.filterStackTrace(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(t))));
|
||||
}
|
||||
|
||||
return (LogUtils.jsonLog(logPairList));
|
||||
@ -582,4 +584,40 @@ public class QLogger
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
protected Level determineIfShouldDowngrade(Throwable t, Level level)
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
// look for QExceptions in the chain, if none found, return the log level passed in //
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
List<QException> exceptionList = ExceptionUtils.getClassListFromRootChain(t, QException.class);
|
||||
if(CollectionUtils.nullSafeIsEmpty(exceptionList))
|
||||
{
|
||||
return (level);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// check if any QException in this chain to see if it has already //
|
||||
// logged this level, if so, downgrade to INFO //
|
||||
////////////////////////////////////////////////////////////////////
|
||||
for(QException qException : exceptionList)
|
||||
{
|
||||
if(qException.hasLoggedLevel(level))
|
||||
{
|
||||
log(Level.DEBUG, "Downgrading log message from " + level.toString() + " to " + Level.INFO, t);
|
||||
return (Level.INFO);
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// if it has not logged at this level, set that it has in QException, and return passed in level //
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
exceptionList.get(0).setHasLoggedLevel(level);
|
||||
return (level);
|
||||
}
|
||||
}
|
||||
|
@ -62,6 +62,45 @@ public class ExceptionUtils
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Find a list of exceptions of the given class in an exception's caused-by chain.
|
||||
** Returns empty list if none found.
|
||||
**
|
||||
*******************************************************************************/
|
||||
public static <T extends Throwable> List<T> getClassListFromRootChain(Throwable e, Class<T> targetClass)
|
||||
{
|
||||
List<T> throwableList = new ArrayList<>();
|
||||
if(targetClass.isInstance(e))
|
||||
{
|
||||
throwableList.add(targetClass.cast(e));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
// iterate through the chain with a limit of 100 //
|
||||
///////////////////////////////////////////////////
|
||||
int counter = 0;
|
||||
while(counter++ < 100)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// look for the same class from the last throwable found of that type //
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
e = findClassInRootChain(e.getCause(), targetClass);
|
||||
if(e == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// if we did not break, higher one must have been found, keep looking //
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
throwableList.add(targetClass.cast(e));
|
||||
}
|
||||
|
||||
return (throwableList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Get the root exception in a caused-by-chain.
|
||||
**
|
||||
|
Reference in New Issue
Block a user