1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
package com.mycompany.myapp.server.utilities.logging;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
@Aspect public class ServerSideLogger
implements Ordered {
public ServerSideLogger() {}
@After("com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()")
public void logOperationEnd(JoinPoint logged) {
getLog(logged).info("End of operation " + getLoggedOperationDescription(logged));
}
@AfterThrowing(
pointcut = "com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()",
throwing = "exception"
)
public void logOperationFailure(JoinPoint logged,
RuntimeException exception) {
Logger log = getLog(logged);
log.warn("Exception was thrown in " + getLoggedOperationDescription(logged));
if (log.isDebugEnabled()) {
log.debug(exception.getMessage(), exception);
}
}
@Before("com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()")
public void logOperationStart(JoinPoint logged) {
getLog(logged).info("Entering operation " + getLoggedOperationDescription(logged));
}
@AfterReturning(
pointcut = "com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()",
returning = "returnValue"
)
public void logOperationSuccess(JoinPoint logged,
Object returnValue) {
Logger log = getLog(logged);
log.info("Successful operation " + getLoggedOperationDescription(logged));
if (log.isTraceEnabled()) {
log.trace("Operation returned: " + returnValue);
}
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
private Logger getLog(JoinPoint logged) {
return Logger.getLogger(logged.getTarget().getClass());
}
private String getLoggedOperationDescription(JoinPoint logged) {
return logged.toLongString();
}
private int order;
} |
Partager