summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacek Centkowski <jcentkowski@collab.net>2020-03-10 20:25:33 +0100
committerDavid Pursehouse <dpursehouse@collab.net>2020-03-16 15:48:44 +0900
commita215f3ee45831c1b5da9984c05fcd64d0adb6552 (patch)
treedc443429d305e554d5e6449464d8d0e1fc0c7467
parent1a83264828f382d67ab2323e8b0ca27652f746c2 (diff)
Introduce NamedFluentLogger
Regardless of its flexibility, FluentLogger doesn't provide an API to create a logger with predefined name, which is possible with slf4j, for example: Logger logger = LoggerFactory.getLogger("my_logger_name"); This is used for example in the replication plugin, and the GC logger, which both create a named log and set up a specific appender that uses it. It is also possible that there are other plugins that need to make use of it during migration to Flogger. For example, CollabNet has an internal plugin that would need it. A proposal to add such an API [1] was not accepted, with suggestion for a more advanced solution. Since it's unlikely that this will happen soon, we add a simple solution that can be used within core Gerrit and its plugins: NamedFluentLogger logger = NamedFluentLogger.forName("my_logger_name"); In the long term, after a suitable API is added in FluentLogger, we can remove this. [1] https://github.com/google/flogger/pull/138 Change-Id: Ieb914961bc1133d74684ecc39cae86c2e693daf9
-rw-r--r--java/com/google/gerrit/util/logging/BUILD1
-rw-r--r--java/com/google/gerrit/util/logging/NamedFluentLogger.java84
-rw-r--r--plugins/BUILD1
m---------plugins/replication0
4 files changed, 86 insertions, 0 deletions
diff --git a/java/com/google/gerrit/util/logging/BUILD b/java/com/google/gerrit/util/logging/BUILD
index b8db49bdc6..ee598a436f 100644
--- a/java/com/google/gerrit/util/logging/BUILD
+++ b/java/com/google/gerrit/util/logging/BUILD
@@ -8,6 +8,7 @@ java_library(
visibility = ["//visibility:public"],
deps = [
"//lib:gson",
+ "//lib/flogger:api",
"//lib/log:log4j",
],
)
diff --git a/java/com/google/gerrit/util/logging/NamedFluentLogger.java b/java/com/google/gerrit/util/logging/NamedFluentLogger.java
new file mode 100644
index 0000000000..04fc18df74
--- /dev/null
+++ b/java/com/google/gerrit/util/logging/NamedFluentLogger.java
@@ -0,0 +1,84 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed 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 com.google.gerrit.util.logging;
+
+import com.google.common.flogger.AbstractLogger;
+import com.google.common.flogger.LogContext;
+import com.google.common.flogger.LoggingApi;
+import com.google.common.flogger.backend.LoggerBackend;
+import com.google.common.flogger.backend.Platform;
+import com.google.common.flogger.parser.DefaultPrintfMessageParser;
+import com.google.common.flogger.parser.MessageParser;
+import java.util.logging.Level;
+
+/**
+ * FluentLogger.forEnclosingClass() searches for caller class name and passes it as String to
+ * constructor FluentLogger.FluentLogger(LoggerBackend) (which is package protected).
+ *
+ * <p>This allows to create NamedFluentLogger with given name so that dedicated configuration can be
+ * specified by a custom appender in the log4j.properties file. An example of this is the logger
+ * used by the replication queue in the replication plugin, and gerrit's Garbage Collection log.
+ */
+public class NamedFluentLogger extends AbstractLogger<NamedFluentLogger.Api> {
+ /** Copied from FluentLogger */
+ public interface Api extends LoggingApi<Api> {}
+
+ /** Copied from FluentLogger */
+ private static final class NoOp extends LoggingApi.NoOp<Api> implements Api {}
+
+ private static final NoOp NO_OP = new NoOp();
+
+ public static NamedFluentLogger forName(String name) {
+ return new NamedFluentLogger(Platform.getBackend(name));
+ }
+
+ private NamedFluentLogger(LoggerBackend backend) {
+ super(backend);
+ }
+
+ @Override
+ public Api at(Level level) {
+ boolean isLoggable = isLoggable(level);
+ boolean isForced = Platform.shouldForceLogging(getName(), level, isLoggable);
+ return (isLoggable || isForced) ? new Context(level, isForced) : NO_OP;
+ }
+
+ /** Copied from FluentLogger */
+ private final class Context extends LogContext<NamedFluentLogger, Api> implements Api {
+ private Context(Level level, boolean isForced) {
+ super(level, isForced);
+ }
+
+ @Override
+ protected NamedFluentLogger getLogger() {
+ return NamedFluentLogger.this;
+ }
+
+ @Override
+ protected Api api() {
+ return this;
+ }
+
+ @Override
+ protected Api noOp() {
+ return NO_OP;
+ }
+
+ @Override
+ protected MessageParser getMessageParser() {
+ return DefaultPrintfMessageParser.getInstance();
+ }
+ }
+}
diff --git a/plugins/BUILD b/plugins/BUILD
index 7d0a2ede57..a33ce56b56 100644
--- a/plugins/BUILD
+++ b/plugins/BUILD
@@ -50,6 +50,7 @@ EXPORTS = [
"//java/com/google/gerrit/server/util/time",
"//java/com/google/gerrit/util/cli",
"//java/com/google/gerrit/util/http",
+ "//java/com/google/gerrit/util/logging",
"//lib/commons:compress",
"//lib/commons:dbcp",
"//lib/commons:lang",
diff --git a/plugins/replication b/plugins/replication
-Subproject 04bbb43e28f14b61412b71ecae19921ab67d62c
+Subproject 425f4f20218986615593da1a9319466743ee12e