From 2310a862c59b719521e261bdf80366b47ab61b12 Mon Sep 17 00:00:00 2001 From: Anthony Chen Date: Thu, 20 Jul 2023 10:55:09 -0400 Subject: [PATCH] Issue-927: Update README and examples Passes linting and can be executed inside the examples directory --- README.md | 65 +++++++++++++++++++++++------------------ examples/node/app.js | 19 ++++++++++++ examples/node/stdout.js | 20 +++++++++++++ examples/node/worker.js | 16 ++++++++++ 4 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 examples/node/app.js create mode 100644 examples/node/stdout.js create mode 100644 examples/node/worker.js diff --git a/README.md b/README.md index e9c3e047..1746e2d0 100644 --- a/README.md +++ b/README.md @@ -20,45 +20,48 @@ $ npm install debug Example [_app.js_](./examples/node/app.js): ```js -var debug = require('debug')('http') - , http = require('http') - , name = 'My App'; +const http = require('http'); +const debug = require('../../src')('http'); // ../../src can be replaced with debug if run outside of this repository +const {worka, workb} = require('./worker'); -// fake app +const name = 'My App'; +// Fake app debug('booting %o', name); -http.createServer(function(req, res){ - debug(req.method + ' ' + req.url); - res.end('hello\n'); -}).listen(3000, function(){ - debug('listening'); +http.createServer((req, res) => { + debug(req.method + ' ' + req.url); + res.end('hello\n'); +}).listen(3000, () => { + debug('listening'); }); -// fake worker of some kind +// Fake worker of some kind +worka(); +workb(); -require('./worker'); ``` Example [_worker.js_](./examples/node/worker.js): ```js -var a = require('debug')('worker:a') - , b = require('debug')('worker:b'); +const debug = require('../../src'); // ../../src can be replaced with debug if run outside of this repository -function work() { - a('doing lots of uninteresting work'); - setTimeout(work, Math.random() * 1000); -} +const a = debug('worker:a'); +const b = debug('worker:b'); -work(); +function worka() { + a('doing lots of uninteresting work'); + setTimeout(worka, Math.random() * 1000); +} function workb() { - b('doing some work'); - setTimeout(workb, Math.random() * 2000); + b('doing some work'); + setTimeout(workb, Math.random() * 2000); } -workb(); +module.exports = {worka, workb}; + ``` The `DEBUG` environment variable is then used to enable these based on space or @@ -252,23 +255,27 @@ In Chromium-based web browsers (e.g. Brave, Chrome, and Electron), the JavaScrip Example [_stdout.js_](./examples/node/stdout.js): ```js -var debug = require('debug'); -var error = debug('app:error'); +const debug = require('../../src'); // ../../src can be replaced with debug if run outside of this repository + +const error = debug('app:error'); -// by default stderr is used +// By default stderr is used error('goes to stderr!'); -var log = debug('app:log'); -// set this namespace to log via console.log -log.log = console.log.bind(console); // don't forget to bind to console! +const log = debug('app:log'); + +// Set this namespace to log via console.log +log.log = console.log.bind(console); // Don't forget to bind to console! + log('goes to stdout'); error('still goes to stderr!'); -// set all output to go via console.info -// overrides all per-namespace log settings +// Set all output to go via console.info +// Overrides all per-namespace log settings debug.log = console.info.bind(console); error('now goes to stdout via console.info'); log('still goes to stdout, but via console.info now'); + ``` ## Extend diff --git a/examples/node/app.js b/examples/node/app.js new file mode 100644 index 00000000..21f9f10f --- /dev/null +++ b/examples/node/app.js @@ -0,0 +1,19 @@ +const http = require('http'); +const debug = require('../../src')('http'); // ../../src can be replaced with debug if run outside of this repository +const {worka, workb} = require('./worker'); + +const name = 'My App'; + +// Fake app +debug('booting %o', name); + +http.createServer((req, res) => { + debug(req.method + ' ' + req.url); + res.end('hello\n'); +}).listen(3000, () => { + debug('listening'); +}); + +// Fake worker of some kind +worka(); +workb(); diff --git a/examples/node/stdout.js b/examples/node/stdout.js new file mode 100644 index 00000000..393c22c2 --- /dev/null +++ b/examples/node/stdout.js @@ -0,0 +1,20 @@ +const debug = require('../../src'); // ../../src can be replaced with debug if run outside of this repository + +const error = debug('app:error'); + +// By default stderr is used +error('goes to stderr!'); + +const log = debug('app:log'); + +// Set this namespace to log via console.log +log.log = console.log.bind(console); // Don't forget to bind to console! + +log('goes to stdout'); +error('still goes to stderr!'); + +// Set all output to go via console.info +// Overrides all per-namespace log settings +debug.log = console.info.bind(console); +error('now goes to stdout via console.info'); +log('still goes to stdout, but via console.info now'); diff --git a/examples/node/worker.js b/examples/node/worker.js new file mode 100644 index 00000000..cfac1628 --- /dev/null +++ b/examples/node/worker.js @@ -0,0 +1,16 @@ +const debug = require('../../src'); // ../../src can be replaced with debug if run outside of this repository + +const a = debug('worker:a'); +const b = debug('worker:b'); + +function worka() { + a('doing lots of uninteresting work'); + setTimeout(worka, Math.random() * 1000); +} + +function workb() { + b('doing some work'); + setTimeout(workb, Math.random() * 2000); +} + +module.exports = {worka, workb};