@@ -53,9 +53,11 @@ class Logger {
5353 this . noColor = false ;
5454
5555 this . _getDate = ( ) => ( new Date ( ) ) . toISOString ( ) ;
56+
57+ this . _customizedConsole = console ;
5658 }
5759
58- createNamedLogger ( name ) {
60+ createNamedLogger ( name ) {
5961 return new Logger ( name )
6062 }
6163
@@ -68,6 +70,14 @@ class Logger {
6870
6971 }
7072
73+ setLogStream ( newStream ) {
74+ if ( newStream . writable ) {
75+ this . _customizedConsole = new console . Console ( newStream ) ;
76+ } else {
77+ throw "invalid writable stream object" ;
78+ }
79+ }
80+
7181 setLevelNoColor ( ) {
7282 this . noColor = true ;
7383 }
@@ -85,36 +95,23 @@ class Logger {
8595 }
8696
8797 log ( ...args ) {
88-
89- for ( const idx in args ) {
90- const arg = args [ idx ] ;
91- if ( typeof arg === "string" ) {
92- this . command += arg ;
93- } else {
94- try {
95- this . command += JSON . stringify ( arg ) ;
96- } catch {
97- this . command += arg ;
98- }
99- }
100- if ( args . length > 1 && idx < args . length - 1 ) {
101- this . command += " " ;
102- }
103- }
104-
98+ this . append ( ...args ) ;
10599 if ( ! this . noColor ) {
106100 this . command += CONFIG . SYSTEM . reset ;
107101 }
108- console . log ( this . command ) ;
102+ this . _print ( this . command ) ;
109103 // Save last command if we need to use for joint
110104 this . lastCommand = this . command ;
111105 this . command = '' ;
112106 return this ;
113107 }
114108
109+ // deprecated
115110 joint ( ) {
111+ console . error ( "node-color-log warning: `joint` is deprecated, please use `append`" ) ;
112+
116113 // Clear the last line
117- console . log ( CONFIG . SYSTEM . backoneline + CONFIG . SYSTEM . cleanthisline ) ;
114+ this . _print ( CONFIG . SYSTEM . backoneline + CONFIG . SYSTEM . cleanthisline ) ;
118115
119116 // Reset the command to let it joint the next
120117 // And print from the position of last line
@@ -137,7 +134,7 @@ class Logger {
137134 }
138135
139136 getPrefix ( ) {
140- if ( this . name ) {
137+ if ( this . name ) {
141138 return `${ this . _getDate ( ) } [${ this . name } ]` ;
142139 } else {
143140 return this . _getDate ( ) ;
@@ -148,7 +145,7 @@ class Logger {
148145 if ( ticket in CONFIG . FONT ) {
149146 this . command += CONFIG . FONT [ ticket ] ;
150147 } else {
151- this . warn ( "node-color-log: Font color not found! Use the default." )
148+ console . error ( "node-color-log warning : Font color not found! Use the default." )
152149 }
153150 return this ;
154151 }
@@ -157,7 +154,7 @@ class Logger {
157154 if ( ticket in CONFIG . BACKGROUND ) {
158155 this . command += CONFIG . BACKGROUND [ ticket ] ;
159156 } else {
160- this . warn ( "node-color-log: Background color not found! Use the default." )
157+ console . error ( "node-color-log warning : Background color not found! Use the default." )
161158 }
162159 return this ;
163160 }
@@ -200,12 +197,12 @@ class Logger {
200197 if ( ticket in CONFIG . FONT ) {
201198 command += CONFIG . FONT [ ticket ] ;
202199 } else {
203- this . warn ( "node-color-log: Font color not found! Use the default." )
200+ console . error ( "node-color-log warning : Font color not found! Use the default." )
204201 }
205202 command += text ;
206203
207204 command += CONFIG . SYSTEM . reset ;
208- console . log ( command ) ;
205+ this . _print ( command ) ;
209206 }
210207
211208 bgColorLog ( ticket , text , setting ) {
@@ -216,12 +213,12 @@ class Logger {
216213 if ( ticket in CONFIG . BACKGROUND ) {
217214 command += CONFIG . BACKGROUND [ ticket ] ;
218215 } else {
219- this . warn ( "node-color-log: Background color not found! Use the default." )
216+ console . error ( "node-color-log warning : Background color not found! Use the default." )
220217 }
221218 command += text ;
222219
223220 command += CONFIG . SYSTEM . reset ;
224- console . log ( command ) ;
221+ this . _print ( command ) ;
225222 }
226223
227224 colorLog ( ticketObj , text , setting ) {
@@ -232,18 +229,18 @@ class Logger {
232229 if ( ticketObj . font in CONFIG . FONT ) {
233230 command += CONFIG . FONT [ ticketObj . font ] ;
234231 } else {
235- this . warn ( "node-color-log: Font color not found! Use the default." )
232+ console . error ( "node-color-log warning : Font color not found! Use the default." )
236233 }
237234 if ( ticketObj . bg in CONFIG . BACKGROUND ) {
238235 command += CONFIG . BACKGROUND [ ticketObj . bg ]
239236 } else {
240- this . warn ( "node-color-log: Background color not found! Use the default." )
237+ console . error ( "node-color-log warning : Background color not found! Use the default." )
241238 }
242239
243240 command += text ;
244241
245242 command += CONFIG . SYSTEM . reset ;
246- console . log ( command ) ;
243+ this . _print ( command ) ;
247244 }
248245
249246 error ( ...args ) {
@@ -255,9 +252,9 @@ class Logger {
255252 this . log ( d , " [ERROR] " , ...args ) ;
256253 } else {
257254 const d = this . getPrefix ( ) ;
258- this . log ( d + " " ) . joint ( )
259- . bgColor ( 'red' ) . log ( '[ERROR]' ) . joint ( )
260- . log ( " " ) . joint ( )
255+ this . append ( d + " " )
256+ . bgColor ( 'red' ) . append ( '[ERROR]' ) . reset ( )
257+ . append ( " " )
261258 . color ( 'red' ) . log ( ...args ) ;
262259 }
263260 }
@@ -271,9 +268,9 @@ class Logger {
271268 this . log ( d , " [WARN] " , ...args ) ;
272269 } else {
273270 const d = this . getPrefix ( ) ;
274- this . log ( d + " " ) . joint ( )
275- . bgColor ( 'yellow' ) . color ( 'black' ) . log ( '[WARN]' ) . joint ( )
276- . log ( " " ) . joint ( )
271+ this . append ( d + " " )
272+ . bgColor ( 'yellow' ) . color ( 'black' ) . append ( '[WARN]' ) . reset ( )
273+ . append ( " " )
277274 . color ( 'yellow' ) . log ( ...args ) ;
278275 }
279276 }
@@ -287,9 +284,9 @@ class Logger {
287284 this . log ( d , " [INFO] " , ...args ) ;
288285 } else {
289286 const d = this . getPrefix ( ) ;
290- this . log ( d + " " ) . joint ( )
291- . bgColor ( 'green' ) . color ( 'black' ) . log ( '[INFO]' ) . joint ( )
292- . log ( " " ) . joint ( )
287+ this . append ( d + " " )
288+ . bgColor ( 'green' ) . color ( 'black' ) . append ( '[INFO]' ) . reset ( )
289+ . append ( " " )
293290 . color ( 'green' ) . log ( ...args ) ;
294291 }
295292 }
@@ -303,9 +300,9 @@ class Logger {
303300 this . log ( d , " [DEBUG] " , ...args ) ;
304301 } else {
305302 const d = this . getPrefix ( ) ;
306- this . log ( d + " " ) . joint ( )
307- . bgColor ( 'cyan' ) . color ( 'black' ) . log ( "[DEBUG]" ) . joint ( )
308- . log ( ' ' ) . joint ( )
303+ this . append ( d + " " )
304+ . bgColor ( 'cyan' ) . color ( 'black' ) . append ( "[DEBUG]" ) . reset ( )
305+ . append ( ' ' )
309306 . color ( 'cyan' )
310307 . log ( ...args ) ;
311308 }
@@ -320,9 +317,9 @@ class Logger {
320317 this . log ( d , " [SUCCESS] " , ...args ) ;
321318 } else {
322319 const d = this . getPrefix ( ) ;
323- this . log ( d + " " ) . joint ( )
324- . bgColor ( 'green' ) . color ( 'black' ) . log ( "[SUCCESS]" ) . joint ( )
325- . log ( ' ' ) . joint ( )
320+ this . append ( d + " " )
321+ . bgColor ( 'green' ) . color ( 'black' ) . append ( "[SUCCESS]" ) . reset ( )
322+ . append ( ' ' )
326323 . color ( 'green' )
327324 . log ( ...args ) ;
328325 }
@@ -336,15 +333,44 @@ class Logger {
336333 if ( setting [ item ] === true ) {
337334 command += CONFIG . SYSTEM [ item ] ;
338335 } else if ( setting [ item ] !== false ) {
339- this . warn ( `node-color-log: The value ${ item } should be boolean.` )
336+ console . error ( `node-color-log warning : The value ${ item } should be boolean.` )
340337 }
341338 } else {
342- this . warn ( `node-color-log: ${ item } is not valid in setting.` )
339+ console . error ( `node-color-log warning : ${ item } is not valid in setting.` )
343340 }
344341 }
345342 return command ;
346343 }
347344
345+ // helper function to output the the log to stream
346+ _print ( ...args ) {
347+ this . _customizedConsole . log ( ...args ) ;
348+ }
349+
350+ // helper function to append the command buffer
351+ append ( ...args ) {
352+ for ( const idx in args ) {
353+ const arg = args [ idx ] ;
354+ if ( typeof arg === "string" ) {
355+ this . command += arg ;
356+ } else {
357+ try {
358+ this . command += JSON . stringify ( arg ) ;
359+ } catch {
360+ this . command += arg ;
361+ }
362+ }
363+ if ( args . length > 1 && idx < args . length - 1 ) {
364+ this . command += " " ;
365+ }
366+ }
367+ return this ;
368+ }
369+
370+ reset ( ) {
371+ this . command += CONFIG . SYSTEM . reset ;
372+ return this ;
373+ }
348374}
349375
350376const logger = new Logger ( ) ;
0 commit comments