@@ -56,14 +56,14 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
56
56
loggers = { } ,
57
57
appenders = { } ,
58
58
levels = {
59
- ALL : new Level ( Number . MIN_VALUE , "ALL" ) ,
60
- TRACE : new Level ( 5000 , "TRACE" ) ,
61
- DEBUG : new Level ( 10000 , "DEBUG" ) ,
62
- INFO : new Level ( 20000 , "INFO" ) ,
63
- WARN : new Level ( 30000 , "WARN" ) ,
64
- ERROR : new Level ( 40000 , "ERROR" ) ,
65
- FATAL : new Level ( 50000 , "FATAL" ) ,
66
- OFF : new Level ( Number . MAX_VALUE , "OFF" )
59
+ ALL : new Level ( Number . MIN_VALUE , "ALL" , "grey" ) ,
60
+ TRACE : new Level ( 5000 , "TRACE" , "blue" ) ,
61
+ DEBUG : new Level ( 10000 , "DEBUG" , "cyan" ) ,
62
+ INFO : new Level ( 20000 , "INFO" , "green" ) ,
63
+ WARN : new Level ( 30000 , "WARN" , "yellow" ) ,
64
+ ERROR : new Level ( 40000 , "ERROR" , "red" ) ,
65
+ FATAL : new Level ( 50000 , "FATAL" , "magenta" ) ,
66
+ OFF : new Level ( Number . MAX_VALUE , "OFF" , "grey" )
67
67
} ,
68
68
appenderMakers = {
69
69
"file" : function ( config ) {
@@ -226,9 +226,10 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
226
226
}
227
227
}
228
228
229
- function Level ( level , levelStr ) {
229
+ function Level ( level , levelStr , colour ) {
230
230
this . level = level ;
231
231
this . levelStr = levelStr ;
232
+ this . colour = colour ;
232
233
}
233
234
234
235
/**
@@ -334,7 +335,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
334
335
}
335
336
336
337
function consoleAppender ( layout ) {
337
- layout = layout || basicLayout ;
338
+ layout = layout || colouredLayout ;
338
339
return function ( loggingEvent ) {
339
340
standardOutput ( layout ( loggingEvent ) ) ;
340
341
} ;
@@ -394,6 +395,57 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
394
395
return output ;
395
396
}
396
397
398
+ /**
399
+ * Taken from masylum's fork (https://github.com/masylum/log4js-node)
400
+ */
401
+ function colorize ( str , style ) {
402
+ var styles = {
403
+ //styles
404
+ 'bold' : [ 1 , 22 ] ,
405
+ 'italic' : [ 3 , 23 ] ,
406
+ 'underline' : [ 4 , 24 ] ,
407
+ 'inverse' : [ 7 , 27 ] ,
408
+ //grayscale
409
+ 'white' : [ 37 , 39 ] ,
410
+ 'grey' : [ 90 , 39 ] ,
411
+ 'black' : [ 90 , 39 ] ,
412
+ //colors
413
+ 'blue' : [ 34 , 39 ] ,
414
+ 'cyan' : [ 36 , 39 ] ,
415
+ 'green' : [ 32 , 39 ] ,
416
+ 'magenta' : [ 35 , 39 ] ,
417
+ 'red' : [ 31 , 39 ] ,
418
+ 'yellow' : [ 33 , 39 ]
419
+ } ;
420
+ return '\033[' + styles [ style ] [ 0 ] + 'm' + str +
421
+ '\033[' + styles [ style ] [ 1 ] + 'm' ;
422
+ }
423
+
424
+ /**
425
+ * colouredLayout - taken from masylum's fork.
426
+ * same as basicLayout, but with colours.
427
+ */
428
+ function colouredLayout ( loggingEvent ) {
429
+ var timestampLevelAndCategory = colorize ( '[' + loggingEvent . startTime . toFormattedString ( ) + '] ' , 'grey' ) ;
430
+ timestampLevelAndCategory += colorize (
431
+ '[' + loggingEvent . level . toString ( ) + '] ' , loggingEvent . level . colour
432
+ ) ;
433
+ timestampLevelAndCategory += colorize ( loggingEvent . categoryName + ' - ' , 'grey' ) ;
434
+
435
+ var output = timestampLevelAndCategory + loggingEvent . message ;
436
+
437
+ if ( loggingEvent . exception ) {
438
+ output += '\n'
439
+ output += timestampLevelAndCategory ;
440
+ if ( loggingEvent . exception . stack ) {
441
+ output += loggingEvent . exception . stack ;
442
+ } else {
443
+ output += loggingEvent . exception . name + ': ' + loggingEvent . exception . message ;
444
+ }
445
+ }
446
+ return output ;
447
+ }
448
+
397
449
function messagePassThroughLayout ( loggingEvent ) {
398
450
return loggingEvent . message ;
399
451
}
@@ -533,7 +585,9 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
533
585
534
586
basicLayout : basicLayout ,
535
587
messagePassThroughLayout : messagePassThroughLayout ,
536
- patternLayout : patternLayout
588
+ patternLayout : patternLayout ,
589
+ colouredLayout : colouredLayout ,
590
+ coloredLayout : colouredLayout
537
591
} ;
538
592
}
539
593
0 commit comments