This is a Babel plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications.
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.jsasync 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 (!
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.jsasync 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
This will produce output like:
login:authenticate: authenticating user Boblogin:authenticate: no such userlogin:authenticate: authenticating user Alicelogin:authenticate: invalid passwordlogin:authenticate: authenticating user Alicelogin: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.