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

Add Exceptions Type and Message #161

Merged
merged 2 commits into from
Aug 31, 2023
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
22 changes: 22 additions & 0 deletions cf-java-logging-support-core/beats/app-logs/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,28 @@ required: True
The original log message that has been written by the application.


==== exception_type

type: string

example: java.lang.NullPointerException

required: False

The Java class name of the logged exception when available.


==== exception_message

type: string

example: Something went wrong

required: False

The message of the logged exception when available.


==== stacktrace

type: array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
"index": "not_analyzed",
"type": "string"
},
"exception_message": {
"doc_values": true,
"index": "not_analyzed",
"type": "string"
},
"exception_type": {
"doc_values": true,
"index": "not_analyzed",
"type": "string"
},
"layer": {
"doc_values": true,
"index": "not_analyzed",
Expand Down
14 changes: 14 additions & 0 deletions cf-java-logging-support-core/beats/app-logs/etc/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,20 @@ app-logs:
description: |
The original log message that has been written by the application.

- name: "exception_type"
type: string
required: false
example: "java.lang.NullPointerException"
description: |
The Java class name of the logged exception when available.

- name: "exception_message"
type: string
required: false
example: "Something went wrong"
description: |
The message of the logged exception when available.

- name: "stacktrace"
type: array
required: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def fields_to_asciidoc(input, output, beat):
""".format(**dict))


docs = yaml.load(input)
docs = yaml.load(input, Loader=yaml.Loader)

# fields file is empty
if docs is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fields_to_es_template(input, output, index):
"""

# Custom properties
docs = yaml.load(input)
docs = yaml.load(input, Loader=yaml.Loader)

# No fields defined, can't generate template
if docs is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public interface Fields {
public String THREAD = "thread";
public String LEVEL = "level";
public String MSG = "msg";
public String EXCEPTION_TYPE = "exception_type";
public String EXCEPTION_MESSAGE = "exception_message";
public String STACKTRACE = "stacktrace";
public String CATEGORIES = "categories";
public String CUSTOM_FIELDS = "#cf";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.LogEvent;

import com.sap.hcp.cf.log4j2.converter.api.Log4jContextFieldSupplier;
Expand All @@ -24,6 +25,13 @@ public Map<String, Object> map(LogEvent event) {
if (!LogEventUtilities.isRequestLog(event) && event.getMessage() != null) {
fields.put(Fields.MSG, LogEventUtilities.getFormattedMessage(event));
}
if (event.getThrown() != null) {
Throwable throwable = event.getThrown();
fields.put(Fields.EXCEPTION_TYPE, throwable.getClass().getName());
if (StringUtils.isNotBlank(throwable.getMessage())) {
fields.put(Fields.EXCEPTION_MESSAGE, throwable.getMessage());
}
}
return fields;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sap.hcp.cf.log4j2.layout.suppliers;

import com.sap.hcp.cf.log4j2.converter.api.Log4jContextFieldSupplier;
import com.sap.hcp.cf.log4j2.layout.supppliers.BaseFieldSupplier;
import com.sap.hcp.cf.logging.common.Fields;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.time.Instant;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.time.Clock;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;

import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class BaseFieldSupplierTest {

@Mock
private LogEvent event;

private Log4jContextFieldSupplier baseFieldSupplier = new BaseFieldSupplier();

@Before
public void initializeEvent() {
when(event.getInstant()).thenReturn(mock(Instant.class));
}

@Test
public void addsNoExceptionFieldsWithoutException() {
Map<String, Object> fields = baseFieldSupplier.map(event);

assertThat(fields, not(hasKey(Fields.EXCEPTION_TYPE)));
assertThat(fields, not(hasKey(Fields.EXCEPTION_MESSAGE)));
}

@Test
public void mapsException() {
Exception exception = new RuntimeException("exception message");
when(event.getThrown()).thenReturn(exception);

Map<String, Object> fields = baseFieldSupplier.map(event);

assertThat(fields, hasEntry(Fields.EXCEPTION_TYPE, RuntimeException.class.getName()));
assertThat(fields, hasEntry(Fields.EXCEPTION_MESSAGE, "exception message"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import java.util.HashMap;
import java.util.Map;

import ch.qos.logback.classic.spi.ThrowableProxy;
import com.sap.hcp.cf.logback.converter.api.LogbackContextFieldSupplier;
import com.sap.hcp.cf.logging.common.Defaults;
import com.sap.hcp.cf.logging.common.Fields;
import com.sap.hcp.cf.logging.common.Markers;

import ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.commons.lang3.StringUtils;

public class BaseFieldSupplier implements LogbackContextFieldSupplier {

Expand All @@ -25,6 +27,13 @@ public Map<String, Object> map(ILoggingEvent event) {
if (!isRequestLog(event)) {
fields.put(Fields.MSG, event.getFormattedMessage());
}
if (event.getThrowableProxy() != null && event.getThrowableProxy() instanceof ThrowableProxy) {
Throwable throwable = ((ThrowableProxy) event.getThrowableProxy()).getThrowable();
fields.put(Fields.EXCEPTION_TYPE, throwable.getClass().getName());
if (StringUtils.isNotBlank(throwable.getMessage())) {
fields.put(Fields.EXCEPTION_MESSAGE, throwable.getMessage());
}
}
return fields;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.sap.hcp.cf.logback.encoder;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import com.sap.hcp.cf.logback.converter.api.LogbackContextFieldSupplier;
import com.sap.hcp.cf.logging.common.Fields;
import org.apache.commons.math3.stat.inference.GTest;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Map;

import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class BaseFieldSupplierTest {

@Mock
private ILoggingEvent event;

private LogbackContextFieldSupplier baseFieldSupplier = new BaseFieldSupplier();

@Test
public void addsNoExceptionFieldsWithoutException() {
Map<String, Object> fields = baseFieldSupplier.map(event);

assertThat(fields, not(hasKey(Fields.EXCEPTION_TYPE)));
assertThat(fields, not(hasKey(Fields.EXCEPTION_MESSAGE)));
}

@Test
public void mapsException() {
Exception exception = new RuntimeException("exception message");
when(event.getThrowableProxy()).thenReturn(new ThrowableProxy(exception));

Map<String, Object> fields = baseFieldSupplier.map(event);

assertThat(fields, hasEntry(Fields.EXCEPTION_TYPE, RuntimeException.class.getName()));
assertThat(fields, hasEntry(Fields.EXCEPTION_MESSAGE, "exception message"));
}
}