Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-927: Update README and examples #938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 36 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions examples/node/app.js
Original file line number Diff line number Diff line change
@@ -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();
20 changes: 20 additions & 0 deletions examples/node/stdout.js
Original file line number Diff line number Diff line change
@@ -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');
16 changes: 16 additions & 0 deletions examples/node/worker.js
Original file line number Diff line number Diff line change
@@ -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};