UNPKG

@logtape/sentry

Version:

LogTape Sentry sink

53 lines (51 loc) 2.06 kB
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs'); const __sentry_core = require_rolldown_runtime.__toESM(require("@sentry/core")); //#region mod.ts function getParameterizedString(record) { let result = ""; let tplString = ""; const tplValues = []; for (let i = 0; i < record.message.length; i++) if (i % 2 === 0) { result += record.message[i]; tplString += String(record.message[i]).replaceAll("%", "%%"); } else { const value = inspect(record.message[i]); result += value; tplString += `%s`; tplValues.push(value); } const paramStr = new String(result); paramStr.__sentry_template_string__ = tplString; paramStr.__sentry_template_values__ = tplValues; return result; } /** * A platform-specific inspect function. In Deno, this is {@link Deno.inspect}, * and in Node.js/Bun it is {@link util.inspect}. If neither is available, it * falls back to {@link JSON.stringify}. * * @param value The value to inspect. * @returns The string representation of the value. */ const inspect = "Deno" in globalThis && "inspect" in globalThis.Deno && typeof globalThis.Deno.inspect === "function" ? globalThis.Deno.inspect : "util" in globalThis && "inspect" in globalThis.util && globalThis.util.inspect === "function" ? globalThis.util.inspect : JSON.stringify; /** * Gets a LogTape sink that sends logs to Sentry. * @param client The Sentry client. If omitted, the global default client is * used. * @returns A LogTape sink that sends logs to Sentry. */ function getSentrySink(client) { return (record) => { const message = getParameterizedString(record); if (client == null) client = (0, __sentry_core.getClient)(); if (record.level === "error" && record.properties.error instanceof Error) { const { error,...rest } = record.properties; client?.captureException(error, { data: { message, ...rest } }); } else client?.captureMessage(message, record.level === "trace" ? "debug" : record.level, { data: record.properties }); }; } //#endregion exports.getSentrySink = getSentrySink;