File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -268,6 +268,20 @@ error('now goes to stdout via console.info');
268268log (' still goes to stdout, but via console.info now' );
269269```
270270
271+ ## Extend
272+ You can simply extend debugger
273+ ``` js
274+ const log = require (' debug' )(' auth' );
275+
276+ // creates new debug instance with extended namespace
277+ const logSign = log .extend (' sign' );
278+ const logLogin = log .extend (' login' );
279+
280+ log (' hello' ); // auth hello
281+ logSign (' hello' ); // auth:sign hello
282+ logLogin (' hello' ); // auth:login hello
283+ ```
284+
271285## Set dynamically
272286
273287You can also enable debug dynamically by calling the ` enable() ` method :
Original file line number Diff line number Diff line change @@ -123,6 +123,7 @@ module.exports = function setup(env) {
123123 debug . useColors = createDebug . useColors ( ) ;
124124 debug . color = selectColor ( namespace ) ;
125125 debug . destroy = destroy ;
126+ debug . extend = extend ;
126127 //debug.formatArgs = formatArgs;
127128 //debug.rawLog = rawLog;
128129
@@ -146,6 +147,10 @@ module.exports = function setup(env) {
146147 }
147148 }
148149
150+ function extend ( namespace , delimiter ) {
151+ return createDebug ( this . namespace + ( typeof delimiter !== 'undefined' ? delimiter : ':' ) + namespace ) ;
152+ }
153+
149154 /**
150155 * Enables a debug mode by namespaces. This can include modes
151156 * separated by a colon and wildcards.
Original file line number Diff line number Diff line change @@ -64,4 +64,30 @@ describe('debug', function () {
6464 } ) ;
6565 } ) ;
6666
67+
68+ describe ( 'extend namespace' , function ( ) {
69+ var log ;
70+
71+ beforeEach ( function ( ) {
72+ debug . enable ( 'foo' ) ;
73+ log = debug ( 'foo' ) ;
74+ } ) ;
75+
76+ it ( 'should extend namespace' , function ( ) {
77+ var logBar = log . extend ( 'bar' ) ;
78+ expect ( logBar . namespace ) . to . be . equal ( 'foo:bar' ) ;
79+ } ) ;
80+
81+ it ( 'should extend namespace with custom delimiter' , function ( ) {
82+ var logBar = log . extend ( 'bar' , '--' ) ;
83+ expect ( logBar . namespace ) . to . be . equal ( 'foo--bar' ) ;
84+ } ) ;
85+
86+ it ( 'should extend namespace with empty delimiter' , function ( ) {
87+ var logBar = log . extend ( 'bar' , '' ) ;
88+ expect ( logBar . namespace ) . to . be . equal ( 'foobar' ) ;
89+ } ) ;
90+
91+ } ) ;
92+
6793} ) ;
You can’t perform that action at this time.
0 commit comments