Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When debugging the applications that work with pools of streams or sockets, the debug stream often comes out interleaved making it impossible to distinguish between streams. It is a common knowledge that unique ids could be assigned to each stream and be used in logging to solve this. However, this solution is rather ad-hoc and usually applied only during manual debugging. Introduce `debug.unique([ format ])` method that returns function with the same signature as `debug`. This function however prepends either `'%d '` or `format + ' '` (depending on the presence of `format`) to the first argument of original `debug` function, and supplies unique integer as a second argument, shifting the rest to the right. Example: ``` const debug = require('debug')('ns'); class Instance { constructor() { this.debug = debug.unique('id=%d '); } method() { this.debug('method data=%j', {}); // "id=123 method data={}" } attach(other) { this.debug('attach to=%d', other.debug.id()); // "id=123 attach to=456" } } ```
- Loading branch information