DEV Community

It’s 2022, Don’t Use the console.log() Anymore 😎

Braincuber Technologies on April 07, 2022

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 “...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

You say "don't use console.log" and then proceed to do exactly that

Collapse
 
nicolasdanelon profile image
Nicolás Danelón

clickbait.

Collapse
 
jankapunkt profile image
Jan Küster

What you actually want is

const l = (...args) => console.log(...args)
Enter fullscreen mode Exit fullscreen mode

because you might pass multiple arguments to console.log:

l('foo', 'bar') // foo bar
Enter fullscreen mode Exit fullscreen mode

but what you really want is this:

const createLog = ({ name }) => {
  const logName = `[${name}]:`
  return (...args) => console.log(logName, ...args)
}
Enter fullscreen mode Exit fullscreen mode

which allows you to prefix logs with some contextual scope:

const log = createLog({ name: 'foo' })
log('bar', 'baz') // [foo]: bar baz
Enter fullscreen mode Exit fullscreen mode

you can easily extend this function to add some type argument to use warn, debug or error instead .

Collapse
 
nicolasdanelon profile image
Nicolás Danelón

this is the way

Collapse
 
topolanekmartin profile image
Martin Topolanek

In addition you can check for dev mode in that function. Something like: process.env === 'development'.

Collapse
 
pedrohenriquebr profile image
pedrohenriquebr

Amazing 🤩

Collapse
 
skinnypetethegiraffe profile image
Bobby Plunkett

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 or winston, 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.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

I'm just going to say that there are snippet plugins where you type clg and it suggest you to convert it to an actual console.log('first') statement plus with the word "first" selected so you can easily type your identifier + what you want to log like console.log('user', user); pretty simple, huh?

Collapse
 
moopet profile image
Ben Sinclair

const l = (arg) => console.log(arg) could be written more simply as const 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.

Collapse
 
martialseron profile image
Martial Séron

Most useless article ever.
And your monkey patch function isn't even working the same way than console.log()

Clickbait

Collapse
 
brunozampieri1985 profile image
brunozampieri1985

My God... This was the most nasty clickbait I've ever clicked before

Collapse
 
khalidsaifullahfuad profile image
Khalid Saifullah Fuad

Clickbait 😡. Is there a way to downvote an article in DEV??

Collapse
 
rajeshroyal profile image
Rajesh Royal

Cheap marketing, dev ko to baksh do. Dusra medium bana daaloge.

Collapse
 
yachelee profile image
Yache Li

Use "debugger;" is much better...

Collapse
 
lexlohr profile image
Alex Lohr

You do know that console.log supports an arbitrary number of arguments. If you want to shorten the statement, use

const log = console.log.bind(console);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chris2kus31 profile image
Chuy Moreno

Hmm 🤔 I swear I just read to not use console.log but only console.log. What a twist 🙂

Collapse
 
frankwisniewski profile image
Frank Wisniewski

why console.log ?
set a breakpoint and monitor the variable in the developer tools...

Collapse
 
jmsalazardev profile image
José Migue Salazar

Use loglevel and don't waste your time reading this post.