Import atomix/{storage,utils}
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / logging / LoggerContext.java
1 /*
2  * Copyright 2017-present Open Networking Foundation
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.atomix.utils.logging;
17
18 import com.google.common.base.MoreObjects;
19 import com.google.errorprone.annotations.CanIgnoreReturnValue;
20
21 import java.util.function.Supplier;
22
23 /**
24  * Logger context.
25  */
26 public class LoggerContext {
27
28   /**
29    * Returns a new contextual logger builder.
30    *
31    * @param name the logger name
32    * @return the logger builder
33    */
34   public static Builder builder(String name) {
35     return new Builder(name);
36   }
37
38   /**
39    * Returns a new contextual logger builder.
40    *
41    * @param clazz the logger class
42    * @return the logger builder
43    */
44   public static Builder builder(Class clazz) {
45     return new Builder(clazz.getSimpleName());
46   }
47
48   private final Supplier<String> stringProvider;
49
50   public LoggerContext(Supplier<String> stringProvider) {
51     this.stringProvider = stringProvider;
52   }
53
54   @Override
55   public String toString() {
56     return stringProvider.get();
57   }
58
59   /**
60    * Contextual logger builder.
61    */
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;
66
67     public Builder(String name) {
68       this.identityStringHelper = MoreObjects.toStringHelper(name);
69     }
70
71     /**
72      * Initializes the arguments string helper.
73      */
74     private void initializeArgs() {
75       if (argsStringHelper == null) {
76         argsStringHelper = MoreObjects.toStringHelper("");
77       }
78     }
79
80     /**
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.
84      */
85     @CanIgnoreReturnValue
86     public Builder omitNullValues() {
87       this.omitNullValues = true;
88       return this;
89     }
90
91     /**
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.
95      */
96     @CanIgnoreReturnValue
97     public Builder add(String name, Object value) {
98       initializeArgs();
99       argsStringHelper.add(name, value);
100       return this;
101     }
102
103     /**
104      * Adds a name/value pair to the formatted output in {@code name=value} format.
105      */
106     @CanIgnoreReturnValue
107     public Builder add(String name, boolean value) {
108       initializeArgs();
109       argsStringHelper.add(name, value);
110       return this;
111     }
112
113     /**
114      * Adds a name/value pair to the formatted output in {@code name=value} format.
115      */
116     @CanIgnoreReturnValue
117     public Builder add(String name, char value) {
118       initializeArgs();
119       argsStringHelper.add(name, value);
120       return this;
121     }
122
123     /**
124      * Adds a name/value pair to the formatted output in {@code name=value} format.
125      */
126     @CanIgnoreReturnValue
127     public Builder add(String name, double value) {
128       initializeArgs();
129       argsStringHelper.add(name, value);
130       return this;
131     }
132
133     /**
134      * Adds a name/value pair to the formatted output in {@code name=value} format.
135      */
136     @CanIgnoreReturnValue
137     public Builder add(String name, float value) {
138       initializeArgs();
139       argsStringHelper.add(name, value);
140       return this;
141     }
142
143     /**
144      * Adds a name/value pair to the formatted output in {@code name=value} format.
145      */
146     @CanIgnoreReturnValue
147     public Builder add(String name, int value) {
148       initializeArgs();
149       argsStringHelper.add(name, value);
150       return this;
151     }
152
153     /**
154      * Adds a name/value pair to the formatted output in {@code name=value} format.
155      */
156     @CanIgnoreReturnValue
157     public Builder add(String name, long value) {
158       initializeArgs();
159       argsStringHelper.add(name, value);
160       return this;
161     }
162
163     /**
164      * Adds an unnamed value to the formatted output.
165      *
166      * <p>It is strongly encouraged to use {@link #add(String, Object)} instead and give value a
167      * readable name.
168      */
169     @CanIgnoreReturnValue
170     public Builder addValue(Object value) {
171       identityStringHelper.addValue(value);
172       return this;
173     }
174
175     /**
176      * Adds an unnamed value to the formatted output.
177      *
178      * <p>It is strongly encouraged to use {@link #add(String, boolean)} instead and give value a
179      * readable name.
180      */
181     @CanIgnoreReturnValue
182     public Builder addValue(boolean value) {
183       identityStringHelper.addValue(value);
184       return this;
185     }
186
187     /**
188      * Adds an unnamed value to the formatted output.
189      *
190      * <p>It is strongly encouraged to use {@link #add(String, char)} instead and give value a
191      * readable name.
192      */
193     @CanIgnoreReturnValue
194     public Builder addValue(char value) {
195       identityStringHelper.addValue(value);
196       return this;
197     }
198
199     /**
200      * Adds an unnamed value to the formatted output.
201      *
202      * <p>It is strongly encouraged to use {@link #add(String, double)} instead and give value a
203      * readable name.
204      */
205     @CanIgnoreReturnValue
206     public Builder addValue(double value) {
207       identityStringHelper.addValue(value);
208       return this;
209     }
210
211     /**
212      * Adds an unnamed value to the formatted output.
213      *
214      * <p>It is strongly encouraged to use {@link #add(String, float)} instead and give value a
215      * readable name.
216      */
217     @CanIgnoreReturnValue
218     public Builder addValue(float value) {
219       identityStringHelper.addValue(value);
220       return this;
221     }
222
223     /**
224      * Adds an unnamed value to the formatted output.
225      *
226      * <p>It is strongly encouraged to use {@link #add(String, int)} instead and give value a
227      * readable name.
228      */
229     @CanIgnoreReturnValue
230     public Builder addValue(int value) {
231       identityStringHelper.addValue(value);
232       return this;
233     }
234
235     /**
236      * Adds an unnamed value to the formatted output.
237      *
238      * <p>It is strongly encouraged to use {@link #add(String, long)} instead and give value a
239      * readable name.
240      */
241     @CanIgnoreReturnValue
242     public Builder addValue(long value) {
243       identityStringHelper.addValue(value);
244       return this;
245     }
246
247     @Override
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();
255         }
256       }
257       return new LoggerContext(() -> {
258         if (argsStringHelper == null) {
259           return identityStringHelper.toString();
260         } else {
261           return identityStringHelper.toString() + argsStringHelper.toString();
262         }
263       });
264     }
265   }
266 }