Skip to content

Commit a493925

Browse files
authored
Merge pull request #45900 from mcruzdev/config-gelf-logging
Convert logging-gelf to use @ConfigMapping
2 parents 02dda9d + 16e6154 commit a493925

File tree

5 files changed

+72
-82
lines changed

5 files changed

+72
-82
lines changed

extensions/logging-gelf/deployment/pom.xml

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
<version>${project.version}</version>
4040
</path>
4141
</annotationProcessorPaths>
42-
<compilerArgs>
43-
<arg>-AlegacyConfigRoot=true</arg>
44-
</compilerArgs>
4542
</configuration>
4643
</execution>
4744
</executions>

extensions/logging-gelf/runtime/pom.xml

-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
<version>${project.version}</version>
5050
</path>
5151
</annotationProcessorPaths>
52-
<compilerArgs>
53-
<arg>-AlegacyConfigRoot=true</arg>
54-
</compilerArgs>
5552
</configuration>
5653
</execution>
5754
</executions>
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package io.quarkus.logging.gelf;
22

33
import io.quarkus.runtime.annotations.ConfigGroup;
4-
import io.quarkus.runtime.annotations.ConfigItem;
4+
import io.smallrye.config.WithDefault;
55

66
/**
77
* Post additional fields. E.g. `fieldName1=value1,fieldName2=value2`.
88
*/
99
@ConfigGroup
10-
public class AdditionalFieldConfig {
10+
public interface AdditionalFieldConfig {
1111
/**
1212
* Additional field value.
1313
*/
14-
@ConfigItem
15-
public String value;
14+
String value();
1615

1716
/**
1817
* Additional field type specification.
1918
* Supported types: String, long, Long, double, Double and discover.
2019
* Discover is the default if not specified, it discovers field type based on parseability.
2120
*/
22-
@ConfigItem(defaultValue = "discover")
23-
public String type;
21+
@WithDefault("discover")
22+
String type();
2423
}

extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfConfig.java

+41-44
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,46 @@
66

77
import io.quarkus.runtime.annotations.ConfigDocMapKey;
88
import io.quarkus.runtime.annotations.ConfigDocSection;
9-
import io.quarkus.runtime.annotations.ConfigItem;
109
import io.quarkus.runtime.annotations.ConfigPhase;
1110
import io.quarkus.runtime.annotations.ConfigRoot;
11+
import io.smallrye.config.ConfigMapping;
12+
import io.smallrye.config.WithDefault;
1213

13-
@ConfigRoot(phase = ConfigPhase.RUN_TIME, name = "log.handler.gelf")
14-
public class GelfConfig {
14+
@ConfigMapping(prefix = "quarkus.log.handler.gelf")
15+
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
16+
public interface GelfConfig {
1517
/**
1618
* Determine whether to enable the GELF logging handler
1719
*/
18-
@ConfigItem
19-
public boolean enabled;
20+
@WithDefault("false")
21+
boolean enabled();
2022

2123
/**
2224
* Hostname/IP-Address of the Logstash/Graylog Host
2325
* By default it uses UDP, prepend tcp: to the hostname to switch to TCP, example: "tcp:localhost"
2426
*/
25-
@ConfigItem(defaultValue = "localhost")
26-
public String host;
27+
@WithDefault("localhost")
28+
String host();
2729

2830
/**
2931
* The port
3032
*/
31-
@ConfigItem(defaultValue = "12201")
32-
public int port;
33+
@WithDefault("12201")
34+
int port();
3335

3436
/**
3537
* GELF version: 1.0 or 1.1
3638
*/
37-
@ConfigItem(defaultValue = "1.1")
38-
public String version;
39+
@WithDefault("1.1")
40+
String version();
3941

4042
/**
4143
* Whether to post Stack-Trace to StackTrace field.
4244
*
4345
* @see #stackTraceThrowableReference to customize the way the Stack-Trace is handled.
4446
*/
45-
@ConfigItem(defaultValue = "true")
46-
public boolean extractStackTrace;
47+
@WithDefault("true")
48+
boolean extractStackTrace();
4749

4850
/**
4951
* Only used when `extractStackTrace` is `true`.
@@ -53,32 +55,32 @@ public class GelfConfig {
5355
* Negative throwable reference walk the exception chain from the root cause side: -1 will extract the root cause,
5456
* -2 the exception wrapping the root cause, ...
5557
*/
56-
@ConfigItem
57-
public int stackTraceThrowableReference;
58+
@WithDefault("0")
59+
int stackTraceThrowableReference();
5860

5961
/**
6062
* Whether to perform Stack-Trace filtering
6163
*/
62-
@ConfigItem
63-
public boolean filterStackTrace;
64+
@WithDefault("false")
65+
boolean filterStackTrace();
6466

6567
/**
6668
* Java date pattern, see {@link java.text.SimpleDateFormat}
6769
*/
68-
@ConfigItem(defaultValue = "yyyy-MM-dd HH:mm:ss,SSS")
69-
public String timestampPattern;
70+
@WithDefault("yyyy-MM-dd HH:mm:ss,SSS")
71+
String timestampPattern();
7072

7173
/**
7274
* The logging-gelf log level.
7375
*/
74-
@ConfigItem(defaultValue = "ALL")
75-
public Level level;
76+
@WithDefault("ALL")
77+
Level level();
7678

7779
/**
7880
* Name of the facility.
7981
*/
80-
@ConfigItem(defaultValue = "jboss-logmanager")
81-
public String facility;
82+
@WithDefault("jboss-logmanager")
83+
String facility();
8284

8385
/**
8486
* Post additional fields.
@@ -89,67 +91,62 @@ public class GelfConfig {
8991
* quarkus.log.handler.gelf.additional-field.field1.type=String
9092
* </pre>
9193
*/
92-
@ConfigItem
93-
@ConfigDocMapKey("field-name")
9494
@ConfigDocSection
95-
public Map<String, AdditionalFieldConfig> additionalField;
95+
@ConfigDocMapKey("field-name")
96+
Map<String, AdditionalFieldConfig> additionalField();
9697

9798
/**
9899
* Whether to include all fields from the MDC.
99100
*/
100-
@ConfigItem
101-
public boolean includeFullMdc;
101+
@WithDefault("false")
102+
boolean includeFullMdc();
102103

103104
/**
104105
* Send additional fields whose values are obtained from MDC. Name of the Fields are comma-separated. Example:
105106
* mdcFields=Application,Version,SomeOtherFieldName
106107
*/
107-
@ConfigItem()
108-
public Optional<String> mdcFields;
108+
Optional<String> mdcFields();
109109

110110
/**
111111
* Dynamic MDC Fields allows you to extract MDC values based on one or more regular expressions. Multiple regexes are
112112
* comma-separated. The name of the MDC entry is used as GELF field name.
113113
*/
114-
@ConfigItem
115-
public Optional<String> dynamicMdcFields;
114+
Optional<String> dynamicMdcFields();
116115

117116
/**
118117
* Pattern-based type specification for additional and MDC fields. Key-value pairs are comma-separated. Example:
119118
* my_field.*=String,business\..*\.field=double
120119
*/
121-
@ConfigItem
122-
public Optional<String> dynamicMdcFieldTypes;
120+
Optional<String> dynamicMdcFieldTypes();
123121

124122
/**
125123
* Maximum message size (in bytes).
126124
* If the message size is exceeded, the appender will submit the message in multiple chunks.
127125
*/
128-
@ConfigItem(defaultValue = "8192")
129-
public int maximumMessageSize;
126+
@WithDefault("8192")
127+
int maximumMessageSize();
130128

131129
/**
132130
* Include message parameters from the log event
133131
*/
134-
@ConfigItem(defaultValue = "true")
135-
public boolean includeLogMessageParameters;
132+
@WithDefault("true")
133+
boolean includeLogMessageParameters();
136134

137135
/**
138136
* Include source code location
139137
*/
140-
@ConfigItem(defaultValue = "true")
141-
public boolean includeLocation;
138+
@WithDefault("true")
139+
boolean includeLocation();
142140

143141
/**
144142
* Origin hostname
145143
*/
146-
@ConfigItem
147-
public Optional<String> originHost;
144+
Optional<String> originHost();
148145

149146
/**
150147
* Bypass hostname resolution. If you didn't set the {@code originHost} property, and resolution is disabled, the value
151148
* “unknown” will be used as hostname
152149
*/
153-
@ConfigItem
154-
public boolean skipHostnameResolution;
150+
@WithDefault("false")
151+
boolean skipHostnameResolution();
155152
}

extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfLogHandlerRecorder.java

+26-26
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,59 @@
1313
@Recorder
1414
public class GelfLogHandlerRecorder {
1515
public RuntimeValue<Optional<Handler>> initializeHandler(final GelfConfig config) {
16-
if (!config.enabled) {
16+
if (!config.enabled()) {
1717
return new RuntimeValue<>(Optional.empty());
1818
}
1919

2020
String previousSkipHostnameResolution = null;
21-
if (config.skipHostnameResolution) {
21+
if (config.skipHostnameResolution()) {
2222
previousSkipHostnameResolution = System.setProperty(PROPERTY_LOGSTASH_GELF_SKIP_HOSTNAME_RESOLUTION, "true");
2323
}
2424
final JBoss7GelfLogHandler handler = new JBoss7GelfLogHandler();
25-
if (config.skipHostnameResolution) {
25+
if (config.skipHostnameResolution()) {
2626
if (previousSkipHostnameResolution == null) {
2727
System.clearProperty(PROPERTY_LOGSTASH_GELF_SKIP_HOSTNAME_RESOLUTION);
2828
} else {
2929
System.setProperty(PROPERTY_LOGSTASH_GELF_SKIP_HOSTNAME_RESOLUTION, previousSkipHostnameResolution);
3030
}
3131
}
32-
handler.setVersion(config.version);
33-
handler.setFacility(config.facility);
34-
String extractStackTrace = String.valueOf(config.extractStackTrace);
35-
if (config.extractStackTrace && config.stackTraceThrowableReference != 0) {
36-
extractStackTrace = String.valueOf(config.stackTraceThrowableReference);
32+
handler.setVersion(config.version());
33+
handler.setFacility(config.facility());
34+
String extractStackTrace = String.valueOf(config.extractStackTrace());
35+
if (config.extractStackTrace() && config.stackTraceThrowableReference() != 0) {
36+
extractStackTrace = String.valueOf(config.stackTraceThrowableReference());
3737
}
3838
handler.setExtractStackTrace(extractStackTrace);
39-
handler.setFilterStackTrace(config.filterStackTrace);
40-
handler.setTimestampPattern(config.timestampPattern);
41-
handler.setIncludeFullMdc(config.includeFullMdc);
42-
handler.setDynamicMdcFields(config.dynamicMdcFields.orElse(null));
43-
handler.setMdcFields(config.mdcFields.orElse(null));
44-
handler.setDynamicMdcFieldTypes(config.dynamicMdcFieldTypes.orElse(null));
45-
handler.setHost(config.host);
46-
handler.setPort(config.port);
47-
handler.setLevel(config.level);
48-
handler.setMaximumMessageSize(config.maximumMessageSize);
49-
handler.setIncludeLocation(config.includeLocation);
50-
handler.setIncludeLogMessageParameters(config.includeLogMessageParameters);
51-
if (config.originHost.isPresent()) {
52-
handler.setOriginHost(config.originHost.get());
39+
handler.setFilterStackTrace(config.filterStackTrace());
40+
handler.setTimestampPattern(config.timestampPattern());
41+
handler.setIncludeFullMdc(config.includeFullMdc());
42+
handler.setDynamicMdcFields(config.dynamicMdcFields().orElse(null));
43+
handler.setMdcFields(config.mdcFields().orElse(null));
44+
handler.setDynamicMdcFieldTypes(config.dynamicMdcFieldTypes().orElse(null));
45+
handler.setHost(config.host());
46+
handler.setPort(config.port());
47+
handler.setLevel(config.level());
48+
handler.setMaximumMessageSize(config.maximumMessageSize());
49+
handler.setIncludeLocation(config.includeLocation());
50+
handler.setIncludeLogMessageParameters(config.includeLogMessageParameters());
51+
if (config.originHost().isPresent()) {
52+
handler.setOriginHost(config.originHost().get());
5353
}
5454

5555
// handle additional fields
56-
if (!config.additionalField.isEmpty()) {
56+
if (!config.additionalField().isEmpty()) {
5757
StringBuilder additionalFieldsValue = new StringBuilder();
5858
StringBuilder additionalFieldsType = new StringBuilder();
59-
for (Map.Entry<String, AdditionalFieldConfig> additionalField : config.additionalField.entrySet()) {
59+
for (Map.Entry<String, AdditionalFieldConfig> additionalField : config.additionalField().entrySet()) {
6060
if (additionalFieldsValue.length() > 0) {
6161
additionalFieldsValue.append(',');
6262
}
63-
additionalFieldsValue.append(additionalField.getKey()).append('=').append(additionalField.getValue().value);
63+
additionalFieldsValue.append(additionalField.getKey()).append('=').append(additionalField.getValue().value());
6464

6565
if (additionalFieldsType.length() > 0) {
6666
additionalFieldsType.append(',');
6767
}
68-
additionalFieldsType.append(additionalField.getKey()).append('=').append(additionalField.getValue().type);
68+
additionalFieldsType.append(additionalField.getKey()).append('=').append(additionalField.getValue().type());
6969
}
7070

7171
handler.setAdditionalFields(additionalFieldsValue.toString());

0 commit comments

Comments
 (0)