Rationalize structured logging service classes
Rename `ElasticCommonSchemaService` & `GraylogExtendedLogFormatService` and change a few of the property names. The `Service` suffix was originally chosen because ECS uses the term, but `Properties` is more common in the Spring Boot codebase and works better for Graylog. Closes gh-42578
This commit is contained in:
@@ -524,19 +524,19 @@ A log line looks like this:
|
||||
This format also adds every key value pair contained in the MDC to the JSON object.
|
||||
You can also use the https://www.slf4j.org/manual.html#fluent[SLF4J fluent logging API] to add key value pairs to the logged JSON object with the https://www.slf4j.org/apidocs/org/slf4j/spi/LoggingEventBuilder.html#addKeyValue(java.lang.String,java.lang.Object)[addKeyValue] method.
|
||||
|
||||
The `service` values can be customized using `logging.structured.gelf.service` properties:
|
||||
Several fields can be customized using `logging.structured.gelf` properties:
|
||||
|
||||
[configprops,yaml]
|
||||
----
|
||||
logging:
|
||||
structured:
|
||||
gelf:
|
||||
host: MyService
|
||||
service:
|
||||
name: MyService
|
||||
version: 1.0
|
||||
----
|
||||
|
||||
NOTE: configprop:logging.structured.gelf.service.name[] will default to configprop:spring.application.name[] if not specified.
|
||||
NOTE: configprop:logging.structured.gelf.host[] will default to configprop:spring.application.name[] if not specified.
|
||||
|
||||
NOTE: configprop:logging.structured.gelf.service.version[] will default to configprop:spring.application.version[] if not specified.
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.apache.logging.log4j.util.ReadOnlyStringMap;
|
||||
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
|
||||
import org.springframework.boot.logging.structured.ElasticCommonSchemaService;
|
||||
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties;
|
||||
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
|
||||
import org.springframework.boot.logging.structured.StructuredLogFormatter;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -51,7 +51,7 @@ class ElasticCommonSchemaStructuredLogFormatter extends JsonWriterStructuredLogF
|
||||
members.add("process.pid", environment.getProperty("spring.application.pid", Long.class))
|
||||
.when(Objects::nonNull);
|
||||
members.add("process.thread.name", LogEvent::getThreadName);
|
||||
ElasticCommonSchemaService.get(environment).jsonMembers(members);
|
||||
ElasticCommonSchemaProperties.get(environment).jsonMembers(members);
|
||||
members.add("log.logger", LogEvent::getLoggerName);
|
||||
members.add("message", LogEvent::getMessage).as(StructuredMessage::get);
|
||||
members.from(LogEvent::getContextData)
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.json.JsonWriter.Members;
|
||||
import org.springframework.boot.json.WritableJson;
|
||||
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
|
||||
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatService;
|
||||
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties;
|
||||
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
|
||||
import org.springframework.boot.logging.structured.StructuredLogFormatter;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -83,7 +83,7 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
|
||||
members.add("_process_pid", environment.getProperty("spring.application.pid", Long.class))
|
||||
.when(Objects::nonNull);
|
||||
members.add("_process_thread_name", LogEvent::getThreadName);
|
||||
GraylogExtendedLogFormatService.get(environment).jsonMembers(members);
|
||||
GraylogExtendedLogFormatProperties.get(environment).jsonMembers(members);
|
||||
members.add("_log_logger", LogEvent::getLoggerName);
|
||||
members.from(LogEvent::getContextData)
|
||||
.whenNot(ReadOnlyStringMap::isEmpty)
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.slf4j.event.KeyValuePair;
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.json.JsonWriter.PairExtractor;
|
||||
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
|
||||
import org.springframework.boot.logging.structured.ElasticCommonSchemaService;
|
||||
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties;
|
||||
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
|
||||
import org.springframework.boot.logging.structured.StructuredLogFormatter;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -55,7 +55,7 @@ class ElasticCommonSchemaStructuredLogFormatter extends JsonWriterStructuredLogF
|
||||
members.add("process.pid", environment.getProperty("spring.application.pid", Long.class))
|
||||
.when(Objects::nonNull);
|
||||
members.add("process.thread.name", ILoggingEvent::getThreadName);
|
||||
ElasticCommonSchemaService.get(environment).jsonMembers(members);
|
||||
ElasticCommonSchemaProperties.get(environment).jsonMembers(members);
|
||||
members.add("log.logger", ILoggingEvent::getLoggerName);
|
||||
members.add("message", ILoggingEvent::getFormattedMessage);
|
||||
members.addMapEntries(ILoggingEvent::getMDCPropertyMap);
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.json.JsonWriter.Members;
|
||||
import org.springframework.boot.json.WritableJson;
|
||||
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
|
||||
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatService;
|
||||
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties;
|
||||
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
|
||||
import org.springframework.boot.logging.structured.StructuredLogFormatter;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -85,7 +85,7 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
|
||||
members.add("_process_pid", environment.getProperty("spring.application.pid", Long.class))
|
||||
.when(Objects::nonNull);
|
||||
members.add("_process_thread_name", ILoggingEvent::getThreadName);
|
||||
GraylogExtendedLogFormatService.get(environment).jsonMembers(members);
|
||||
GraylogExtendedLogFormatProperties.get(environment).jsonMembers(members);
|
||||
members.add("_log_logger", ILoggingEvent::getLoggerName);
|
||||
members.from(ILoggingEvent::getMDCPropertyMap)
|
||||
.when((mdc) -> !CollectionUtils.isEmpty(mdc))
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.structured;
|
||||
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.json.JsonWriter.Members;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Properties for Elastic Common Schema structured logging.
|
||||
*
|
||||
* @param service service details
|
||||
* @author Moritz Halbritter
|
||||
* @author Phillip Webb
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public record ElasticCommonSchemaProperties(Service service) {
|
||||
|
||||
static final ElasticCommonSchemaProperties NONE = new ElasticCommonSchemaProperties(Service.NONE);
|
||||
|
||||
ElasticCommonSchemaProperties withDefaults(Environment environment) {
|
||||
Service service = this.service.withDefaults(environment);
|
||||
return new ElasticCommonSchemaProperties(service);
|
||||
}
|
||||
|
||||
static String withFallbackProperty(Environment environment, String value, String property) {
|
||||
return (!StringUtils.hasLength(value)) ? environment.getProperty(property) : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@link JsonWriter} members for the service.
|
||||
* @param members the members to add to
|
||||
*/
|
||||
public void jsonMembers(JsonWriter.Members<?> members) {
|
||||
this.service.jsonMembers(members);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new {@link ElasticCommonSchemaProperties} from bound from properties in
|
||||
* the given {@link Environment}.
|
||||
* @param environment the source environment
|
||||
* @return a new {@link ElasticCommonSchemaProperties} instance
|
||||
*/
|
||||
public static ElasticCommonSchemaProperties get(Environment environment) {
|
||||
return Binder.get(environment)
|
||||
.bind("logging.structured.ecs", ElasticCommonSchemaProperties.class)
|
||||
.orElse(NONE)
|
||||
.withDefaults(environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Service details.
|
||||
*
|
||||
* @param name the application name
|
||||
* @param version the version of the application
|
||||
* @param environment the name of the environment the application is running in
|
||||
* @param nodeName the name of the node the application is running on
|
||||
*/
|
||||
public record Service(String name, String version, String environment, String nodeName) {
|
||||
|
||||
static final Service NONE = new Service(null, null, null, null);
|
||||
|
||||
void jsonMembers(Members<?> members) {
|
||||
members.add("service.name", this::name).whenHasLength();
|
||||
members.add("service.version", this::version).whenHasLength();
|
||||
members.add("service.environment", this::environment).whenHasLength();
|
||||
members.add("service.node.name", this::nodeName).whenHasLength();
|
||||
}
|
||||
|
||||
Service withDefaults(Environment environment) {
|
||||
String name = withFallbackProperty(environment, this.name, "spring.application.name");
|
||||
String version = withFallbackProperty(environment, this.version, "spring.application.version");
|
||||
return new Service(name, version, this.environment, this.nodeName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.structured;
|
||||
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Service details for Elastic Common Schema structured logging.
|
||||
*
|
||||
* @param name the application name
|
||||
* @param version the version of the application
|
||||
* @param environment the name of the environment the application is running in
|
||||
* @param nodeName the name of the node the application is running on
|
||||
* @author Moritz Halbritter
|
||||
* @author Phillip Webb
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public record ElasticCommonSchemaService(String name, String version, String environment, String nodeName) {
|
||||
|
||||
static final ElasticCommonSchemaService NONE = new ElasticCommonSchemaService(null, null, null, null);
|
||||
|
||||
private ElasticCommonSchemaService withDefaults(Environment environment) {
|
||||
String name = withFallbackProperty(environment, this.name, "spring.application.name");
|
||||
String version = withFallbackProperty(environment, this.version, "spring.application.version");
|
||||
return new ElasticCommonSchemaService(name, version, this.environment, this.nodeName);
|
||||
}
|
||||
|
||||
private String withFallbackProperty(Environment environment, String value, String property) {
|
||||
return (!StringUtils.hasLength(value)) ? environment.getProperty(property) : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@link JsonWriter} members for the service.
|
||||
* @param members the members to add to
|
||||
*/
|
||||
public void jsonMembers(JsonWriter.Members<?> members) {
|
||||
members.add("service.name", this::name).whenHasLength();
|
||||
members.add("service.version", this::version).whenHasLength();
|
||||
members.add("service.environment", this::environment).whenHasLength();
|
||||
members.add("service.node.name", this::nodeName).whenHasLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new {@link ElasticCommonSchemaService} from bound from properties in the
|
||||
* given {@link Environment}.
|
||||
* @param environment the source environment
|
||||
* @return a new {@link ElasticCommonSchemaService} instance
|
||||
*/
|
||||
public static ElasticCommonSchemaService get(Environment environment) {
|
||||
return Binder.get(environment)
|
||||
.bind("logging.structured.ecs.service", ElasticCommonSchemaService.class)
|
||||
.orElse(NONE)
|
||||
.withDefaults(environment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.structured;
|
||||
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Service details for Graylog Extended Log Format structured logging.
|
||||
*
|
||||
* @param host the application name
|
||||
* @param service the version of the application
|
||||
* @author Samuel Lissner
|
||||
* @author Phillip Webb
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public record GraylogExtendedLogFormatProperties(String host, Service service) {
|
||||
|
||||
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE);
|
||||
|
||||
GraylogExtendedLogFormatProperties withDefaults(Environment environment) {
|
||||
String name = withFallbackProperty(environment, this.host, "spring.application.name");
|
||||
Service service = this.service.withDefaults(environment);
|
||||
return new GraylogExtendedLogFormatProperties(name, service);
|
||||
}
|
||||
|
||||
static String withFallbackProperty(Environment environment, String value, String property) {
|
||||
return (!StringUtils.hasLength(value)) ? environment.getProperty(property) : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@link JsonWriter} members for the service.
|
||||
* @param members the members to add to
|
||||
*/
|
||||
public void jsonMembers(JsonWriter.Members<?> members) {
|
||||
members.add("host", this::host).whenHasLength();
|
||||
this.service.jsonMembers(members);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new {@link GraylogExtendedLogFormatProperties} from bound from properties
|
||||
* in the given {@link Environment}.
|
||||
* @param environment the source environment
|
||||
* @return a new {@link GraylogExtendedLogFormatProperties} instance
|
||||
*/
|
||||
public static GraylogExtendedLogFormatProperties get(Environment environment) {
|
||||
return Binder.get(environment)
|
||||
.bind("logging.structured.gelf", GraylogExtendedLogFormatProperties.class)
|
||||
.orElse(NONE)
|
||||
.withDefaults(environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Service details.
|
||||
*
|
||||
* @param version the version of the application
|
||||
*/
|
||||
public record Service(String version) {
|
||||
|
||||
static final Service NONE = new Service(null);
|
||||
|
||||
Service withDefaults(Environment environment) {
|
||||
String version = withFallbackProperty(environment, this.version, "spring.application.version");
|
||||
return new Service(version);
|
||||
}
|
||||
|
||||
void jsonMembers(JsonWriter.Members<?> members) {
|
||||
members.add("_service_version", this::version).whenHasLength();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.structured;
|
||||
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Service details for Graylog Extended Log Format structured logging.
|
||||
*
|
||||
* @param name the application name
|
||||
* @param version the version of the application
|
||||
* @author Samuel Lissner
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public record GraylogExtendedLogFormatService(String name, String version) {
|
||||
|
||||
static final GraylogExtendedLogFormatService NONE = new GraylogExtendedLogFormatService(null, null);
|
||||
|
||||
private GraylogExtendedLogFormatService withDefaults(Environment environment) {
|
||||
String name = withFallbackProperty(environment, this.name, "spring.application.name");
|
||||
String version = withFallbackProperty(environment, this.version, "spring.application.version");
|
||||
return new GraylogExtendedLogFormatService(name, version);
|
||||
}
|
||||
|
||||
private String withFallbackProperty(Environment environment, String value, String property) {
|
||||
return (!StringUtils.hasLength(value)) ? environment.getProperty(property) : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@link JsonWriter} members for the service.
|
||||
* @param members the members to add to
|
||||
*/
|
||||
public void jsonMembers(JsonWriter.Members<?> members) {
|
||||
members.add("host", this::name).whenHasLength();
|
||||
members.add("_service_version", this::version).whenHasLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new {@link GraylogExtendedLogFormatService} from bound from properties in
|
||||
* the given {@link Environment}.
|
||||
* @param environment the source environment
|
||||
* @return a new {@link GraylogExtendedLogFormatService} instance
|
||||
*/
|
||||
public static GraylogExtendedLogFormatService get(Environment environment) {
|
||||
return Binder.get(environment)
|
||||
.bind("logging.structured.gelf.service", GraylogExtendedLogFormatService.class)
|
||||
.orElse(NONE)
|
||||
.withDefaults(environment);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -255,9 +255,14 @@
|
||||
"description": "Structured logging format for output to a file. Must be either a format id or a fully qualified class name."
|
||||
},
|
||||
{
|
||||
"name": "logging.structured.gelf.service.name",
|
||||
"name": "logging.structured.gelf.host",
|
||||
"type": "java.lang.String",
|
||||
"description": "Structured GELF service name (defaults to 'spring.application.name')."
|
||||
"description": "Structured GELF host (defaults to 'spring.application.name')."
|
||||
},
|
||||
{
|
||||
"name": "logging.structured.gelf.service.version",
|
||||
"type": "java.lang.String",
|
||||
"description": "Structured GELF service version (defaults to 'spring.application.version')."
|
||||
},
|
||||
{
|
||||
"name": "logging.structured.gelf.service.version",
|
||||
|
||||
@@ -44,7 +44,7 @@ class GraylogExtendedLogFormatStructuredLogFormatterTests extends AbstractStruct
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("logging.structured.gelf.service.name", "name");
|
||||
environment.setProperty("logging.structured.gelf.host", "name");
|
||||
environment.setProperty("logging.structured.gelf.service.version", "1.0.0");
|
||||
environment.setProperty("spring.application.pid", "1");
|
||||
this.formatter = new GraylogExtendedLogFormatStructuredLogFormatter(environment);
|
||||
|
||||
@@ -47,7 +47,7 @@ class GraylogExtendedLogFormatStructuredLogFormatterTests extends AbstractStruct
|
||||
void setUp() {
|
||||
super.setUp();
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("logging.structured.gelf.service.name", "name");
|
||||
environment.setProperty("logging.structured.gelf.host", "name");
|
||||
environment.setProperty("logging.structured.gelf.service.version", "1.0.0");
|
||||
environment.setProperty("spring.application.pid", "1");
|
||||
this.formatter = new GraylogExtendedLogFormatStructuredLogFormatter(environment, getThrowableProxyConverter());
|
||||
|
||||
@@ -19,17 +19,18 @@ package org.springframework.boot.logging.structured;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties.Service;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ElasticCommonSchemaService}.
|
||||
* Tests for {@link ElasticCommonSchemaProperties}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
class ElasticCommonSchemaServiceTests {
|
||||
class ElasticCommonSchemaPropertiesTests {
|
||||
|
||||
@Test
|
||||
void getBindsFromEnvironment() {
|
||||
@@ -38,38 +39,40 @@ class ElasticCommonSchemaServiceTests {
|
||||
environment.setProperty("logging.structured.ecs.service.version", "1.2.3");
|
||||
environment.setProperty("logging.structured.ecs.service.environment", "prod");
|
||||
environment.setProperty("logging.structured.ecs.service.node-name", "boot");
|
||||
ElasticCommonSchemaService service = ElasticCommonSchemaService.get(environment);
|
||||
assertThat(service).isEqualTo(new ElasticCommonSchemaService("spring", "1.2.3", "prod", "boot"));
|
||||
ElasticCommonSchemaProperties properties = ElasticCommonSchemaProperties.get(environment);
|
||||
assertThat(properties)
|
||||
.isEqualTo(new ElasticCommonSchemaProperties(new Service("spring", "1.2.3", "prod", "boot")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenNoServiceNameUsesApplicationName() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.application.name", "spring");
|
||||
ElasticCommonSchemaService service = ElasticCommonSchemaService.get(environment);
|
||||
assertThat(service).isEqualTo(new ElasticCommonSchemaService("spring", null, null, null));
|
||||
ElasticCommonSchemaProperties properties = ElasticCommonSchemaProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new ElasticCommonSchemaProperties(new Service("spring", null, null, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenNoServiceVersionUsesApplicationVersion() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.application.version", "1.2.3");
|
||||
ElasticCommonSchemaService service = ElasticCommonSchemaService.get(environment);
|
||||
assertThat(service).isEqualTo(new ElasticCommonSchemaService(null, "1.2.3", null, null));
|
||||
ElasticCommonSchemaProperties properties = ElasticCommonSchemaProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new ElasticCommonSchemaProperties(new Service(null, "1.2.3", null, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenNoPropertiesToBind() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
ElasticCommonSchemaService service = ElasticCommonSchemaService.get(environment);
|
||||
assertThat(service).isEqualTo(new ElasticCommonSchemaService(null, null, null, null));
|
||||
ElasticCommonSchemaProperties properties = ElasticCommonSchemaProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new ElasticCommonSchemaProperties(new Service(null, null, null, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addToJsonMembersCreatesValidJson() {
|
||||
ElasticCommonSchemaService service = new ElasticCommonSchemaService("spring", "1.2.3", "prod", "boot");
|
||||
JsonWriter<ElasticCommonSchemaService> writer = JsonWriter.of(service::jsonMembers);
|
||||
assertThat(writer.writeToString(service))
|
||||
ElasticCommonSchemaProperties properties = new ElasticCommonSchemaProperties(
|
||||
new Service("spring", "1.2.3", "prod", "boot"));
|
||||
JsonWriter<ElasticCommonSchemaProperties> writer = JsonWriter.of(properties::jsonMembers);
|
||||
assertThat(writer.writeToString(properties))
|
||||
.isEqualTo("{\"service.name\":\"spring\",\"service.version\":\"1.2.3\","
|
||||
+ "\"service.environment\":\"prod\",\"service.node.name\":\"boot\"}");
|
||||
}
|
||||
@@ -19,54 +19,57 @@ package org.springframework.boot.logging.structured;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties.Service;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link GraylogExtendedLogFormatService}.
|
||||
* Tests for {@link GraylogExtendedLogFormatProperties}.
|
||||
*
|
||||
* @author Samuel Lissner
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class GraylogExtendedLogFormatServiceTests {
|
||||
class GraylogExtendedLogFormatPropertiesTests {
|
||||
|
||||
@Test
|
||||
void getBindsFromEnvironment() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("logging.structured.gelf.service.name", "spring");
|
||||
environment.setProperty("logging.structured.gelf.host", "spring");
|
||||
environment.setProperty("logging.structured.gelf.service.version", "1.2.3");
|
||||
GraylogExtendedLogFormatService service = GraylogExtendedLogFormatService.get(environment);
|
||||
assertThat(service).isEqualTo(new GraylogExtendedLogFormatService("spring", "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();
|
||||
environment.setProperty("spring.application.name", "spring");
|
||||
GraylogExtendedLogFormatService service = GraylogExtendedLogFormatService.get(environment);
|
||||
assertThat(service).isEqualTo(new GraylogExtendedLogFormatService("spring", null));
|
||||
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service(null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenNoServiceVersionUsesApplicationVersion() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.application.version", "1.2.3");
|
||||
GraylogExtendedLogFormatService service = GraylogExtendedLogFormatService.get(environment);
|
||||
assertThat(service).isEqualTo(new GraylogExtendedLogFormatService(null, "1.2.3"));
|
||||
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties(null, new Service("1.2.3")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenNoPropertiesToBind() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
GraylogExtendedLogFormatService service = GraylogExtendedLogFormatService.get(environment);
|
||||
assertThat(service).isEqualTo(new GraylogExtendedLogFormatService(null, null));
|
||||
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties(null, new Service(null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addToJsonMembersCreatesValidJson() {
|
||||
GraylogExtendedLogFormatService service = new GraylogExtendedLogFormatService("spring", "1.2.3");
|
||||
JsonWriter<GraylogExtendedLogFormatService> writer = JsonWriter.of(service::jsonMembers);
|
||||
assertThat(writer.writeToString(service)).isEqualTo("{\"host\":\"spring\",\"_service_version\":\"1.2.3\"}");
|
||||
GraylogExtendedLogFormatProperties properties = new GraylogExtendedLogFormatProperties("spring",
|
||||
new Service("1.2.3"));
|
||||
JsonWriter<GraylogExtendedLogFormatProperties> writer = JsonWriter.of(properties::jsonMembers);
|
||||
assertThat(writer.writeToString(properties)).isEqualTo("{\"host\":\"spring\",\"_service_version\":\"1.2.3\"}");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user