npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

debug-trace

v2.2.3

Published

Adds a handy `trace` flag to the console object to prepend the file and line number

Downloads

5,556

Readme

debug-trace

This fork of console-trace adds the following features:

  • work with callsite >= version 1.0.0
  • work with https://github.com/visionmedia/debug (and print the caller of debug instead of console)
  • provide an easy to override formatting function console.format e.g.:
  // overridable console string prefix formatting function
  console.format = function (c) {
    return c.getDate() + ": [" + c.filename + ":" + c.getLineNumber() + "] " + c.functionName;
  };

Available methods from V8 JavaScript stack trace API

  • getThis: returns the value of this
  • getTypeName: returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.
  • getFunction: returns the current function
  • getFunctionName: returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
  • getMethodName: returns the name of the property of this or one of its prototypes that holds the current function
  • getFileName: if this function was defined in a script returns the name of the script
  • getLineNumber: if this function was defined in a script returns the current line number
  • getColumnNumber: if this function was defined in a script returns the current column number
  • getEvalOrigin: if this function was created using a call to eval returns a CallSite object representing the location where eval was called
  • isToplevel: is this a toplevel invocation, that is, is this the global object?
  • isEval: does this call take place in code defined by a call to eval?
  • isNative: is this call in native V8 code?
  • isConstructor: is this a constructor call?

Additional method

  • getDate(): actual date formatted like this: "2016-10-04 07:18:46.719"

Additional properties

  • filename: getFileName without the base path: console.traceOptions.cwd
  • method: console method name like log, error ect.
  • functionName: call.getFunctionName() || 'anonymous'

Extends the native Node.JS console object to prefix logging functions with the CallSite information.

To read more about runtime stack trace introspection you can refer to this article.

Installation

$ npm install debug-trace

Syntax:

require('debug-trace')([options])

Available Options:

  • always - (Boolean: defaults to false) always print the callsite info even without accessing methods from the t or traced getters.
  • cwd - (String: defaults to process.cwd()) the path that will be stripped from the callsite info
  • colors - (Boolean|Object: defaults to undefined) terminal colors support flag or a custom color object
  • right - (Boolean: defaults to false) callsite alignment flag, when true prints infos on the right
  • overwriteDebugLog - (Function: defaults to console.log) overwrites debug module log function, can be turned off with overwriteDebugLog: false
  • patchOutput - (Boolean: defaults to true) monkey patches process.stdout.write and process.stderr.write with console.log and console.error in case it is called from debug (function with the name log)

Examples:

require('debug-trace')

You can add the t or traced getter to your calls to obtain a stacktrace:

console.t.log('a');
console.traced.log('a');

You can also make every console call trace:

require('debug-trace')({
  always: true,
})

...

console.log('a');     // tracing
console.error('a');   // tracing

You can align the callsite infos to the right

require('debug-trace')({
  always: true,
  right: true
})

...

console.log('a');     // tracing right
console.error('a');   // tracing right

You can change defaults colors too

require('./debug-trace')({
  always: true,
  colors: {
    warn: '35',
    info: '32'
  }
})

...

console.warn('a');    // magenta
console.info('a');    // green

To customize the string that's prefixed to the calls, override the console.traceFormat function.

Beyond console

If you have more sophisticated logging needs, or don't wish to extend console, I suggest you look at tracer.

Credits

I only added some functionality to the original console-trace:

License

MIT License