@logtape/sentry
Version:
LogTape Sentry Sink
85 lines (84 loc) • 3.21 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSentrySink = getSentrySink;
const dntShim = __importStar(require("./_dnt.shims.js"));
const core_1 = require("@sentry/core");
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 =
// @ts-ignore: Deno global
"Deno" in dntShim.dntGlobalThis && "inspect" in globalThis.Deno &&
// @ts-ignore: Deno global
typeof globalThis.Deno.inspect === "function"
// @ts-ignore: Deno global
? globalThis.Deno.inspect
// @ts-ignore: Node.js global
: "util" in dntShim.dntGlobalThis && "inspect" in globalThis.util &&
// @ts-ignore: Node.js global
globalThis.util.inspect === "function"
// @ts-ignore: Node.js global
? 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) => {
if (client == null)
client = (0, core_1.getClient)();
client?.captureMessage(getParameterizedString(record), record.level, { data: record.properties });
};
}