Browse Source

Fix potential NPE in GraylogExtendedLogFormatProperties

Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>

See gh-43863
pull/43862/head
Dmytro Nosan 1 year ago committed by Stéphane Nicoll
parent
commit
9de517281e
  1. 7
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java
  2. 19
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,6 +34,11 @@ public record GraylogExtendedLogFormatProperties(String host, Service service) { @@ -34,6 +34,11 @@ public record GraylogExtendedLogFormatProperties(String host, Service service) {
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE);
public GraylogExtendedLogFormatProperties(String host, Service service) {
this.host = host;
this.service = (service != null) ? service : Service.NONE;
}
GraylogExtendedLogFormatProperties withDefaults(Environment environment) {
String name = withFallbackProperty(environment, this.host, "spring.application.name");
Service service = this.service.withDefaults(environment);

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,6 +41,23 @@ class GraylogExtendedLogFormatPropertiesTests { @@ -41,6 +41,23 @@ class GraylogExtendedLogFormatPropertiesTests {
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
}
@Test
void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissing() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.structured.gelf.host", "spring");
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", Service.NONE));
}
@Test
void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissingUsesApplicationVersion() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.structured.gelf.host", "spring");
environment.setProperty("spring.application.version", "1.2.3");
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
}
@Test
void getWhenNoServiceNameUsesApplicationName() {
MockEnvironment environment = new MockEnvironment();

Loading…
Cancel
Save