File tree 3 files changed +45
-0
lines changed
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');
268
268
log (' still goes to stdout, but via console.info now' );
269
269
```
270
270
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
+
271
285
## Set dynamically
272
286
273
287
You 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) {
123
123
debug . useColors = createDebug . useColors ( ) ;
124
124
debug . color = selectColor ( namespace ) ;
125
125
debug . destroy = destroy ;
126
+ debug . extend = extend ;
126
127
//debug.formatArgs = formatArgs;
127
128
//debug.rawLog = rawLog;
128
129
@@ -146,6 +147,10 @@ module.exports = function setup(env) {
146
147
}
147
148
}
148
149
150
+ function extend ( namespace , delimiter ) {
151
+ return createDebug ( this . namespace + ( typeof delimiter !== 'undefined' ? delimiter : ':' ) + namespace ) ;
152
+ }
153
+
149
154
/**
150
155
* Enables a debug mode by namespaces. This can include modes
151
156
* separated by a colon and wildcards.
Original file line number Diff line number Diff line change @@ -64,4 +64,30 @@ describe('debug', function () {
64
64
} ) ;
65
65
} ) ;
66
66
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
+
67
93
} ) ;
You can’t perform that action at this time.
0 commit comments