2021-08-16 11:03:27 +00:00
|
|
|
import {
|
|
|
|
|
addErrorLogInit,
|
2022-09-16 19:18:54 +00:00
|
|
|
debuggerLog,
|
2021-08-16 11:03:27 +00:00
|
|
|
debuggerLogInit,
|
2022-12-07 10:28:29 +00:00
|
|
|
deleteErrorLogsInit,
|
2021-08-16 11:03:27 +00:00
|
|
|
} from "actions/debuggerActions";
|
2022-04-12 10:50:01 +00:00
|
|
|
import { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
|
2022-09-04 11:58:05 +00:00
|
|
|
import {
|
|
|
|
|
Severity,
|
|
|
|
|
LogActionPayload,
|
|
|
|
|
Log,
|
|
|
|
|
LOG_CATEGORY,
|
|
|
|
|
} from "entities/AppsmithConsole";
|
2021-04-23 13:50:55 +00:00
|
|
|
import moment from "moment";
|
|
|
|
|
import store from "store";
|
2022-09-04 11:58:05 +00:00
|
|
|
import AnalyticsUtil from "./AnalyticsUtil";
|
2022-12-07 10:28:29 +00:00
|
|
|
import { isEmpty } from "lodash";
|
2021-02-03 13:16:48 +00:00
|
|
|
|
2023-02-18 12:55:46 +00:00
|
|
|
// * @param payload - payload of the error
|
|
|
|
|
// * @param severity - severity of the error
|
|
|
|
|
// * @param category - category of the error
|
|
|
|
|
export interface ErrorObject {
|
|
|
|
|
payload: LogActionPayload;
|
|
|
|
|
severity?: Severity;
|
|
|
|
|
category?: LOG_CATEGORY;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 11:03:27 +00:00
|
|
|
function dispatchAction(action: ReduxAction<unknown>) {
|
|
|
|
|
store.dispatch(action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function log(ev: Log) {
|
2022-09-04 11:58:05 +00:00
|
|
|
if (ev.category === LOG_CATEGORY.USER_GENERATED) {
|
|
|
|
|
AnalyticsUtil.logEvent("CONSOLE_LOG_CREATED", {
|
|
|
|
|
entityName: ev.source?.name,
|
|
|
|
|
entityType: ev.source?.type,
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-12-07 10:28:29 +00:00
|
|
|
dispatchAction(debuggerLogInit([ev]));
|
2021-02-03 13:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 13:50:55 +00:00
|
|
|
function getTimeStamp() {
|
2023-02-18 12:55:46 +00:00
|
|
|
return moment().format("HH:mm:ss");
|
2021-02-03 13:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
2022-09-16 19:18:54 +00:00
|
|
|
function addLogs(logs: Log[]) {
|
|
|
|
|
dispatchAction(debuggerLog(logs));
|
2022-09-04 11:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function info(
|
|
|
|
|
ev: LogActionPayload,
|
|
|
|
|
timestamp = getTimeStamp(),
|
|
|
|
|
category = LOG_CATEGORY.PLATFORM_GENERATED,
|
|
|
|
|
) {
|
2021-04-23 13:50:55 +00:00
|
|
|
log({
|
|
|
|
|
...ev,
|
|
|
|
|
severity: Severity.INFO,
|
2022-09-04 11:58:05 +00:00
|
|
|
timestamp,
|
|
|
|
|
category,
|
2022-09-19 06:29:04 +00:00
|
|
|
occurrenceCount: 1,
|
2021-04-23 13:50:55 +00:00
|
|
|
});
|
2021-02-03 13:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
2022-09-04 11:58:05 +00:00
|
|
|
function warning(
|
|
|
|
|
ev: LogActionPayload,
|
|
|
|
|
timestamp = getTimeStamp(),
|
|
|
|
|
category = LOG_CATEGORY.PLATFORM_GENERATED,
|
|
|
|
|
) {
|
2021-04-23 13:50:55 +00:00
|
|
|
log({
|
|
|
|
|
...ev,
|
|
|
|
|
severity: Severity.WARNING,
|
2022-09-04 11:58:05 +00:00
|
|
|
timestamp,
|
|
|
|
|
category,
|
2022-09-19 06:29:04 +00:00
|
|
|
occurrenceCount: 1,
|
2021-04-23 13:50:55 +00:00
|
|
|
});
|
2021-02-03 13:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-16 11:03:27 +00:00
|
|
|
// This is used to show a log as an error
|
|
|
|
|
// NOTE: These logs won't appear in the errors tab
|
|
|
|
|
// To add errors to the errors tab use the addError method.
|
2022-09-04 11:58:05 +00:00
|
|
|
function error(
|
|
|
|
|
ev: LogActionPayload,
|
|
|
|
|
timestamp = getTimeStamp(),
|
|
|
|
|
category = LOG_CATEGORY.PLATFORM_GENERATED,
|
|
|
|
|
) {
|
2021-04-23 13:50:55 +00:00
|
|
|
log({
|
|
|
|
|
...ev,
|
|
|
|
|
severity: Severity.ERROR,
|
2022-09-04 11:58:05 +00:00
|
|
|
timestamp,
|
|
|
|
|
category,
|
2022-09-19 06:29:04 +00:00
|
|
|
occurrenceCount: 1,
|
2021-04-23 13:50:55 +00:00
|
|
|
});
|
2021-02-03 13:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 10:28:29 +00:00
|
|
|
// Function used to add errors to the error tab of the debugger
|
2023-02-18 12:55:46 +00:00
|
|
|
function addErrors(errors: ErrorObject[]) {
|
2022-12-07 10:28:29 +00:00
|
|
|
if (isEmpty(errors)) return;
|
|
|
|
|
const refinedErrors = errors.map((error) => ({
|
|
|
|
|
...error.payload,
|
|
|
|
|
severity: error.severity ?? Severity.ERROR,
|
2023-03-14 13:47:06 +00:00
|
|
|
timestamp: Date.now().toString(),
|
2022-12-07 10:28:29 +00:00
|
|
|
occurrenceCount: 1,
|
|
|
|
|
category: error.category ?? LOG_CATEGORY.PLATFORM_GENERATED,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
dispatchAction(addErrorLogInit(refinedErrors));
|
2021-08-16 11:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 10:28:29 +00:00
|
|
|
// This is used to remove errors from the error tab of the debugger
|
|
|
|
|
function deleteErrors(errors: { id: string; analytics?: Log["analytics"] }[]) {
|
|
|
|
|
if (isEmpty(errors)) return;
|
|
|
|
|
dispatchAction(deleteErrorLogsInit(errors));
|
2021-08-16 11:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 13:50:55 +00:00
|
|
|
export default {
|
2022-09-16 19:18:54 +00:00
|
|
|
addLogs,
|
2021-04-23 13:50:55 +00:00
|
|
|
info,
|
|
|
|
|
warning,
|
|
|
|
|
error,
|
2022-12-07 10:28:29 +00:00
|
|
|
addErrors,
|
|
|
|
|
deleteErrors,
|
2021-04-23 13:50:55 +00:00
|
|
|
};
|