Skip to content

Commit 4ed6605

Browse files
committed
GraylogExtendedLogFormatProperties throws NullPointerException
when only 'logging.structured.gelf.host' is specified Signed-off-by: Dmytro Nosan <[email protected]>
1 parent e48cfce commit 4ed6605

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,10 +36,14 @@ public record GraylogExtendedLogFormatProperties(String host, Service service) {
3636

3737
GraylogExtendedLogFormatProperties withDefaults(Environment environment) {
3838
String name = withFallbackProperty(environment, this.host, "spring.application.name");
39-
Service service = this.service.withDefaults(environment);
39+
Service service = getService().withDefaults(environment);
4040
return new GraylogExtendedLogFormatProperties(name, service);
4141
}
4242

43+
private Service getService() {
44+
return (this.service != null) ? this.service : Service.NONE;
45+
}
46+
4347
static String withFallbackProperty(Environment environment, String value, String property) {
4448
return (!StringUtils.hasLength(value)) ? environment.getProperty(property) : value;
4549
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,23 @@ void getBindsFromEnvironment() {
4141
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
4242
}
4343

44+
@Test
45+
void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissing() {
46+
MockEnvironment environment = new MockEnvironment();
47+
environment.setProperty("logging.structured.gelf.host", "spring");
48+
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
49+
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", Service.NONE));
50+
}
51+
52+
@Test
53+
void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissingUsesApplicationVersion() {
54+
MockEnvironment environment = new MockEnvironment();
55+
environment.setProperty("logging.structured.gelf.host", "spring");
56+
environment.setProperty("spring.application.version", "1.2.3");
57+
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
58+
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
59+
}
60+
4461
@Test
4562
void getWhenNoServiceNameUsesApplicationName() {
4663
MockEnvironment environment = new MockEnvironment();

0 commit comments

Comments
 (0)