summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/util/logging/NamedFluentLogger.java
blob: 04fc18df74dda24e114468113770d3a7440a81fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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();
    }
  }
}