[ 
https://issues.apache.org/jira/browse/LOG4J2-2428?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Andrei Ivanov updated LOG4J2-2428:
----------------------------------
    Description: 
Since I don't want the audit exceptions to stop the execution of the 
application code, I've tried defining a simple {{AuditExceptionHandler}} that 
just logs the exceptions.

The {{AuditExceptionHandler}} is invoked currently only to handle exceptions 
caused by the {{logEvent}} method, but it would be nice to use it for the 
validation exceptions as well.

Otherwise my logging code looks like this:
{code:java}
try {
        logCashPaymentOut(result);
} catch (Exception e) {
        // log exception here
}

 private void logCashPaymentOut(SalesTransaction salesTransaction) {
         CashPaymentOut event = LogEventFactory.getEvent(CashPaymentOut.class);
        event.setSalesTransaction(salesTransaction.getUuid());
        event.setAmount(salesTransaction.getReturn().getAmount());
        event.setCurrency(salesTransaction.getReturn().getCurrency().name());
        event.setCurrency("Cash return");
        event.logEvent();
}
{code}
And I want to get rid of the try/catch blocks that are now needed because the 
setters can trigger exceptions too.

  was:
Since I don't want the audit exceptions to stop the execution of the 
application code, I've tried defining a simple {{AuditExceptionHandler}} that 
just logs the exceptions.

The {{AuditExceptionHandler}} is invoked currently only to handle exceptions 
caused by the {{logEvent}} method, but it would be nice to use it for the 
validation exceptions as well.

Otherwise my logging code looks like this:
{code:java}
try {
        logCashPaymentOut(result);
} catch (Exception e) {
        // log exception here
}

 private void logCashPaymentOut(SalesTransaction salesTransaction) {
         CashPaymentOut event = LogEventFactory.getEvent(CashPaymentOut.class);
        event.setSalesTransaction(salesTransaction.getUuid());
        event.setAmount(salesTransaction.getReturn().getAmount());
        event.setCurrency(salesTransaction.getReturn().getCurrency().name());
        event.setCurrency("Cash return");
        event.logEvent();
}
{code}
And I want to get rid of the try/catch blocks that are now needed because the 
setters can trigger exceptions too.

P.S.
Setting the {{exceptionHandler}} on the {{AuditEvent}} triggers [this 
code|https://github.com/apache/logging-log4j-audit/blob/master/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java#L306],
 but it falls through after the set and ends up at line 315 and sets it as a 
{{ThreadContext}} variable and ends up getting logged 🤦‍♂️
{noformat}
[AuditLogger] - Audit [cashPaymentOut amount="0.500" 
auditExceptionHandler="AuditEventLogger$$Lambda$137/1968918278@54de299" 
currency="CHF" reason="Cash return" 
salesTransaction="3b974661-d8ed-481d-b2fe-5f2979b40fa9"]
{noformat}

P.S. 2 I've discovered that even though {{LogEventFactory}} has a 
{{defaultExceptionHandler}} property, it doesn't set it on the events it 
creates through {{getEvent}}, so I've created my own implementation of 
{{LogEventFactory}}.
Can this be changed?

P.S. 3 Should the signature of {{LogEventFactory.getEvent}} be changed like 
this?
{code:java}
public static <T extends AuditEvent> T getEvent(Class<T> intrface)
{code}


> Use the AuditExceptionHandler for validation exceptions
> -------------------------------------------------------
>
>                 Key: LOG4J2-2428
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2428
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: Log4j-Audit
>    Affects Versions: Log4j-Audit 1.0.0
>            Reporter: Andrei Ivanov
>            Priority: Major
>
> Since I don't want the audit exceptions to stop the execution of the 
> application code, I've tried defining a simple {{AuditExceptionHandler}} that 
> just logs the exceptions.
> The {{AuditExceptionHandler}} is invoked currently only to handle exceptions 
> caused by the {{logEvent}} method, but it would be nice to use it for the 
> validation exceptions as well.
> Otherwise my logging code looks like this:
> {code:java}
> try {
>         logCashPaymentOut(result);
> } catch (Exception e) {
>         // log exception here
> }
>  private void logCashPaymentOut(SalesTransaction salesTransaction) {
>          CashPaymentOut event = 
> LogEventFactory.getEvent(CashPaymentOut.class);
>         event.setSalesTransaction(salesTransaction.getUuid());
>         event.setAmount(salesTransaction.getReturn().getAmount());
>         event.setCurrency(salesTransaction.getReturn().getCurrency().name());
>         event.setCurrency("Cash return");
>         event.logEvent();
> }
> {code}
> And I want to get rid of the try/catch blocks that are now needed because the 
> setters can trigger exceptions too.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to