mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Add warnAndThrow and errorAndThrow methods, to slightly simplify catch-blocks that want to do both of those things; also add variant data in session portion of log, when available
This commit is contained in:
@ -22,6 +22,7 @@
|
|||||||
package com.kingsrook.qqq.backend.core.logging;
|
package com.kingsrook.qqq.backend.core.logging;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -147,6 +148,28 @@ public class QLogger
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public <T extends Throwable> T warnAndThrow(T t, LogPair... logPairs) throws T
|
||||||
|
{
|
||||||
|
warn(t.getMessage(), t, logPairs);
|
||||||
|
throw (t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public <T extends Throwable> T errorAndThrow(T t, LogPair... logPairs) throws T
|
||||||
|
{
|
||||||
|
error(t.getMessage(), t, logPairs);
|
||||||
|
throw (t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
@ -595,7 +618,10 @@ public class QLogger
|
|||||||
{
|
{
|
||||||
user = session.getUser().getIdReference();
|
user = session.getUser().getIdReference();
|
||||||
}
|
}
|
||||||
sessionLogPair = logPair("session", logPair("id", session.getUuid()), logPair("user", user));
|
|
||||||
|
LogPair variantsLogPair = getVariantsLogPair(session);
|
||||||
|
|
||||||
|
sessionLogPair = logPair("session", logPair("id", session.getUuid()), logPair("user", user), variantsLogPair);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -615,6 +641,38 @@ public class QLogger
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
private static LogPair getVariantsLogPair(QSession session)
|
||||||
|
{
|
||||||
|
LogPair variantsLogPair = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if(session.getBackendVariants() != null)
|
||||||
|
{
|
||||||
|
LogPair[] variants = new LogPair[session.getBackendVariants().size()];
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for(Map.Entry<String, Serializable> entry : session.getBackendVariants().entrySet())
|
||||||
|
{
|
||||||
|
variants[i] = new LogPair(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
variantsLogPair = new LogPair("variants", variants);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
////////////////
|
||||||
|
// leave null //
|
||||||
|
////////////////
|
||||||
|
}
|
||||||
|
return variantsLogPair;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
@ -43,6 +43,7 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -67,6 +68,50 @@ class QLoggerTest extends BaseTest
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testLogAndThrowMethods() throws QException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
LOG.info("Some info");
|
||||||
|
LOG.warnAndThrow(new QException("Something failed"), new LogPair("something", 1));
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
//////////////
|
||||||
|
// ok, done //
|
||||||
|
//////////////
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThatThrownBy(() ->
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
methodThatThrows();
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
throw LOG.errorAndThrow(new QException("I caught, now i errorAndThrow", e), new LogPair("iLove", "logPairs"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).isInstanceOf(QException.class).hasMessageContaining("I caught").rootCause().hasMessageContaining("See, I throw");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
private void methodThatThrows() throws QException
|
||||||
|
{
|
||||||
|
throw (new QException("See, I throw"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
Reference in New Issue
Block a user