What is callsite-record?
The callsite-record npm package is used to capture and format call site information in Node.js. It is particularly useful for debugging and logging purposes, as it allows developers to obtain detailed information about the call stack, including file names, line numbers, and function names.
What are callsite-record's main functionalities?
Capture Call Site Information
This feature allows you to capture call site information by creating a new Error object and passing it to callsite-record. The renderSync method is then used to format and display the call site information.
const callsiteRecord = require('callsite-record');
function getCallSite() {
const record = callsiteRecord({ forError: new Error() });
console.log(record.renderSync());
}
getCallSite();
Asynchronous Call Site Information
This feature allows you to capture call site information asynchronously. The render method returns a promise that resolves to the formatted call site information.
const callsiteRecord = require('callsite-record');
async function getAsyncCallSite() {
const record = await callsiteRecord({ forError: new Error() }).render();
console.log(record);
}
getAsyncCallSite();
Custom Error Handling
This feature demonstrates how to use callsite-record for custom error handling. When an error is caught, the customErrorHandler function captures and logs the call site information.
const callsiteRecord = require('callsite-record');
function customErrorHandler(err) {
const record = callsiteRecord({ forError: err });
console.log(record.renderSync());
}
try {
throw new Error('Something went wrong');
} catch (err) {
customErrorHandler(err);
}
Other packages similar to callsite-record
stack-trace
The stack-trace package provides a similar functionality by capturing and parsing stack traces in Node.js. It allows you to get detailed information about the call stack, including file names, line numbers, and function names. Compared to callsite-record, stack-trace offers more flexibility in terms of parsing and manipulating stack traces.
error-stack-parser
The error-stack-parser package is used to parse error stack traces into a more readable format. It is particularly useful for front-end applications where you need to display error information to users. While callsite-record focuses on capturing call site information, error-stack-parser is more about formatting and presenting error stack traces.
traceback
The traceback package provides utilities for capturing and formatting stack traces in Node.js. It offers similar functionalities to callsite-record, such as capturing call site information and formatting stack traces. However, traceback is more focused on providing a simple API for capturing and formatting stack traces without additional features like asynchronous support.
callsite-record
Create fancy call site records for any function up in the stack for the logging purposes.
'use strict';
const createCallsiteRecord = require('callsite-record');
let record = null;
function func1 () {
record = createCallsiteRecord('func1');
}
(function func2(){
func1();
})();
console.log(record.renderSync());
⬇
Install
npm install callsite-record
API
createCallsiteRecord(functionName, [typeName]) → CallsiteRecord
Creates CallsiteRecord
for the function up in the call stack specified by functionName
. You can optionally specify a
typeName
if the function is a method. If the function is a constructor set functionName
to constructor
.
Example:
const createCallsiteRecord = require('callsite-record');
(function func1() {
(function func2() {
(function func3() {
const record = createCallsiteRecord('func2');
})();
})();
})();
CallsiteRecord
CallsiteRecord.render([renderOptions]) → Promise<String>
Renders call site record to the string.
Example:
console.log(record.render());
renderOptions.frameSize
Specifies the number of lines rendered above and below the call site in the code frame. Default: 5
.
Example:
console.log(record.render({ frameSize: 0 }));
console.log(record.render({ frameSize: 1 }));
renderOptions.stack
Specifies if stack trace should be rendered in addition to the code frame. Default: true
.
renderOptions.stackFilter
Function that will be used to filter stack frames. Function accepts 2 arguments:
stackFrame
- V8 CallSite object.idx
- index of the frame.
Default: null
.
Example:
const sep = require('path').sep;
record.render({ stackFilter: frame => frame.getFileName().indexOf(sep) > -1 });
renderOptions.renderer
Specifies the output format of the rendering. Default: renderers.default
. You can pass your own
renderer object (example implementations) or use
one of the built-in renderers:
renderers.default
Provides ANSI-colored output as shown above.
Usage:
const defaultRenderer = require('callsite-record').renderers.default;
record.render({ renderer: defaultRenderer });
renderers.noColor
Same as default
renderer but without colors.
Usage:
const noColorRenderer = require('callsite-record').renderers.noColor;
record.render({ renderer: noColorRenderer });
renderers.html
Outputs HTML that can be later decorated with the CSS and embeded into the web page. Example output.
Usage:
const htmlRenderer = require('callsite-record').renderers.html;
record.render({ renderer: html });
CallsiteRecord.renderSync([renderOptions]) → String
Sync version of the CallsiteRecord.render
.
Related
Author
Ivan Nikulin (ifaaan@gmail.com)