diff options
author | David Pursehouse <dpursehouse@collab.net> | 2020-03-16 07:41:49 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2020-03-16 07:41:49 +0000 |
commit | 3a85a1a803fba3f85ca5aab5151521e4062c8199 (patch) | |
tree | dc443429d305e554d5e6449464d8d0e1fc0c7467 | |
parent | 1a83264828f382d67ab2323e8b0ca27652f746c2 (diff) | |
parent | a215f3ee45831c1b5da9984c05fcd64d0adb6552 (diff) |
Merge "Introduce NamedFluentLogger" into stable-2.16
-rw-r--r-- | java/com/google/gerrit/util/logging/BUILD | 1 | ||||
-rw-r--r-- | java/com/google/gerrit/util/logging/NamedFluentLogger.java | 84 | ||||
-rw-r--r-- | plugins/BUILD | 1 | ||||
m--------- | plugins/replication | 0 |
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 |