UNPKG

@sap-cloud-sdk/util

Version:

SAP Cloud SDK for JavaScript general utilities

52 lines 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.local = void 0; exports.getMessageOrStack = getMessageOrStack; const node_util_1 = require("node:util"); const winston_1 = require("winston"); const { combine, timestamp, cli, printf, errors } = winston_1.format; /** * Format for local logging. */ exports.local = combine(errors({ stack: true }), timestamp(), (0, winston_1.format)(localTransformer)(), cli(), printf((info) => { // Ensure custom_fields is an object and has messageContext const messageContext = info.custom_fields && typeof info.custom_fields === 'object' && 'messageContext' in info.custom_fields ? `${(0, node_util_1.styleText)('blue', `(${info.custom_fields.messageContext})`)}: ` : ''; // Type guard to ensure message is a string const message = typeof info.message === 'string' ? info.message : ''; const trimmedMessage = message.replace(/^\s*/, ''); const paddingLength = message.length - trimmedMessage.length + messageContext.length; if (info.error) { info.level = (0, node_util_1.styleText)('inverse', info.level); } return `${(0, node_util_1.styleText)('gray', `[${info.timestamp}]`)} ${info.level} ${messageContext.padStart(paddingLength, ' ')}${trimmedMessage}`; })); /** * Gets the stack of the given error if available, otherwise the message. * @param info - Object to be transformed. * @returns The message string to be used. * @internal */ function getMessageOrStack(info) { const isString = (value) => typeof value === 'string'; // Check if it's an error with a stack trace const hasStackTrace = info.stack && info.level === 'error'; if (hasStackTrace && isString(info.stack)) { return info.stack; } if (isString(info.message)) { return info.message; } return ''; } function localTransformer(info) { return { ...info, level: info.level.toUpperCase(), message: getMessageOrStack(info) }; } //# sourceMappingURL=local.js.map