Skip to content

Commit 0889fd9

Browse files
authored
JUL - fix loading of boolean configs (#182)
1 parent f286e3f commit 0889fd9

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ public EcsFormatter() {
5353
serviceName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceName", null);
5454
serviceVersion= getProperty("co.elastic.logging.jul.EcsFormatter.serviceVersion", null);
5555
serviceNodeName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceNodeName", null);
56-
includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
57-
stackTraceAsArray = Boolean
58-
.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false"));
56+
includeOrigin = Boolean.parseBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
57+
stackTraceAsArray = Boolean.parseBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false"));
5958
eventDataset = getProperty("co.elastic.logging.jul.EcsFormatter.eventDataset", null);
6059
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
6160
setAdditionalFields(getProperty("co.elastic.logging.jul.EcsFormatter.additionalFields", null));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*-
2+
* #%L
3+
* Java ECS logging
4+
* %%
5+
* Copyright (C) 2019 - 2022 Elastic and contributors
6+
* %%
7+
* Licensed to Elasticsearch B.V. under one or more contributor
8+
* license agreements. See the NOTICE file distributed with
9+
* this work for additional information regarding copyright
10+
* ownership. Elasticsearch B.V. licenses this file to you under
11+
* the Apache License, Version 2.0 (the "License"); you may
12+
* not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing,
18+
* software distributed under the License is distributed on an
19+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
* KIND, either express or implied. See the License for the
21+
* specific language governing permissions and limitations
22+
* under the License.
23+
* #L%
24+
*/
25+
package co.elastic.logging.jul;
26+
27+
import com.fasterxml.jackson.databind.JsonNode;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
import java.io.IOException;
32+
import java.util.logging.LogManager;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests logging setup based on a custom logging.properties file, as well as Exception stack trace formatting as array
38+
*/
39+
public class JulIntegrationTest extends JulLoggingTest {
40+
@BeforeEach
41+
void setUp() throws IOException {
42+
LogManager.getLogManager().readConfiguration(getClass().getClassLoader().getResourceAsStream("logging.properties"));
43+
super.setUpFormatter();
44+
}
45+
46+
@Override
47+
protected EcsFormatter createEcsFormatter() {
48+
// relying on the configuration loaded from the logging.properties file
49+
return new EcsFormatter();
50+
}
51+
52+
@Test
53+
void testLogException() throws Exception {
54+
error("test", new RuntimeException("test"));
55+
JsonNode log = getLastLogLine();
56+
assertThat(log.get("log.level").textValue()).isIn("ERROR", "SEVERE");
57+
assertThat(log.get("error.message").textValue()).isEqualTo("test");
58+
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
59+
assertThat(log.get("error.stack_trace").isArray()).isTrue();
60+
assertThat(log.get("error.stack_trace").get(0).textValue()).isEqualTo("java.lang.RuntimeException: test");
61+
assertThat(log.get("error.stack_trace").get(1).textValue()).contains("at co.elastic.logging.jul.JulIntegrationTest");
62+
}
63+
64+
@Test
65+
void testLogExceptionNullMessage() throws Exception {
66+
error("test", new RuntimeException());
67+
JsonNode log = getLastLogLine();
68+
assertThat(log.get("error.message")).isNull();
69+
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
70+
assertThat(log.get("error.stack_trace").get(0).textValue()).isEqualTo("java.lang.RuntimeException");
71+
assertThat(log.get("error.stack_trace").get(1).textValue()).contains("at co.elastic.logging.jul.JulIntegrationTest");
72+
}
73+
}

jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulLoggingTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.logging.Formatter;
4040
import java.util.logging.Handler;
4141
import java.util.logging.Level;
42+
import java.util.logging.LogManager;
4243
import java.util.logging.LogRecord;
4344
import java.util.logging.Logger;
4445
import java.util.logging.StreamHandler;
@@ -109,20 +110,16 @@ public JsonNode getLastLogLine() throws IOException {
109110
}
110111

111112
@BeforeEach
112-
void setUp() {
113+
void setUp() throws IOException {
114+
// loads configuration based on the default logging.properties file
115+
LogManager.getLogManager().readConfiguration();
113116
setUpFormatter();
114117
formatter.setAdditionalFields("key1=value1,key2=value2");
115118
}
116119

117-
private void setUpFormatter() {
120+
protected void setUpFormatter() {
118121
clearHandlers();
119-
120-
formatter = new EcsFormatter();
121-
formatter.setIncludeOrigin(true);
122-
formatter.setServiceName("test");
123-
formatter.setServiceVersion("test-version");
124-
formatter.setServiceNodeName("test-node");
125-
formatter.setEventDataset("testdataset");
122+
formatter = createEcsFormatter();
126123

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

131+
protected EcsFormatter createEcsFormatter() {
132+
EcsFormatter ret = new EcsFormatter();
133+
ret.setIncludeOrigin(true);
134+
ret.setServiceName("test");
135+
ret.setServiceVersion("test-version");
136+
ret.setServiceNodeName("test-node");
137+
ret.setEventDataset("testdataset");
138+
return ret;
139+
}
140+
134141
@Test
135142
void testLogOrigin() throws Exception {
136143
debug("test");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ECS formatter
2+
co.elastic.logging.jul.EcsFormatter.serviceName=test
3+
co.elastic.logging.jul.EcsFormatter.serviceVersion=test-version
4+
co.elastic.logging.jul.EcsFormatter.serviceNodeName=test-node
5+
co.elastic.logging.jul.EcsFormatter.eventDataset=testdataset
6+
co.elastic.logging.jul.EcsFormatter.includeOrigin=true
7+
co.elastic.logging.jul.EcsFormatter.stackTraceAsArray=true
8+
co.elastic.logging.jul.EcsFormatter.additionalFields=key1=value1,key2=value2

0 commit comments

Comments
 (0)