babel-plugin-trace 6

This is a Babel plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications.

codemix/babel-plugin-trace

Written in JavaScript by 1 contributor

Contribute

Need Support?


we offer maintenance, support and development services for all our open source projects.

Contact Us

Babel Plugin: Trace

This is a Babel plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications.

Build Status

What?

It's common to insert console.log() statements to help keep track of the internal state of functions when writing tricky pieces of code.

A simplified version looks like this:

// login.js

async function authenticate (username, password) {
  console.log('authenticating user', username);
  const user = await db.select().from('users').where({username: username});
  if (!user) {
    console.log('no such user');
    return false;
  }
  else if (!user.checkPassword(password)) {
    console.log('invalid password');
    return false;
  }
  else if (!user.isActive) {
    console.log('user is not active');
    return false;
  }
  console.log('logging user', username, 'into the site');
  return true;
}

During development this is very useful, but it creates a lot of noise in the console, and when development of that particular piece of code is complete, the developer is likely to delete the console.log() calls. If we're lucky, they might leave comments in their place.

But this is a tragedy - that logging information is extremely useful, not only is it helpful when fixing bugs, it's a great assistance for new developers (including yourself, 6 months from now) when getting to know a codebase.

This plugin repurposes JavaScript LabeledStatements to provide a logging / tracing syntax which can be selectively enabled or disabled at the folder, file, or function level at build time.

When disabled in production it incurs no overhead.

The syntax looks like this:

// login.js

async function authenticate (username, password) {
  trace: 'authenticating user', username;
  const user = await db.select().from('users').where({username: username});
  if (!user) {
    trace: 'no such user';
    return false;
  }
  else if (!user.checkPassword(password)) {
    trace: 'invalid password';
    return false;
  }
  else if (!user.isActive) {
    trace: 'user is not active';
    return false;
  }
  trace: 'logging user', username, 'into the site';
  return true;
}

This will produce output like:

login:authenticate: authenticating user Bob
login:authenticate:   no such user
login:authenticate: authenticating user Alice
login:authenticate:   invalid password
login:authenticate: authenticating user Alice
login:authenticate: logging user Alice into the site

As well as trace:, you can also use log: and warn:, or specify your own using the aliases plugin option.

Installation

Install via npm.

npm install --save-dev babel-plugin-trace

Then, in your babel configuration (usually in your .babelrc file), add "trace" to your list of plugins:

{
  "plugins": [["trace", {
    "env": {
      "production": {
        "strip": true
      }
    }
  }]]
}

The above example configuration will remove all tracing when NODE_ENV=production.

Alternatively, you may wish to disable tracing all of the time, and enable it for certain files or functions only.

To disable tracing all of the time, use this in your .babelrc:

{
  "plugins": [["trace", {
    "strip": true
  }]]
}

Enable by filename

Enable logging for any file with login.js in the path.

TRACE_FILE=login.js babel -d ./lib ./src

Enable logging for any file with db/models or components/login in the path.

TRACE_FILE=db/models,components/login babel -d ./lib ./src

Enable for specific functions

Enable logging for any function called login() or logout().

TRACE_CONTEXT=:login,:logout babel -d ./lib ./src

Enable logging for any function in a class called User.

TRACE_CONTEXT=:User: babel -d ./lib ./src

Enable only specific logging levels

Log only warn statements.

TRACE_LEVEL=warn babel -d ./lib ./src

Log trace and warn statements.

TRACE_LEVEL=trace,warn babel -d ./lib ./src

License

Published by codemix under a permissive MIT License, see LICENSE.md.





44 other JavaScript projects


fast.jsJavaScript 2930

Faster user-land reimplementations for several common builtin native JavaScript functions.

babel-plugin-typecheckJavaScript 823

Static and runtime type checking for JavaScript in the form of a Babel plugin.

babel-plugin-closure-eliminationJavaScript 223

A Babel plugin which eliminates closures from your JavaScript wherever possible.

orientoJavaScript 212

Former official node.js driver for OrientDB. Fast, lightweight, uses the binary protocol. Now deprecated.

babel-plugin-macrosJavaScript 184

Hygienic, non-syntactic macros for JavaScript via a Babel plugin.

htmlingJavaScript 167

Polymer / HTML5 templating syntax for node.js

babel-plugin-contractsJavaScript 128

Design by Contract for JavaScript via a Babel plugin.

YiiBlocksJavaScript 70

A set of building blocks that provide common functions for Yii web applications

contractualJavaScript 52

Unobtrusive, backwards compatible, syntactic sugar for Design by contract in JavaScript.

gitignore-parserJavaScript 34

A simple .gitignore parser for node.js

modelingJavaScript 14

Fast and flexible data models for node.js and the browser.

binary-protocolJavaScript 13

Easy, fast, writers and readers for implementing custom binary protocols in node.js.

YiiJSJavaScript 11

A port of the Yii PHP framework to JavaScript

url-routeJavaScript 10

Web component providing URL routing

validatingJavaScript 10

Quick and easy validators for node.js and the browser.

mallocJavaScript 6

Simple malloc() & free() implementation for node.js, built on top of array buffers.

geonames-importerJavaScript 6

Imports geonames data into elasticsearch

dispatchingJavaScript 5

Tiny routing / dispatch library for node and the browser.

atomicbuffersJavaScript 5

Atomic `readInt32()`, `writeInt32()`, `readUInt32()` and `writeUInt32()` for node.js buffers.

castingJavaScript 5

Tiny type casting library for node.js and the browser.

classingJavaScript 5

Fluent classes for node.js and the browser.

mimingJavaScript 3

Processing and formatting for various mime types.

bootstrap-transitionJavaScript 1

Twitter Bootstrap Transition plugin packaged for component.js instead of bower

bootstrap-tooltipJavaScript 1

Twitter Bootstrap Tooltip plugin packaged for component.js instead of bower

oriento-query-builderJavaScript 1

A standalone query builder for OrientDB, designed for node.js/io.js and the browser.

obligationsJavaScript 1

Tiny JavaScript library for preconditions and postconditions, intended for use with Contractual.

cover.jsJavaScript

JQuery plugin for covering/uncovering DOM elements

bootstrap-scrollspyJavaScript

Twitter Bootstrap Scrollspy plugin packaged for component.js instead of bower

bootstrap-typeaheadJavaScript

Twitter Bootstrap Typeahead plugin packaged for component.js instead of bower

bootstrap-modalJavaScript

Twitter Bootstrap Modal plugin packaged for component.js instead of bower

bootstrap-tabJavaScript

Twitter Bootstrap Tab plugin packaged for component.js instead of bower

bootstrap-dropdownJavaScript

Twitter Bootstrap Dropdown plugin packaged for component.js instead of bower

bootstrap-carouselJavaScript

Twitter Bootstrap Carousel plugin packaged for component.js instead of bower

webrtc-playgroundJavaScript

WebRTC sample code. For testing purposes only.

flex-basicJavaScript

A basic Flex App setup for video broadcasting

bootstrap-popoverJavaScript

Twitter Bootstrap Popover plugin packaged for component.js instead of bower

bootstrap-buttonJavaScript

Twitter Bootstrap Button plugin packaged for component.js instead of bower

bootstrap-alertJavaScript

Twitter Bootstrap Alert plugin packaged for component.js instead of bower

bootstrap-affixJavaScript

Twitter Bootstrap Affix plugin packaged for component.js instead of bower

ProductManagerJavaScript

A Sample CRUD application structured by Backbone js and backed by a node restfull api.

bootstrap-collapseJavaScript

Twitter Bootstrap Collapse plugin packaged for component.js instead of bower

bootstrap-componentJavaScript

Twitter Bootstrap packaged for component.js instead of bower

elevatorJavaScript

http://samsonradu.github.io/elevator/

tabinetJavaScript

A popular romanian card game