Home
Forums
New posts
Search forums
What's new
New posts
New resources
New profile posts
Latest activity
Resources
Latest reviews
Search resources
Members
Current visitors
New profile posts
Search profile posts
DMCA Policy
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
FEEL FREE TO SHARE TUTORIALS, YOUR SKILS & KNOWLEDGE ON CODING, SCRIPTS, THEMES, PLUGINS OR ANY RESOURCES YOU HAVE WITH THE COMMUNITY-
Click Here To Post Your Request,
JOIN COMPUTER REPAIR FORUM
Home
Forums
TUTORIALS
CODING TUTORIALS
Node.js
Node.js Debugging
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="codeguru" data-source="post: 58" data-attributes="member: 2"><p>Proper logging is of massive utility for web apps, both during development and after deployment. What can sometimes be difficult is organizing both the code and output of logging, i.e. knowing where each log message is coming from. I recently found <a href="https://github.com/visionmedia/debug" target="_blank">[ICODE]debug[/ICODE]</a>, a Node.js utility for organized and optimized debugging.</p><p></p><p>Creating an instance of [ICODE]debug[/ICODE] is simple and you can create multiple loggers per file:</p><p></p><p></p><p>// Create multiple instances of debug</p><p>// In theory these would serve two different purposes</p><p>var debuggerA = require('debug')('worker:a'),</p><p> debuggerB = require('debug')('worker:b');</p><p></p><p>// Sample usages of the debugger</p><p>function work() {</p><p> debuggerA('doing lots of uninteresting work');</p><p> setTimeout(work, Math.random() * 1000);</p><p>}</p><p></p><p>work();</p><p></p><p>function workb() {</p><p> debuggerB('doing some work');</p><p> setTimeout(workb, Math.random() * 2000);</p><p>}</p><p></p><p>workb();</p><p></p><p></p><p><img src="http://davidwalsh.name/demo/debug-worker.png" alt="Node.js Debug" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>The namespace given to a [ICODE]debug[/ICODE] instance as you must use an environment variable to signal which loggers should go to STDOUT when the script is run:</p><p></p><p></p><p>// Show all debugger messages prefixed "worker:_____"</p><p>DEBUG=worker:* node app.js</p><p></p><p></p><p>The environment variable strategy for signaling which instances should output is brilliant as you may want only certain types of messages logged in production vs. development. Use namespaces wisely!</p><p></p><p>I was also able to use [ICODE]chalk[/ICODE] to color messages as desired:</p><p></p><p></p><p>var chalk = require('chalk');</p><p></p><p>debuggerA(chalk.red.bold('OMG an awful error!'));</p><p></p><p></p><p>[ICODE]debug[/ICODE] is one of those utilities that has a very simple purpose and accomplishes the task well. Don’t skimp when it comes to logging informative messages — they’ll help you during development and could be critical when auditing the app after a security incident!</p><p></p><p>The post <a href="https://davidwalsh.name/node-debugging" target="_blank">Node.js Debugging</a> appeared first on <a href="https://davidwalsh.name" target="_blank">David Walsh Blog</a>.</p><p></p><p><a href="https://tkjs.us/dwb" target="_blank"><img src="https://davidwalsh.name/demo/tjs_block-1.svg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></a></p><p></p><p><a href="https://davidwalsh.name/node-debugging" target="_blank">Continue reading...</a></p></blockquote><p></p>
[QUOTE="codeguru, post: 58, member: 2"] Proper logging is of massive utility for web apps, both during development and after deployment. What can sometimes be difficult is organizing both the code and output of logging, i.e. knowing where each log message is coming from. I recently found [URL='https://github.com/visionmedia/debug'][ICODE]debug[/ICODE][/URL], a Node.js utility for organized and optimized debugging. Creating an instance of [ICODE]debug[/ICODE] is simple and you can create multiple loggers per file: // Create multiple instances of debug // In theory these would serve two different purposes var debuggerA = require('debug')('worker:a'), debuggerB = require('debug')('worker:b'); // Sample usages of the debugger function work() { debuggerA('doing lots of uninteresting work'); setTimeout(work, Math.random() * 1000); } work(); function workb() { debuggerB('doing some work'); setTimeout(workb, Math.random() * 2000); } workb(); [IMG alt="Node.js Debug"]http://davidwalsh.name/demo/debug-worker.png[/IMG] The namespace given to a [ICODE]debug[/ICODE] instance as you must use an environment variable to signal which loggers should go to STDOUT when the script is run: // Show all debugger messages prefixed "worker:_____" DEBUG=worker:* node app.js The environment variable strategy for signaling which instances should output is brilliant as you may want only certain types of messages logged in production vs. development. Use namespaces wisely! I was also able to use [ICODE]chalk[/ICODE] to color messages as desired: var chalk = require('chalk'); debuggerA(chalk.red.bold('OMG an awful error!')); [ICODE]debug[/ICODE] is one of those utilities that has a very simple purpose and accomplishes the task well. Don’t skimp when it comes to logging informative messages — they’ll help you during development and could be critical when auditing the app after a security incident! The post [URL='https://davidwalsh.name/node-debugging']Node.js Debugging[/URL] appeared first on [URL='https://davidwalsh.name']David Walsh Blog[/URL]. [URL='https://tkjs.us/dwb'][IMG]https://davidwalsh.name/demo/tjs_block-1.svg[/IMG][/URL] [url="https://davidwalsh.name/node-debugging"]Continue reading...[/url] [/QUOTE]
Insert quotes…
Verification
Post reply
Richest Freecoded User
Most Freecoin
freecoded
4,838 Freecoin
Davy200
590 Freecoin
J
Johnhendrick
575 Freecoin
S
Smith16
527 Freecoin
nathan69
426 Freecoin
Laureine
415 Freecoin
A
anajeen
370 Freecoin
C
codeguru
287 Freecoin
Tekera
267 Freecoin
A
Akubay
170 Freecoin
Home
Forums
TUTORIALS
CODING TUTORIALS
Node.js
Node.js Debugging
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top