You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/11/30 02:39:19 UTC

[3/9] james-project git commit: JAMES-2233 Add request/response logging to webadmin

JAMES-2233 Add request/response logging to webadmin


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/449d3088
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/449d3088
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/449d3088

Branch: refs/heads/master
Commit: 449d30881ed437ffec70d495c03c5d5d26170382
Parents: fa760c8
Author: benwa <bt...@linagora.com>
Authored: Thu Nov 23 14:35:39 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:35:51 2017 +0700

----------------------------------------------------------------------
 .../apache/james/webadmin/WebAdminServer.java   |  4 ++
 .../webadmin/mdc/LoggingRequestFilter.java      | 45 +++++++++++++++++++
 .../webadmin/mdc/LoggingResponseFilter.java     | 47 ++++++++++++++++++++
 3 files changed, 96 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/449d3088/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
index d621d8c..e5a47b8 100644
--- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
@@ -29,6 +29,8 @@ import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.webadmin.authentication.AuthenticationFilter;
+import org.apache.james.webadmin.mdc.LoggingRequestFilter;
+import org.apache.james.webadmin.mdc.LoggingResponseFilter;
 import org.apache.james.webadmin.mdc.MDCCleanupFilter;
 import org.apache.james.webadmin.mdc.MDCFilter;
 import org.apache.james.webadmin.metric.MetricPostFilter;
@@ -80,6 +82,8 @@ public class WebAdminServer implements Configurable {
 
     private void configureMDC() {
         service.before(new MDCFilter());
+        service.before(new LoggingRequestFilter());
+        service.after(new LoggingResponseFilter());
         service.after(new MDCCleanupFilter());
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/449d3088/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
new file mode 100644
index 0000000..0087c7b
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
@@ -0,0 +1,45 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.webadmin.mdc;
+
+import java.io.Closeable;
+
+import org.apache.james.util.MDCBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import spark.Filter;
+import spark.Request;
+import spark.Response;
+
+public class LoggingRequestFilter implements Filter {
+    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
+    private static final String BODY = "body";
+
+    @Override
+    public void handle(Request request, Response response) throws Exception {
+        try (Closeable closeable =
+                 MDCBuilder.create()
+                     .addContext(BODY, request.body())
+                     .build()) {
+            LOGGER.info("Received request");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/449d3088/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
new file mode 100644
index 0000000..a72e71e
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
@@ -0,0 +1,47 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.webadmin.mdc;
+
+import java.io.Closeable;
+
+import org.apache.james.util.MDCBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import spark.Filter;
+import spark.Request;
+import spark.Response;
+
+public class LoggingResponseFilter implements Filter {
+    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingResponseFilter.class);
+    private static final String STATUS = "status";
+    private static final String BODY = "body";
+
+    @Override
+    public void handle(Request request, Response response) throws Exception {
+        try (Closeable closeable =
+                 MDCBuilder.create()
+                     .addContext(STATUS, response.status())
+                     .addContext(BODY, response.body())
+                     .build()) {
+            LOGGER.info("Received request");
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org