|
| 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 | +} |
0 commit comments