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:20 UTC
[4/9] james-project git commit: JAMES-2233 Add structured logging API
and implementation based on MDC
JAMES-2233 Add structured logging API and implementation based on MDC
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1cd5c3b5
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1cd5c3b5
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1cd5c3b5
Branch: refs/heads/master
Commit: 1cd5c3b586dfafa81c12a42fb95f228b8d4bd551
Parents: 449d308
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 14:22:38 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:36:14 2017 +0700
----------------------------------------------------------------------
.../java/org/apache/james/util/MDCBuilder.java | 11 +++++
.../apache/james/util/MDCStructuredLogger.java | 50 ++++++++++++++++++++
.../org/apache/james/util/StructuredLogger.java | 30 ++++++++++++
3 files changed, 91 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/1cd5c3b5/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
index e728518..7490d41 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
@@ -39,6 +39,10 @@ import com.google.common.collect.ImmutableMap;
public class MDCBuilder {
+ public interface VoidOperation {
+ void perform();
+ }
+
public static <T> T withMdc(MDCBuilder mdcBuilder, Supplier<T> answerSupplier) {
try (Closeable closeable = mdcBuilder.build()) {
try {
@@ -52,6 +56,13 @@ public class MDCBuilder {
}
}
+ public static void withMdc(MDCBuilder mdcBuilder, VoidOperation logOperation) {
+ withMdc(mdcBuilder, () -> {
+ logOperation.perform();
+ return null;
+ });
+ }
+
public static final String HOST = "host";
public static final String IP = "ip";
public static final String PROTOCOL = "protocol";
http://git-wip-us.apache.org/repos/asf/james-project/blob/1cd5c3b5/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java b/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java
new file mode 100644
index 0000000..cf970ea
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java
@@ -0,0 +1,50 @@
+/****************************************************************
+ * 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.util;
+
+import java.util.function.Consumer;
+
+import org.slf4j.Logger;
+
+public class MDCStructuredLogger implements StructuredLogger {
+
+ public static MDCStructuredLogger forLogger(Logger logger) {
+ return new MDCStructuredLogger(logger);
+ }
+
+ private final Logger logger;
+ private final MDCBuilder mdcBuilder;
+
+ public MDCStructuredLogger(Logger logger) {
+ this.logger = logger;
+ this.mdcBuilder = MDCBuilder.create();
+ }
+
+ @Override
+ public StructuredLogger addField(String name, Object value) {
+ mdcBuilder.addContext(name, value);
+ return this;
+ }
+
+ @Override
+ public void log(Consumer<Logger> logOperation) {
+ MDCBuilder.withMdc(mdcBuilder, () -> logOperation.accept(logger));
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/1cd5c3b5/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java b/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java
new file mode 100644
index 0000000..f6fcec4
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java
@@ -0,0 +1,30 @@
+/****************************************************************
+ * 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.util;
+
+import java.util.function.Consumer;
+
+import org.slf4j.Logger;
+
+public interface StructuredLogger {
+ StructuredLogger addField(String name, Object value);
+
+ void log(Consumer<Logger> logOperation);
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org