| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import * as Sentry from "@sentry/react";
- import Cohere from "cohere-js";
- import { isEmpty } from "lodash";
- type LogFunction = (error: Error, tags?: { [key: string]: string }) => void;
- type LogFunctions = {
- [key in Sentry.Severity]: LogFunction;
- };
- type LogFunctionBuilder = (
- scope: string,
- severity: Sentry.Severity
- ) => LogFunction;
- const logFunctionBuilder: LogFunctionBuilder = (scope, severity) => (
- error,
- tags
- ) => {
- if (process.env.ENABLE_COHERE) {
- Cohere.getSessionUrl((sessionUrl) => {
- Sentry.withScope((sentryScope) => {
- sentryScope.setTag("scope", scope);
- sentryScope.setTag("cohere_link", sessionUrl);
- sentryScope.setLevel(severity);
- if (!isEmpty(tags)) {
- sentryScope.setTags(tags);
- }
- Sentry.captureException(error);
- });
- });
- } else {
- Sentry.withScope((sentryScope) => {
- sentryScope.setTag("scope", scope);
- sentryScope.setLevel(severity);
- if (!isEmpty(tags)) {
- sentryScope.setTags(tags);
- }
- 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;
|