Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added trace log interceptor.

  • Loading branch information...
commit 893ff6984b19c09ac9aa98b2db23a9e066a77080 1 parent fed25f2
@ryoasai authored
View
2  .../jsf2/demo/scrum/infra/context/LoggerFactory.java → .../jsf2/demo/scrum/infra/logging/LoggerFactory.java
@@ -2,7 +2,7 @@
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
-package jsf2.demo.scrum.infra.context;
+package jsf2.demo.scrum.infra.logging;
import java.util.logging.Logger;
import javax.enterprise.inject.Produces;
View
65 src/main/java/jsf2/demo/scrum/infra/logging/TraceLogInterceptor.java
@@ -0,0 +1,65 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package jsf2.demo.scrum.infra.logging;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.inject.Inject;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+
+/**
+ *
+ * @author Ryo
+ */
+@Traced @Interceptor
+public class TraceLogInterceptor implements Serializable {
+
+ @Inject
+ transient Logger logger;
+
+ @AroundInvoke
+ public Object trace(InvocationContext ic) throws Exception {
+
+ long start = 0;
+ long end;
+ Method method = ic.getMethod();
+ Object[] params = ic.getParameters();
+
+ try {
+ if (params == null || params.length == 0) {
+ logger.log(Level.INFO, "Start to execute {0}", method);
+ } else {
+ logger.log(Level.INFO, "Start to execute {0}, params = {1}", new Object[]{method, Arrays.asList(params)});
+ }
+
+ start = System.nanoTime();
+ Object result = ic.proceed();
+ end = System.nanoTime();
+ if (method.getReturnType() == void.class) {
+ logger.log(Level.INFO, "Execution of {0} normally in {1}", new Object[]{method, formatTimeInMs(start, end)});
+ } else {
+ logger.log(Level.INFO, "Execution of {0} normally in {1}, result = {2}", new Object[]{method, formatTimeInMs(start, end), result});
+ }
+
+ return result;
+
+ } catch (Exception ex) {
+ end = System.nanoTime();
+ logger.log(Level.INFO, "Execution of {0} failed in {1}", new Object[]{method, formatTimeInMs(start, end)});
+ logger.log(Level.SEVERE, "Exception was:", ex);
+
+ throw ex;
+ }
+ }
+
+ protected String formatTimeInMs(long start, long end) {
+ return String.format("%,d ms", (end - start) / 1000);
+ }
+}
View
24 src/main/java/jsf2/demo/scrum/infra/logging/Traced.java
@@ -0,0 +1,24 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package jsf2.demo.scrum.infra.logging;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.interceptor.InterceptorBinding;
+
+/**
+ *
+ * @author Ryo
+ */
+@Inherited
+@InterceptorBinding
+@Retention(RUNTIME)
+@Target({METHOD, TYPE})
+public @interface Traced {
+}
View
6 src/main/java/jsf2/demo/scrum/infra/web/controller/AbstractAction.java
@@ -38,7 +38,6 @@
*/
package jsf2.demo.scrum.infra.web.controller;
-import java.io.Serializable;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -50,14 +49,13 @@
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import javax.inject.Inject;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import jsf2.demo.scrum.infra.entity.AbstractEntity;
+import jsf2.demo.scrum.infra.logging.Traced;
/**
*
* @author Dr. Spock (spock at dev.java.net)
*/
+@Traced
public abstract class AbstractAction {
@Inject
View
4 src/main/webapp/WEB-INF/beans.xml
@@ -2,4 +2,8 @@
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+
+ <interceptors>
+ <class>jsf2.demo.scrum.infra.logging.TraceLogInterceptor</class>
+ </interceptors>
</beans>
Please sign in to comment.
Something went wrong with that request. Please try again.