@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
157 lines • 5.71 kB
JavaScript
"use strict";
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZBLogger = void 0;
const dayjs_1 = __importDefault(require("dayjs"));
const stackTrace = __importStar(require("stack-trace"));
const typed_duration_1 = require("typed-duration");
class ZBLogger {
constructor({ loglevel, color, id, namespace, stdout, taskType, colorise, pollInterval, _tag, }) {
this._tag = _tag;
this.colorFn = color || ((m) => m);
this.taskType = taskType;
this.id = id;
if (Array.isArray(namespace)) {
namespace = namespace.join(' ');
}
this.namespace = namespace;
this.loglevel = loglevel || 'INFO';
this.stdout = stdout || console;
this.colorise = colorise !== false;
this.pollInterval = pollInterval
? typed_duration_1.Duration.milliseconds.from(pollInterval)
: pollInterval;
}
info(message, ...optionalParameters) {
if (this.loglevel === 'NONE' || this.loglevel === 'ERROR') {
return;
}
const msg = optionalParameters.length > 0
? this.makeMessage(30, message, optionalParameters)
: this.makeMessage(30, message);
this.stdout.info(msg);
}
error(message, ...optionalParameters) {
if (this.loglevel === 'NONE') {
return;
}
const msg = optionalParameters.length > 0
? this.makeMessage(50, message, optionalParameters)
: this.makeMessage(50, message);
this.stdout.error(msg);
}
debug(message, ...optionalParameters) {
if (this.loglevel !== 'DEBUG') {
return;
}
const msg = optionalParameters.length > 0
? this.makeMessage(20, message, optionalParameters)
: this.makeMessage(20, message);
if (this.stdout === console) {
this.stdout.info(this._colorise(msg));
}
else {
this.stdout.info(msg);
}
}
log(message, ...optionalParameters) {
if (this.loglevel === 'NONE' || this.loglevel === 'ERROR') {
return;
}
const msg = optionalParameters.length > 0
? this.makeMessage(30, message, optionalParameters)
: this.makeMessage(30, message);
this.stdout.info(msg);
}
makeMessage(level, message, ...optionalParameters) {
const context = makeUsefulStacktrace();
const msg = {
timestamp: new Date(),
context,
level,
message,
time: (0, dayjs_1.default)().format('YYYY MMM-DD HH:mm:ssA'),
component: this._tag,
};
if (this.id) {
msg.id = this.id;
}
if (this.pollInterval) {
msg.pollInterval = this.pollInterval;
}
if (this.namespace) {
msg.namespace = this.namespace;
}
if (this.taskType) {
msg.taskType = this.taskType;
}
if (optionalParameters.length > 0) {
msg.data = optionalParameters;
}
return JSON.stringify(msg);
}
_colorise(message) {
if (this.colorise) {
// Only colorise console
if (this.colorFn && typeof this.colorFn === 'function') {
return this.colorFn(message);
}
else {
return message;
}
}
return message;
}
}
exports.ZBLogger = ZBLogger;
function makeUsefulStacktrace() {
const frame = stackTrace.get();
/**
* In here, everything uses optional chaining, because we do not want the logger to crash
* if the stack trace is not available for some reason.
*/
return JSON.stringify(frame
.filter((callsite) => {
const callsiteFileName = callsite?.getFileName?.() ?? '';
const shouldInclude = !(callsiteFileName.includes('ZBLogger') ||
callsiteFileName.includes('StatefulLogInterceptor'));
return shouldInclude;
})
.map((callsite) => `${callsite?.getFileName?.()}:${callsite?.getLineNumber?.()} ${callsite?.getFunctionName?.()}`));
}
//# sourceMappingURL=ZBLogger.js.map