001/*
002 * PermissionsEx
003 * Copyright (C) zml and PermissionsEx contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ca.stellardrift.permissionsex.logging;
018
019import net.kyori.adventure.text.Component;
020import net.kyori.adventure.text.serializer.ComponentSerializer;
021import org.checkerframework.checker.nullness.qual.Nullable;
022import org.slf4j.Logger;
023import org.slf4j.Marker;
024
025import java.util.Locale;
026
027public interface FormattedLogger extends Logger {
028
029    /**
030     * Get the language that will be used for logging translatable messages.
031     *
032     * @return the locale
033     * @since 2.0.0
034     */
035    Locale logLocale();
036
037    /**
038     * Get the language that will be used to log a specific {@link Marker}.
039     *
040     * @param marker the marker to log
041     * @return the marker
042     * @since 2.0.0
043     */
044    Locale logLocale(final @Nullable Marker marker);
045
046    @Nullable String prefix();
047
048    void prefix(@Nullable String prefix);
049
050    ComponentSerializer<Component, ?, String> serializer();
051
052    default String formatText(Component component) {
053        return formatText(component, null);
054    }
055
056    String formatText(final Component component, final @Nullable Marker marker);
057
058    /**
059     * Log a message at the TRACE level.
060     *
061     * @param msg the translatable message to be logged
062     * @since 2.0.0
063     */
064    default void trace(Component msg) {
065        if (isTraceEnabled()) {
066            trace(formatText(msg));
067        }
068    }
069
070    /**
071     * Log an exception (throwable) at the TRACE level with an
072     * accompanying message.
073     *
074     * @param msg the message accompanying the exception
075     * @param t   the exception (throwable) to log
076     * @since 2.0.0
077     */
078    default void trace(Component msg, Throwable t) {
079        if (isTraceEnabled()) {
080            trace(formatText(msg), t);
081        }
082
083    }
084
085    /**
086     * Log a message with the specific Marker at the TRACE level.
087     *
088     * @param marker the marker data specific to this log statement
089     * @param msg    the message string to be logged
090     * @since 2.0.0
091     */
092    default void trace(Marker marker, Component msg) {
093        if (isTraceEnabled(marker)) {
094            trace(marker, formatText(msg, marker));
095        }
096
097    }
098
099    /**
100     * This method is similar to {@link #trace(Component, Throwable)} method except that the
101     * marker data is also taken into consideration.
102     *
103     * @param marker the marker data specific to this log statement
104     * @param msg    the message accompanying the exception
105     * @param t      the exception (throwable) to log
106     * @since 2.0.0
107     */
108    default void trace(Marker marker, Component msg, Throwable t) {
109        if (isTraceEnabled(marker)) {
110            trace(marker, formatText(msg, marker), t);
111        }
112
113    }
114
115    /**
116     * Log a message at the DEBUG level.
117     *
118     * @param msg the message string to be logged
119     * @since 2.0.0
120     */
121    default void debug(Component msg) {
122        if (isDebugEnabled()) {
123            debug(formatText(msg));
124        }
125    }
126
127    /**
128     * Log an exception (throwable) at the DEBUG level with an
129     * accompanying message.
130     *
131     * @param msg the message accompanying the exception
132     * @param t   the exception (throwable) to log
133     * @since 2.0.0
134     */
135    default void debug(Component msg, Throwable t) {
136        if (isDebugEnabled()) {
137            debug(formatText(msg), t);
138        }
139
140    }
141
142    /**
143     * Log a message with the specific Marker at the DEBUG level.
144     *
145     * @param marker the marker data specific to this log statement
146     * @param msg    the message string to be logged
147     * @since 2.0.0
148     */
149    default void debug(Marker marker, Component msg) {
150        if (isDebugEnabled(marker)) {
151            debug(marker, formatText(msg, marker));
152        }
153
154    }
155
156    /**
157     * This method is similar to {@link #debug(Component, Throwable)} method except that the
158     * marker data is also taken into consideration.
159     *
160     * @param marker the marker data specific to this log statement
161     * @param msg    the message accompanying the exception
162     * @param t      the exception (throwable) to log
163     * @since 2.0.0
164     */
165    default void debug(Marker marker, Component msg, Throwable t) {
166        if (isDebugEnabled(marker)) {
167            debug(marker, formatText(msg, marker), t);
168        }
169
170    }
171
172    /**
173     * Log a message at the INFO level.
174     *
175     * @param msg the message string to be logged
176     * @since 2.0.0
177     */
178    default void info(Component msg) {
179        if (isInfoEnabled()) {
180            info(formatText(msg));
181        }
182
183    }
184
185    /**
186     * Log an exception (throwable) at the INFO level with an
187     * accompanying message.
188     *
189     * @param msg the message accompanying the exception
190     * @param t   the exception (throwable) to log
191     * @since 2.0.0
192     */
193    default void info(Component msg, Throwable t) {
194        if (isInfoEnabled()) {
195            info(formatText(msg), t);
196        }
197
198    }
199
200    /**
201     * Log a message with the specific Marker at the INFO level.
202     *
203     * @param marker The marker specific to this log statement
204     * @param msg    the message string to be logged
205     * @since 2.0.0
206     */
207    default void info(Marker marker, Component msg) {
208        if (isInfoEnabled(marker)) {
209            info(marker, formatText(msg, marker));
210        }
211
212    }
213
214    /**
215     * This method is similar to {@link #info(Component, Throwable)} method
216     * except that the marker data is also taken into consideration.
217     *
218     * @param marker the marker data for this log statement
219     * @param msg    the message accompanying the exception
220     * @param t      the exception (throwable) to log
221     * @since 2.0.0
222     */
223    default void info(Marker marker, Component msg, Throwable t) {
224        if (isInfoEnabled(marker)) {
225            info(marker, formatText(msg, marker), t);
226        }
227
228    }
229
230    /**
231     * Log a message at the WARN level.
232     *
233     * @param msg the message string to be logged
234     * @since 2.0.0
235     */
236    default void warn(Component msg) {
237        if (isWarnEnabled()) {
238            warn(formatText(msg));
239        }
240
241    }
242
243    /**
244     * Log an exception (throwable) at the WARN level with an
245     * accompanying message.
246     *
247     * @param msg the message accompanying the exception
248     * @param t   the exception (throwable) to log
249     * @since 2.0.0
250     */
251    default void warn(Component msg, Throwable t) {
252        if (isWarnEnabled()) {
253            warn(formatText(msg), t);
254        }
255
256    }
257
258    /**
259     * Log a message with the specific Marker at the WARN level.
260     *
261     * @param marker The marker specific to this log statement
262     * @param msg    the message string to be logged
263     * @since 2.0.0
264     */
265    default void warn(Marker marker, Component msg) {
266        if (isWarnEnabled(marker)) {
267            warn(marker, formatText(msg, marker));
268        }
269
270    }
271
272    /**
273     * This method is similar to {@link #warn(Component, Throwable)} method
274     * except that the marker data is also taken into consideration.
275     *
276     * @param marker the marker data for this log statement
277     * @param msg    the message accompanying the exception
278     * @param t      the exception (throwable) to log
279     * @since 2.0.0
280     */
281    default void warn(Marker marker, Component msg, Throwable t) {
282        if (isWarnEnabled(marker)) {
283            warn(marker, formatText(msg, marker), t);
284        }
285
286    }
287
288    /**
289     * Log a message at the ERROR level.
290     *
291     * @param msg the message string to be logged
292     * @since 2.0.0
293     */
294    default void error(Component msg) {
295        if (isErrorEnabled()) {
296            error(formatText(msg));
297        }
298
299    }
300
301    /**
302     * Log an exception (throwable) at the ERROR level with an
303     * accompanying message.
304     *
305     * @param msg the message accompanying the exception
306     * @param t   the exception (throwable) to log
307     * @since 2.0.0
308     */
309    default void error(Component msg, Throwable t) {
310        if (isErrorEnabled()) {
311            error(formatText(msg), t);
312        }
313
314    }
315
316    /**
317     * Log a message with the specific Marker at the ERROR level.
318     *
319     * @param marker The marker specific to this log statement
320     * @param msg    the message string to be logged
321     * @since 2.0.0
322     */
323    default void error(Marker marker, Component msg) {
324        if (isErrorEnabled(marker)) {
325            error(marker, formatText(msg, marker));
326        }
327
328    }
329
330    /**
331     * This method is similar to {@link #error(Component, Throwable)}
332     * method except that the marker data is also taken into
333     * consideration.
334     *
335     * @param marker the marker data specific to this log statement
336     * @param msg    the message accompanying the exception
337     * @param t      the exception (throwable) to log
338     * @since 2.0.0
339     */
340    default void error(Marker marker, Component msg, Throwable t) {
341        if (isErrorEnabled(marker)) {
342            error(marker, formatText(msg, marker), t);
343        }
344
345    }
346}