1 | package fr.sii.ogham.core.util; | |
2 | ||
3 | import java.util.function.Supplier; | |
4 | ||
5 | import org.slf4j.Logger; | |
6 | import org.slf4j.LoggerFactory; | |
7 | ||
8 | import fr.sii.ogham.core.message.Message; | |
9 | import fr.sii.ogham.email.message.Email; | |
10 | import fr.sii.ogham.sms.message.Sms; | |
11 | ||
12 | /** | |
13 | * Utility class used by loggers to log only useful information. | |
14 | * | |
15 | * According to the log level and the message, logged information is adapted: | |
16 | * <ul> | |
17 | * <li>If log level is configured to INFO, uses default {@link Email#toString()} | |
18 | * and {@link Sms#toString()} behavior i.e. log useful information about the | |
19 | * message (content is not displayed).</li> | |
20 | * <li>If log level is configured to TRACE or DEBUG, {@link Email#toLogString()} | |
21 | * and {@link Sms#toLogString()}, more information is displayed such as the | |
22 | * content</li> | |
23 | * <li>If log level is configured to WARN or ERROR, only an identifier is | |
24 | * logged.</li> | |
25 | * </ul> | |
26 | * | |
27 | * @author Aurélien Baudet | |
28 | * | |
29 | */ | |
30 | public final class LogUtils { | |
31 | private static final Logger LOG = LoggerFactory.getLogger(LogUtils.class); | |
32 | ||
33 | /** | |
34 | * Generate a string to be logged for the provided message. | |
35 | * | |
36 | * According to the log level and the message, logged information is | |
37 | * adapted: | |
38 | * <ul> | |
39 | * <li>If log level is configured to INFO, uses default | |
40 | * {@link Email#toString()} and {@link Sms#toString()} behavior i.e. log | |
41 | * useful information about the message (content is not displayed).</li> | |
42 | * <li>If log level is configured to TRACE or DEBUG, | |
43 | * {@link Email#toLogString()} and {@link Sms#toLogString()}, more | |
44 | * information is displayed such as the content</li> | |
45 | * <li>If log level is configured to WARN or ERROR, only an identifier is | |
46 | * logged.</li> | |
47 | * </ul> | |
48 | * | |
49 | * <p> | |
50 | * The {@link Lazy} proxy is used in order to avoid early string | |
51 | * construction while the log may be skipped. | |
52 | * | |
53 | * @param message | |
54 | * the message to log | |
55 | * @return an intermediate object that will be evaluated when log is written | |
56 | * only | |
57 | */ | |
58 | public static Lazy logString(Message message) { | |
59 | if (LOG.isDebugEnabled() || LOG.isTraceEnabled()) { | |
60 |
3
1. logString : replaced return value with null for fr/sii/ogham/core/util/LogUtils::logString → NO_COVERAGE 2. logString : replaced return value with null for fr/sii/ogham/core/util/LogUtils::logString → SURVIVED 3. logString : replaced return value with null for fr/sii/ogham/core/util/LogUtils::logString → TIMED_OUT |
return new Lazy(message::toString); |
61 | } | |
62 | if (LOG.isInfoEnabled() && message instanceof Loggable) { | |
63 |
1
1. logString : replaced return value with null for fr/sii/ogham/core/util/LogUtils::logString → NO_COVERAGE |
return new Lazy(((Loggable) message)::toLogString); |
64 | } | |
65 | // TODO: each message should have a unique identifier (useful for | |
66 | // debugging, logs, ...)? or use a hash | |
67 |
1
1. logString : replaced return value with null for fr/sii/ogham/core/util/LogUtils::logString → NO_COVERAGE |
return new Lazy("<hidden>"); |
68 | } | |
69 | ||
70 | /** | |
71 | * Utility class that delegates {@link #toString()} call to a supplier that | |
72 | * provides the real string. | |
73 | * | |
74 | * <p> | |
75 | * The aim is to construct tge string only when necessary. | |
76 | * | |
77 | * @author Aurélien Baudet | |
78 | * | |
79 | */ | |
80 | public static class Lazy { | |
81 | private final Supplier<String> supplier; | |
82 | ||
83 | public Lazy(String str) { | |
84 |
1
1. lambda$new$0 : replaced return value with "" for fr/sii/ogham/core/util/LogUtils$Lazy::lambda$new$0 → NO_COVERAGE |
this(() -> str); |
85 | } | |
86 | ||
87 | public Lazy(Supplier<String> supplier) { | |
88 | super(); | |
89 | this.supplier = supplier; | |
90 | } | |
91 | ||
92 | @Override | |
93 | public String toString() { | |
94 |
3
1. toString : replaced return value with "" for fr/sii/ogham/core/util/LogUtils$Lazy::toString → SURVIVED 2. toString : replaced return value with "" for fr/sii/ogham/core/util/LogUtils$Lazy::toString → NO_COVERAGE 3. toString : replaced return value with "" for fr/sii/ogham/core/util/LogUtils$Lazy::toString → TIMED_OUT |
return supplier.get(); |
95 | } | |
96 | } | |
97 | ||
98 | private LogUtils() { | |
99 | super(); | |
100 | } | |
101 | } | |
Mutations | ||
60 |
1.1 2.2 3.3 |
|
63 |
1.1 |
|
67 |
1.1 |
|
84 |
1.1 |
|
94 |
1.1 2.2 3.3 |