@sap-cloud-sdk/util
Version:
SAP Cloud SDK for JavaScript general utilities
55 lines • 2.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.local = void 0;
exports.getMessageOrStack = getMessageOrStack;
const chalk_1 = __importDefault(require("chalk"));
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
? `${chalk_1.default.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 = chalk_1.default.inverse(info.level);
}
return `${chalk_1.default.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