@@ -18,6 +18,13 @@ const TransportStream = require('winston-transport');
18
18
* @extends {TransportStream }
19
19
*/
20
20
module . exports = class Console extends TransportStream {
21
+ // Keep a reference to the log, warn, and error console methods
22
+ // in case they get redirected to this transport after the logger is
23
+ // instantiated. This prevents a circular reference issue.
24
+ _consoleLog = console . log . bind ( console ) ;
25
+ _consoleWarn = console . warn . bind ( console ) ;
26
+ _consoleError = console . error . bind ( console ) ;
27
+
21
28
/**
22
29
* Constructor function for the Console transport object responsible for
23
30
* persisting log messages and metadata to a terminal or TTY.
@@ -30,7 +37,7 @@ module.exports = class Console extends TransportStream {
30
37
this . name = options . name || 'console' ;
31
38
this . stderrLevels = this . _stringArrayToSet ( options . stderrLevels ) ;
32
39
this . consoleWarnLevels = this . _stringArrayToSet ( options . consoleWarnLevels ) ;
33
- this . eol = ( typeof options . eol === 'string' ) ? options . eol : os . EOL ;
40
+ this . eol = typeof options . eol === 'string' ? options . eol : os . EOL ;
34
41
this . forceConsole = options . forceConsole || false ;
35
42
36
43
this . setMaxListeners ( 30 ) ;
@@ -52,7 +59,7 @@ module.exports = class Console extends TransportStream {
52
59
console . _stderr . write ( `${ info [ MESSAGE ] } ${ this . eol } ` ) ;
53
60
} else {
54
61
// console.error adds a newline
55
- console . error ( info [ MESSAGE ] ) ;
62
+ this . _consoleError ( info [ MESSAGE ] ) ;
56
63
}
57
64
58
65
if ( callback ) {
@@ -66,7 +73,7 @@ module.exports = class Console extends TransportStream {
66
73
console . _stderr . write ( `${ info [ MESSAGE ] } ${ this . eol } ` ) ;
67
74
} else {
68
75
// console.warn adds a newline
69
- console . warn ( info [ MESSAGE ] ) ;
76
+ this . _consoleWarn ( info [ MESSAGE ] ) ;
70
77
}
71
78
72
79
if ( callback ) {
@@ -80,7 +87,7 @@ module.exports = class Console extends TransportStream {
80
87
console . _stdout . write ( `${ info [ MESSAGE ] } ${ this . eol } ` ) ;
81
88
} else {
82
89
// console.log adds a newline.
83
- console . log ( info [ MESSAGE ] ) ;
90
+ this . _consoleLog ( info [ MESSAGE ] ) ;
84
91
}
85
92
86
93
if ( callback ) {
@@ -97,16 +104,16 @@ module.exports = class Console extends TransportStream {
97
104
* @private
98
105
*/
99
106
_stringArrayToSet ( strArray , errMsg ) {
100
- if ( ! strArray )
101
- return { } ;
107
+ if ( ! strArray ) return { } ;
102
108
103
- errMsg = errMsg || 'Cannot make set from type other than Array of string elements' ;
109
+ errMsg =
110
+ errMsg || 'Cannot make set from type other than Array of string elements' ;
104
111
105
112
if ( ! Array . isArray ( strArray ) ) {
106
113
throw new Error ( errMsg ) ;
107
114
}
108
115
109
- return strArray . reduce ( ( set , el ) => {
116
+ return strArray . reduce ( ( set , el ) => {
110
117
if ( typeof el !== 'string' ) {
111
118
throw new Error ( errMsg ) ;
112
119
}
0 commit comments