We as JavaScript developers usually use console.log() to test the output or just for fun. Even I can bet that our (including mine) first code was “...
For further actions, you may consider blocking this person and/or reporting abuse
You say "don't use console.log" and then proceed to do exactly that
clickbait.
What you actually want is
because you might pass multiple arguments to console.log:
but what you really want is this:
which allows you to prefix logs with some contextual scope:
you can easily extend this function to add some
type
argument to usewarn
,debug
orerror
instead .this is the way
In addition you can check for dev mode in that function. Something like: process.env === 'development'.
Amazing 🤩
By aliasing
console.log
you gain nothing, shortening the name to a single letter is bad practice (you want names to have meaning), and honestly, this adds unneeded complexity to the code (which isn't much, but everything adds up).If you want a better logging solution look into actual logging libraries such as
pino
orwinston
, and if they don't have the features you need, you can use them as a reference when writing a new solution.I do like the idea of modules, just trying to find the best place to apply them is sometimes difficult, and I do not believe
console.log
is one of those places if you're not extending functionality.I'm just going to say that there are snippet plugins where you type
clg
and it suggest you to convert it to an actualconsole.log('first')
statement plus with the word "first" selected so you can easily type your identifier + what you want to log likeconsole.log('user', user);
pretty simple, huh?const l = (arg) => console.log(arg)
could be written more simply asconst l = console.log
You're talking about saving a few keypresses to make somthing a little more opaque, but then in your next example, you've called it
log
which is back up to three characters.Most useless article ever.
And your monkey patch function isn't even working the same way than
console.log()
Clickbait
My God... This was the most nasty clickbait I've ever clicked before
Clickbait 😡. Is there a way to downvote an article in DEV??
Cheap marketing, dev ko to baksh do. Dusra medium bana daaloge.
Use "debugger;" is much better...
You do know that
console.log
supports an arbitrary number of arguments. If you want to shorten the statement, useHmm 🤔 I swear I just read to not use console.log but only console.log. What a twist 🙂
why console.log ?
set a breakpoint and monitor the variable in the developer tools...
Use loglevel and don't waste your time reading this post.