SAP-BTP-Spielwiese/app1/node_modules/@sap/logging/lib/logging-tools/Tracer.js
Markus Rettig 775ac7b58c completed step 3 from the tutorial
you must login with an BTP account in order to see the app
2024-02-08 16:13:36 +01:00

80 lines
2.2 KiB
JavaScript

'use strict';
const _ = require('lodash');
const assert = require('assert');
const util = require('util');
const Base = require('./Base');
const consts = require('../constants');
const EntryContext = require('../contexts/EntryContext');
const normalizeComponent = require('../common').normalizeComponent;
class Tracer extends Base {
constructor(logContext, location) {
super(logContext, resolveLocation(location), consts.TRACE_LEVELS, consts.DEFAULT_LEVELS.TRACER);
}
}
Object.keys(consts.TRACE_LEVELS).forEach(function (level) {
Tracer.prototype[level] = function () {
if (!this.isEnabled(level)) {
return;
}
const ctx = new EntryContext(this._logContext, {
type: 'trace',
loggingToolName: 'nodejs-tracer',
component: this._component,
level,
messageArgs: arguments,
addStack: true
});
// eslint-disable-next-line no-console
console.error(this._formatters.trace.format(ctx));
};
});
Tracer.prototype.entering = function (fnName) {
checkFunctionName(fnName);
var message = 'Entering function "' + fnName + '"';
if (arguments.length > 1) {
message += ' with arguments: ';
message += Array.prototype.slice.call(arguments, 1).map(util.inspect).join(', ');
}
this.path(message);
};
Tracer.prototype.exiting = function (fnName) {
checkFunctionName(fnName);
var message = 'Exiting function "' + fnName + '"';
if (arguments.length > 1) {
message += ' with result: ' + util.inspect(arguments[1]);
}
this.path(message);
};
Tracer.prototype.throwing = function (fnName, error) {
checkFunctionName(fnName);
var message = 'Function "' + fnName + '" throwing';
var args = error ? [error, message] : [message];
this.path.apply(this, args);
};
Tracer.prototype.catching = function (fnName, error) {
checkFunctionName(fnName);
var message = 'Function "' + fnName + '" catching';
var args = error ? [error, message] : [message];
this.path.apply(this, args);
};
function resolveLocation(location) {
assert(_.isString(location), 'Location should be a string');
return normalizeComponent(location);
}
function checkFunctionName(fnName) {
assert(_.isString(fnName), 'Function name should be a string');
}
module.exports = Tracer;