'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;