UNPKG

@sentry/core

Version:
54 lines (53 loc) • 2.18 kB
import { isPrimitive } from "../utils/is.js"; import { normalize } from "../utils/normalize.js"; import { GLOBAL_OBJ } from "../utils/worldwide.js"; import { SENTRY_MESSAGE_TEMPLATE } from "@sentry/conventions/attributes"; // src/logs/utils.ts /** * Formats the given values into a string. * * @param values - The values to format. * @param normalizeDepth - The depth to normalize the values. * @param normalizeMaxBreadth - The max breadth to normalize the values. * @returns The formatted string. */ function formatConsoleArgs(values, normalizeDepth, normalizeMaxBreadth) { return "util" in GLOBAL_OBJ && typeof GLOBAL_OBJ.util.format === "function" ? GLOBAL_OBJ.util.format(...values) : safeJoinConsoleArgs(values, normalizeDepth, normalizeMaxBreadth); } /** * Joins the given values into a string. * * @param values - The values to join. * @param normalizeDepth - The depth to normalize the values. * @param normalizeMaxBreadth - The max breadth to normalize the values. * @returns The joined string. */ function safeJoinConsoleArgs(values, normalizeDepth, normalizeMaxBreadth) { return values.map((value) => isPrimitive(value) ? String(value) : JSON.stringify(normalize(value, normalizeDepth, normalizeMaxBreadth))).join(" "); } /** * Checks if a string contains console substitution patterns like %s, %d, %i, %f, %o, %O, %c. * * @param str - The string to check * @returns true if the string contains console substitution patterns */ function hasConsoleSubstitutions(str) { return /%[sdifocO]/.test(str); } /** * Creates template attributes for multiple console arguments. * * @param args - The console arguments * @returns An object with template and parameter attributes */ function createConsoleTemplateAttributes(firstArg, followingArgs) { const attributes = {}; const template = new Array(followingArgs.length).fill("{}").join(" "); attributes[SENTRY_MESSAGE_TEMPLATE] = `${firstArg} ${template}`; followingArgs.forEach((arg, index) => { attributes[`sentry.message.parameter.${index}`] = arg; }); return attributes; } export { createConsoleTemplateAttributes, formatConsoleArgs, hasConsoleSubstitutions, safeJoinConsoleArgs }; //# sourceMappingURL=utils.js.map