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

[backend/frontend] PoC Frontend Error Log Collector Implementation (#1964) #1959

Open
wants to merge 17 commits into
base: release/current
Choose a base branch
from
49 changes: 49 additions & 0 deletions openbas-api/src/main/java/io/openbas/rest/log/LogApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.openbas.rest.log;

import io.openbas.rest.helper.RestBehavior;
import io.openbas.rest.log.form.LogDetailsInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LogApi extends RestBehavior {

public static final Logger logger = LoggerFactory.getLogger(LogApi.class);

@PostMapping("/api/logs")
public void logDetails(@RequestBody LogDetailsInput logDetailsInput) {
switch (logDetailsInput.getLevel()) {
case "WARN":
logger.warn(

Check warning on line 20 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L20

Added line #L20 was not covered by tests
"Message warn received: "
+ logDetailsInput.getMessage()

Check warning on line 22 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L22

Added line #L22 was not covered by tests
+ " stacktrace: "
+ logDetailsInput.getStack());
break;

Check warning on line 25 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L24-L25

Added lines #L24 - L25 were not covered by tests
case "INFO":
logger.info(

Check warning on line 27 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L27

Added line #L27 was not covered by tests
"Message info received: "
+ logDetailsInput.getMessage()

Check warning on line 29 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L29

Added line #L29 was not covered by tests
+ " stacktrace: "
+ logDetailsInput.getStack());
break;

Check warning on line 32 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L31-L32

Added lines #L31 - L32 were not covered by tests
case "DEBUG":
logger.debug(

Check warning on line 34 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L34

Added line #L34 was not covered by tests
"Message debug received: "
+ logDetailsInput.getMessage()

Check warning on line 36 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L36

Added line #L36 was not covered by tests
+ " stacktrace: "
+ logDetailsInput.getStack());
break;

Check warning on line 39 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L38-L39

Added lines #L38 - L39 were not covered by tests
default:
logger.error(

Check warning on line 41 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L41

Added line #L41 was not covered by tests
"Message error received: "
+ logDetailsInput.getMessage()

Check warning on line 43 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L43

Added line #L43 was not covered by tests
+ " stacktrace: "
+ logDetailsInput.getStack());

Check warning on line 45 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L45

Added line #L45 was not covered by tests
break;
}
}

Check warning on line 48 in openbas-api/src/main/java/io/openbas/rest/log/LogApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/LogApi.java#L48

Added line #L48 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.openbas.rest.log.form;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LogDetailsInput {

Check warning on line 8 in openbas-api/src/main/java/io/openbas/rest/log/form/LogDetailsInput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/log/form/LogDetailsInput.java#L8

Added line #L8 was not covered by tests

private String message;
private String stack;
private String level;
}
3 changes: 3 additions & 0 deletions openbas-front/src/components/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Alert, AlertTitle } from '@mui/material';
import * as PropTypes from 'prop-types';
import * as React from 'react';

import { sendErrorToBackend } from '../utils/Action.ts';
import { useFormatter } from './i18n';

class ErrorBoundaryComponent extends React.Component {
Expand All @@ -12,6 +13,8 @@ class ErrorBoundaryComponent extends React.Component {

componentDidCatch(error, stack) {
this.setState({ error, stack });
// Send the error to the backend
sendErrorToBackend(error, stack);
}

render() {
Expand Down
11 changes: 11 additions & 0 deletions openbas-front/src/utils/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosError } from 'axios';
import { FORM_ERROR } from 'final-form';
import type { Schema } from 'normalizr';
import * as R from 'ramda';
import { ErrorInfo } from 'react';
import { createIntl, createIntlCache } from 'react-intl';
import { Dispatch } from 'redux';
import Immutable from 'seamless-immutable';
Expand Down Expand Up @@ -214,3 +215,13 @@ export const bulkDeleteReferential = (uri: string, type: string, data: unknown)
throw error;
});
};

export const sendErrorToBackend = async (error: Error, stack: ErrorInfo) => {
const errorDetails = {
message: error.message,
stack: stack.componentStack,
timestamp: new Date().toISOString(),
level: 'ERROR',
};
simplePostCall('/api/logs', errorDetails);
};
Loading