|
4 | 4 | import com.spt.development.logging.spring.RepositoryLogger; |
5 | 5 | import com.spt.development.logging.spring.RestControllerLogger; |
6 | 6 | import com.spt.development.logging.spring.ServiceLogger; |
| 7 | +import org.springframework.beans.factory.annotation.Value; |
7 | 8 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
8 | 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
9 | 10 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
|
17 | 18 | */ |
18 | 19 | @AutoConfiguration |
19 | 20 | public class LoggingSpringAutoConfiguration { |
| 21 | + private final boolean mdcDisabled; |
20 | 22 |
|
21 | 23 | /** |
22 | | - * Creates a vanilla {@link RestControllerLogger} (aspect) bean. |
| 24 | + * Creates a new instance of the configuration bean with the spt.cid.mdc.disabled property. |
| 25 | + * |
| 26 | + * @param mdcDisabled a flag to determine whether the correlation ID can be expected to be in the MDC or not. If |
| 27 | + * <code>false</code> then the loggers will explicitly include the current correlation ID in |
| 28 | + * the log statements added by the aspects. |
| 29 | + */ |
| 30 | + public LoggingSpringAutoConfiguration(@Value("${spt.cid.mdc.disabled:false}") final boolean mdcDisabled) { |
| 31 | + this.mdcDisabled = mdcDisabled; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a {@link RestControllerLogger} (aspect) bean. If the <code>spt.cid.mdc.disabled</code> property is set to |
| 36 | + * <code>true</code>, the correlation ID will be included in the generated log statements. |
23 | 37 | * |
24 | 38 | * @return a new {@link RestControllerLogger} bean. |
25 | 39 | */ |
26 | 40 | @Bean |
27 | 41 | @ConditionalOnMissingBean |
28 | 42 | @ConditionalOnClass({ RestController.class }) |
29 | 43 | public RestControllerLogger restControllerLogger() { |
30 | | - return new RestControllerLogger(); |
| 44 | + return new RestControllerLogger(mdcDisabled); |
31 | 45 | } |
32 | 46 |
|
33 | 47 | /** |
34 | | - * Creates a vanilla {@link JmsListenerLogger} (aspect) bean. |
| 48 | + * Creates a {@link JmsListenerLogger} (aspect) bean. If the <code>spt.cid.mdc.disabled</code> property is set to |
| 49 | + * <code>true</code>, the correlation ID will be included in the generated log statements. |
35 | 50 | * |
36 | 51 | * @return a new {@link JmsListenerLogger} bean. |
37 | 52 | */ |
38 | 53 | @Bean |
39 | 54 | @ConditionalOnMissingBean |
40 | 55 | @ConditionalOnClass({ JmsListener.class }) |
41 | 56 | public JmsListenerLogger jmsListenerLogger() { |
42 | | - return new JmsListenerLogger(); |
| 57 | + return new JmsListenerLogger(mdcDisabled); |
43 | 58 | } |
44 | 59 |
|
45 | 60 | /** |
46 | | - * Creates a vanilla {@link ServiceLogger} (aspect) bean. |
| 61 | + * Creates a {@link ServiceLogger} (aspect) bean. If the <code>spt.cid.mdc.disabled</code> property is set to |
| 62 | + * <true>true</true>, the correlation ID will be included in the generated log statements. |
47 | 63 | * |
48 | 64 | * @return a new {@link ServiceLogger} bean. |
49 | 65 | */ |
50 | 66 | @Bean |
51 | 67 | @ConditionalOnMissingBean |
52 | 68 | public ServiceLogger serviceLogger() { |
53 | | - return new ServiceLogger(); |
| 69 | + return new ServiceLogger(mdcDisabled); |
54 | 70 | } |
55 | 71 |
|
56 | 72 | /** |
57 | | - * Creates a vanilla {@link RepositoryLogger} (aspect) bean. |
| 73 | + * Creates a vanilla {@link RepositoryLogger} (aspect) bean. If the <code>spt.cid.mdc.disabled</code> property is |
| 74 | + * set to <code>true</code>, the correlation ID will be included in the generated log statements. |
58 | 75 | * |
59 | 76 | * @return a new {@link RepositoryLogger} bean. |
60 | 77 | */ |
61 | 78 | @Bean |
62 | 79 | @ConditionalOnMissingBean |
63 | 80 | public RepositoryLogger repositoryLogger() { |
64 | | - return new RepositoryLogger(); |
| 81 | + return new RepositoryLogger(mdcDisabled); |
65 | 82 | } |
66 | 83 | } |
0 commit comments