Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUL - fix loading of boolean configs #182

Merged
merged 3 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public EcsFormatter() {
serviceName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceName", null);
serviceVersion= getProperty("co.elastic.logging.jul.EcsFormatter.serviceVersion", null);
serviceNodeName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceNodeName", null);
includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
stackTraceAsArray = Boolean
.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false"));
includeOrigin = Boolean.parseBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
stackTraceAsArray = Boolean.parseBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false"));
eventDataset = getProperty("co.elastic.logging.jul.EcsFormatter.eventDataset", null);
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
setAdditionalFields(getProperty("co.elastic.logging.jul.EcsFormatter.additionalFields", null));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*-
* #%L
* Java ECS logging
* %%
* Copyright (C) 2019 - 2022 Elastic and contributors
* %%
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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
*
* http://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.
* #L%
*/
package co.elastic.logging.jul;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.logging.LogManager;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests logging setup based on a custom logging.properties file, as well as Exception stack trace formatting as array
*/
public class JulIntegrationTest extends JulLoggingTest {
@BeforeEach
void setUp() throws IOException {
LogManager.getLogManager().readConfiguration(getClass().getClassLoader().getResourceAsStream("logging.properties"));
super.setUpFormatter();
}

@Override
protected EcsFormatter createEcsFormatter() {
// relying on the configuration loaded from the logging.properties file
return new EcsFormatter();
}

@Test
void testLogException() throws Exception {
error("test", new RuntimeException("test"));
JsonNode log = getLastLogLine();
assertThat(log.get("log.level").textValue()).isIn("ERROR", "SEVERE");
assertThat(log.get("error.message").textValue()).isEqualTo("test");
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
assertThat(log.get("error.stack_trace").isArray()).isTrue();
assertThat(log.get("error.stack_trace").get(0).textValue()).isEqualTo("java.lang.RuntimeException: test");
assertThat(log.get("error.stack_trace").get(1).textValue()).contains("at co.elastic.logging.jul.JulIntegrationTest");
}

@Test
void testLogExceptionNullMessage() throws Exception {
error("test", new RuntimeException());
JsonNode log = getLastLogLine();
assertThat(log.get("error.message")).isNull();
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
assertThat(log.get("error.stack_trace").get(0).textValue()).isEqualTo("java.lang.RuntimeException");
assertThat(log.get("error.stack_trace").get(1).textValue()).contains("at co.elastic.logging.jul.JulIntegrationTest");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
Expand Down Expand Up @@ -109,20 +110,16 @@ public JsonNode getLastLogLine() throws IOException {
}

@BeforeEach
void setUp() {
void setUp() throws IOException {
// loads configuration based on the default logging.properties file
LogManager.getLogManager().readConfiguration();
setUpFormatter();
formatter.setAdditionalFields("key1=value1,key2=value2");
}

private void setUpFormatter() {
protected void setUpFormatter() {
clearHandlers();

formatter = new EcsFormatter();
formatter.setIncludeOrigin(true);
formatter.setServiceName("test");
formatter.setServiceVersion("test-version");
formatter.setServiceNodeName("test-node");
formatter.setEventDataset("testdataset");
formatter = createEcsFormatter();

Handler handler = new InMemoryStreamHandler(out, formatter);
handler.setLevel(Level.ALL);
Expand All @@ -131,6 +128,16 @@ private void setUpFormatter() {
logger.setLevel(Level.ALL);
}

protected EcsFormatter createEcsFormatter() {
EcsFormatter ret = new EcsFormatter();
ret.setIncludeOrigin(true);
ret.setServiceName("test");
ret.setServiceVersion("test-version");
ret.setServiceNodeName("test-node");
ret.setEventDataset("testdataset");
return ret;
}

@Test
void testLogOrigin() throws Exception {
debug("test");
Expand Down
8 changes: 8 additions & 0 deletions jul-ecs-formatter/src/test/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ECS formatter
co.elastic.logging.jul.EcsFormatter.serviceName=test
co.elastic.logging.jul.EcsFormatter.serviceVersion=test-version
co.elastic.logging.jul.EcsFormatter.serviceNodeName=test-node
co.elastic.logging.jul.EcsFormatter.eventDataset=testdataset
co.elastic.logging.jul.EcsFormatter.includeOrigin=true
co.elastic.logging.jul.EcsFormatter.stackTraceAsArray=true
co.elastic.logging.jul.EcsFormatter.additionalFields=key1=value1,key2=value2