Skip to content

Commit 9de5172

Browse files
nosansnicoll
authored andcommitted
Fix potential NPE in GraylogExtendedLogFormatProperties
Signed-off-by: Dmytro Nosan <[email protected]> See gh-43863
1 parent ae2ca01 commit 9de5172

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

+6-1
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.
@@ -34,6 +34,11 @@ public record GraylogExtendedLogFormatProperties(String host, Service service) {
3434

3535
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE);
3636

37+
public GraylogExtendedLogFormatProperties(String host, Service service) {
38+
this.host = host;
39+
this.service = (service != null) ? service : Service.NONE;
40+
}
41+
3742
GraylogExtendedLogFormatProperties withDefaults(Environment environment) {
3843
String name = withFallbackProperty(environment, this.host, "spring.application.name");
3944
Service service = this.service.withDefaults(environment);

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

+18-1
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)