Переглянути джерело

Implement logger functions

jnfrati 4 роки тому
батько
коміт
0cc24c2df8
1 змінених файлів з 37 додано та 0 видалено
  1. 37 0
      dashboard/src/shared/error_handling/logger.ts

+ 37 - 0
dashboard/src/shared/error_handling/logger.ts

@@ -0,0 +1,37 @@
+import * as Sentry from "@sentry/react";
+
+type LogFunction = (error: Error) => void;
+type LogFunctions = {
+  [key in Sentry.Severity]: LogFunction;
+};
+
+const logFunctionBuilder = (scope: string, severity: Sentry.Severity) => (
+  error: Error
+) => {
+  Sentry.withScope((sentryScope) => {
+    sentryScope.setTag("scope", scope);
+    sentryScope.setLevel(severity);
+
+    Sentry.captureException(error);
+  });
+};
+
+function buildLogger(scope: string = "global") {
+  const logFunctions = Object.values(Sentry.Severity).reduce<LogFunctions>(
+    (acc, currentSeverity) => {
+      if (typeof currentSeverity === "string") {
+        acc[currentSeverity] = logFunctionBuilder(
+          scope,
+          Sentry.Severity.fromString(currentSeverity)
+        );
+      }
+
+      return acc;
+    },
+    {} as LogFunctions
+  );
+
+  return logFunctions;
+}
+
+export default buildLogger;