Live data from Hacker News

Use console.log() like a pro (2020)

markodenic.com

111–120 of 156 posts

Re: Use console.log() like a pro (2020)

#111
One missing mention is `console.groupCollapsed(label)` which is handy for nesting large dumps of data that you don't want to steal visual real estate.

Eg: console.groupCollapsed('data$ at load'); console.groupCollapsed('GridData'); console.table(data$.GridData.toList()); console.groupEnd(); console.groupCollapsed('Loads'); console.table(data$.Loads.toList()); console.groupEnd(); console.groupCollapsed('Drv'); console.table(data$.Drv.toList()); console.groupEnd(); console.groupEnd();

This will present `> data$ at load` and clicking on the chevron will open the data showing the list of entries and clicking on their chevrons will show the table for each.

Re: Use console.log() like a pro (2020)

#112
post #108

Very important for backend is %j (e.g.: console.log('%j', variable); which can output complex json objects to the terminal. I often write it to the terminal and then copy it to an online json viewer so it is formatted in a viewable format.

do you know about util.inspect? https://nodejs.org/api/util.html#util_util_inspect_object_sh...

I did not. Thanks!

Re: Use console.log() like a pro (2020)

#113

I think the very fact that you have to rely on things like that when debugging speaks volumes about the language deficiencies. Or maybe it's just the tooling or environments where JS is executed? In any case, the debugging experience is probably the biggest reason why I dislike modern web dev and tend to steer my career towards back-end.

I don’t understand your comment. Firefox and Chrome both have actual debuggers as well, but many times console.log is a faster way. What are the deficiencies you speak of?

I just think there are easier ways to debug your code than including logging statements between your lines. And in some environments I have never felt the need to do so.

I use the browser debuggers as well but I never saw a JS stack trace approaching readability of a C# stack trace, and there are also other things that make the entire debugging experience more troublesome. Maybe that's just because I have very little front-end experience (and no workflows and IDEs properly set up to work with particular frameworks). Or maybe it's because of the JS ecosystem. Threads like this one make me lean towards the latter.

Re: Use console.log() like a pro (2020)

#114
> Using console.log() for JavaScript debugging is the most common practice among developers. But, there is more…

Cut your debugging time, knowledge of console.log, and mental churn in half and set up your tooling to use a `debugger` statement. The console.log method may be used heavily but it’s actually a bad practice and often leaves code littered with log statements. Even for the purpose of logging itself you should use a logging library for serious development.

You should use a debugger in every language you can for development.

Neat tricks though.

Re: Use console.log() like a pro (2020)

#116
post #97

I think the very fact that you have to rely on things like that when debugging speaks volumes about the language deficiencies. Or maybe it's just the tooling or environments where JS is executed? In any case, the debugging experience is probably the biggest reason why I dislike modern web dev and tend to steer my career towards back-end.

You don't have to "rely" on it just like you don't have the rely on `fmt.Println()` in golang for debugging. A language/runtime/framework gives you options and you pick your poison, browsers offer you step by step debuggers and REPL out of the box and I actually think it's awesome in terms of debugging, you can of course only use console.log(), and it's a good place to start. When you're ready to move on there are ma…

What worries me is that, apparently, there are many people with a lot of web dev experience who haven't moved on. Why haven't they moved on? I don't understand it.

I have yet to see a mid-level engineer using the same method when debugging C# code.

Despite all that, I try to refrain from strong statements about technologies. Just throwing around ideas.

Re: Use console.log() like a pro (2020)

#117

Please don't add colours to your console.log statements. It might look nice in the Chrome console, but as soon as your messages end up elsewhere, in a terminal or text file for instance, it makes the statements look very messy.

I love adding colors to my logs whether they are bash or nodejs, but I generally check if the context is tty or not before applying colors.

Re: Use console.log() like a pro (2020)

#118
Is there any way to add logging that is stripped at build time? Like in C# if I have a “Debug.WriteLine” it doesn’t exist if built in release mode. I now build ts to js with a “dev” config, could that not strip out all console.debug for me?

Re: Use console.log() like a pro (2020)

#119

> Using console.log() for JavaScript debugging is the most common practice among developers. But, there is more… Cut your debugging time, knowledge of console.log, and mental churn in half and set up your tooling to use a `debugger` statement. The console.log method may be used heavily but it’s actually a bad practice and often leaves code littered with log statements. Even for the purpose of logging itself you shoul…

If you want to quickly log stuff without adding a log statement, you can right click the line gutter in sources and add a logpoint. Similar to a breakpoint, but logs every time it hits that line. Not modifying the source code so you don't have to remember about removing it.

Re: Use console.log() like a pro (2020)

#120

Console.group is one of my favorite features but Chrome does not handle filtering it very well. Basically, if you want to filter on a certain term, all the groups will remain, even if nothing from those groups (title, subfields, otherwise) matches. There has been a Chromium bug open since 2014: https://bugs.chromium.org/p/chromium/issues/detail?id=363796

Came to post this. been following that issue for 5 years now and still no updates. Maybe if more people star it it'll eventually get some attention :\
Post reply on HN