Skip to content

Commit

Permalink
TRUNK-3979: LoggingAdvice should also log which user is performing an…
Browse files Browse the repository at this point in the history
… action when an error occurs

Null check and taking systemId if username is empty

User null check

Fix of code duplication
  • Loading branch information
DraggonZ authored and wluyima committed May 15, 2013
1 parent 2ed08d1 commit f9804f5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions api/src/main/java/org/openmrs/aop/LoggingAdvice.java
Expand Up @@ -15,16 +15,16 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.User;
import org.openmrs.annotation.Logging;
import org.openmrs.api.context.Context;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.util.StringUtils;

/**
* This class provides the log4j aop around advice for our service layer. This advice is placed on
Expand Down Expand Up @@ -122,8 +122,20 @@ else if (logSetter)
return invocation.proceed();
}
catch (Throwable t) {
if (logGetter || logSetter)
log.error("An error occurred while executing this method. Error message: " + t.getMessage(), t);
if (logGetter || logSetter) {
String username;
User user = Context.getAuthenticatedUser();
if (user == null) {
username = "Guest (Not logged in)";
} else {
username = user.getUsername();
if (username == null || username.length() == 0)
username = user.getSystemId();
}
log.error(String.format(
"An error occurred while executing this method.\nCurrent user: %s\nError message: %s", username, t
.getMessage()), t);
}
throw t;
}
finally {
Expand Down

0 comments on commit f9804f5

Please sign in to comment.