2 * Copyright 2017-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.utils.logging;
18 import com.google.common.base.MoreObjects;
19 import com.google.errorprone.annotations.CanIgnoreReturnValue;
21 import java.util.function.Supplier;
26 public class LoggerContext {
29 * Returns a new contextual logger builder.
31 * @param name the logger name
32 * @return the logger builder
34 public static Builder builder(String name) {
35 return new Builder(name);
39 * Returns a new contextual logger builder.
41 * @param clazz the logger class
42 * @return the logger builder
44 public static Builder builder(Class clazz) {
45 return new Builder(clazz.getSimpleName());
48 private final Supplier<String> stringProvider;
50 public LoggerContext(Supplier<String> stringProvider) {
51 this.stringProvider = stringProvider;
55 public String toString() {
56 return stringProvider.get();
60 * Contextual logger builder.
62 public static class Builder implements io.atomix.utils.Builder<LoggerContext> {
63 private final MoreObjects.ToStringHelper identityStringHelper;
64 private MoreObjects.ToStringHelper argsStringHelper;
65 private boolean omitNullValues = false;
67 public Builder(String name) {
68 this.identityStringHelper = MoreObjects.toStringHelper(name);
72 * Initializes the arguments string helper.
74 private void initializeArgs() {
75 if (argsStringHelper == null) {
76 argsStringHelper = MoreObjects.toStringHelper("");
81 * Configures the {@link MoreObjects.ToStringHelper} so {@link #toString()} will ignore properties with null
82 * value. The order of calling this method, relative to the {@code add()}/{@code addValue()}
83 * methods, is not significant.
86 public Builder omitNullValues() {
87 this.omitNullValues = true;
92 * Adds a name/value pair to the formatted output in {@code name=value} format. If {@code value}
93 * is {@code null}, the string {@code "null"} is used, unless {@link #omitNullValues()} is
94 * called, in which case this name/value pair will not be added.
97 public Builder add(String name, Object value) {
99 argsStringHelper.add(name, value);
104 * Adds a name/value pair to the formatted output in {@code name=value} format.
106 @CanIgnoreReturnValue
107 public Builder add(String name, boolean value) {
109 argsStringHelper.add(name, value);
114 * Adds a name/value pair to the formatted output in {@code name=value} format.
116 @CanIgnoreReturnValue
117 public Builder add(String name, char value) {
119 argsStringHelper.add(name, value);
124 * Adds a name/value pair to the formatted output in {@code name=value} format.
126 @CanIgnoreReturnValue
127 public Builder add(String name, double value) {
129 argsStringHelper.add(name, value);
134 * Adds a name/value pair to the formatted output in {@code name=value} format.
136 @CanIgnoreReturnValue
137 public Builder add(String name, float value) {
139 argsStringHelper.add(name, value);
144 * Adds a name/value pair to the formatted output in {@code name=value} format.
146 @CanIgnoreReturnValue
147 public Builder add(String name, int value) {
149 argsStringHelper.add(name, value);
154 * Adds a name/value pair to the formatted output in {@code name=value} format.
156 @CanIgnoreReturnValue
157 public Builder add(String name, long value) {
159 argsStringHelper.add(name, value);
164 * Adds an unnamed value to the formatted output.
166 * <p>It is strongly encouraged to use {@link #add(String, Object)} instead and give value a
169 @CanIgnoreReturnValue
170 public Builder addValue(Object value) {
171 identityStringHelper.addValue(value);
176 * Adds an unnamed value to the formatted output.
178 * <p>It is strongly encouraged to use {@link #add(String, boolean)} instead and give value a
181 @CanIgnoreReturnValue
182 public Builder addValue(boolean value) {
183 identityStringHelper.addValue(value);
188 * Adds an unnamed value to the formatted output.
190 * <p>It is strongly encouraged to use {@link #add(String, char)} instead and give value a
193 @CanIgnoreReturnValue
194 public Builder addValue(char value) {
195 identityStringHelper.addValue(value);
200 * Adds an unnamed value to the formatted output.
202 * <p>It is strongly encouraged to use {@link #add(String, double)} instead and give value a
205 @CanIgnoreReturnValue
206 public Builder addValue(double value) {
207 identityStringHelper.addValue(value);
212 * Adds an unnamed value to the formatted output.
214 * <p>It is strongly encouraged to use {@link #add(String, float)} instead and give value a
217 @CanIgnoreReturnValue
218 public Builder addValue(float value) {
219 identityStringHelper.addValue(value);
224 * Adds an unnamed value to the formatted output.
226 * <p>It is strongly encouraged to use {@link #add(String, int)} instead and give value a
229 @CanIgnoreReturnValue
230 public Builder addValue(int value) {
231 identityStringHelper.addValue(value);
236 * Adds an unnamed value to the formatted output.
238 * <p>It is strongly encouraged to use {@link #add(String, long)} instead and give value a
241 @CanIgnoreReturnValue
242 public Builder addValue(long value) {
243 identityStringHelper.addValue(value);
248 public LoggerContext build() {
249 MoreObjects.ToStringHelper identityStringHelper = this.identityStringHelper;
250 MoreObjects.ToStringHelper argsStringHelper = this.argsStringHelper;
251 if (omitNullValues) {
252 identityStringHelper.omitNullValues();
253 if (argsStringHelper != null) {
254 argsStringHelper.omitNullValues();
257 return new LoggerContext(() -> {
258 if (argsStringHelper == null) {
259 return identityStringHelper.toString();
261 return identityStringHelper.toString() + argsStringHelper.toString();