@@ -55,15 +55,15 @@ cwc.utils.LogLevel = {
55
55
* @param {string= } name
56
56
* @param {number= } logLevel
57
57
* @constructor
58
- * @final
58
+ * @export
59
59
*/
60
60
cwc . utils . Logger = function ( name = 'Logger' ,
61
61
logLevel = cwc . utils . LogLevel . NOTICE ) {
62
62
/** @type {!string } */
63
63
this . name = name ;
64
64
65
- /** @type {!Array } */
66
- this . displayName = this . name ? [ '%c' + this . name , 'font-weight: bold;' ] : [ ] ;
65
+ /** @type {!string } */
66
+ this . displayName = this . name ? '%c' + this . name : '' ;
67
67
68
68
/** @type {!number } */
69
69
this . logLevel = typeof cwc . config !== 'undefined' ?
@@ -72,107 +72,99 @@ cwc.utils.Logger = function(name = 'Logger',
72
72
/** @type {!boolean } */
73
73
this . enabled_ = ENABLE_LOGGING ;
74
74
75
- /**
76
- * Disable logging styles for specific environments like Mocha, Jasmine, ...
77
- */
75
+ /** @type {!Function } */
76
+ this . trace = function ( ) { } ;
77
+
78
+ /** @type {!Function } */
79
+ this . debug = function ( ) { } ;
80
+
81
+ /** @type {!Function } */
82
+ this . info = function ( ) { } ;
83
+
84
+ /** @type {!Function } */
85
+ this . notice = function ( ) { } ;
86
+
87
+ /** @type {!Function } */
88
+ this . warn = function ( ) { } ;
89
+
90
+ /** @type {!Function } */
91
+ this . error = function ( ) { } ;
92
+
93
+ /** @type {!Function } */
94
+ this . critical = function ( ) { } ;
95
+
96
+ /** @type {!Function } */
97
+ this . alert = function ( ) { } ;
98
+
99
+ // Disable logging styles for specific environments like Mocha, Jasmine, ...
78
100
if ( ( window [ 'mocha' ] || window [ 'jasmine' ] || window [ '__karma__' ] ) &&
79
101
this . name ) {
80
- this . displayName = [ '[' + this . name + ']' ] ;
102
+ this . displayName = '[' + this . name + ']' ;
81
103
}
82
- } ;
83
104
84
-
85
- /**
86
- * Trace logger.
87
- * @param {...* } args
88
- */
89
- cwc . utils . Logger . prototype . trace = function ( ...args ) {
90
- if ( this . enabled_ && this . logLevel >= cwc . utils . LogLevel . TRACE ) {
91
- Function . prototype . apply . apply (
92
- console . log , [ console , this . displayName . concat ( args ) ] ) ;
93
- }
105
+ this . setLogLevel ( this . logLevel ) ;
94
106
} ;
95
107
96
108
97
109
/**
98
- * Debug logger.
99
- * @param { ...* } args
110
+ * @param { !cwc.utils.LogLevel } logLevel
111
+ * @export
100
112
*/
101
- cwc . utils . Logger . prototype . debug = function ( ...args ) {
102
- if ( this . enabled_ && this . logLevel >= cwc . utils . LogLevel . DEBUG ) {
103
- Function . prototype . apply . apply (
104
- console . log , [ console , this . displayName . concat ( args ) ] ) ;
105
- }
106
- } ;
113
+ cwc . utils . Logger . prototype . setLogLevel = function ( logLevel ) {
114
+ this . logLevel = logLevel ;
107
115
116
+ // Trace logger
117
+ this . setLogger_ ( 'trace' , cwc . utils . LogLevel . TRACE , console . log ) ;
108
118
109
- /**
110
- * Info logger.
111
- * @param {...* } args
112
- */
113
- cwc . utils . Logger . prototype . info = function ( ...args ) {
114
- if ( this . enabled_ && this . logLevel >= cwc . utils . LogLevel . INFO ) {
115
- Function . prototype . apply . apply (
116
- console . log , [ console , this . displayName . concat ( args ) ] ) ;
117
- }
118
- } ;
119
+ // Debug logger
120
+ this . setLogger_ ( 'debug' , cwc . utils . LogLevel . DEBUG , console . log ) ;
119
121
122
+ // Info logger
123
+ this . setLogger_ ( 'info' , cwc . utils . LogLevel . INFO , console . log ) ;
120
124
121
- /**
122
- * Notice logger.
123
- * @param {...* } args
124
- */
125
- cwc . utils . Logger . prototype . notice = function ( ...args ) {
126
- if ( this . enabled_ && this . logLevel >= cwc . utils . LogLevel . NOTICE ) {
127
- Function . prototype . apply . apply (
128
- console . log , [ console , this . displayName . concat ( args ) ] ) ;
129
- }
130
- } ;
125
+ // Notice logger
126
+ this . setLogger_ ( 'notice' , cwc . utils . LogLevel . NOTICE , console . log ) ;
131
127
128
+ // Warn logger
129
+ this . setLogger_ ( 'warn' , cwc . utils . LogLevel . WARN , console . warn ) ;
132
130
133
- /**
134
- * Warn logger.
135
- * @param {...* } args
136
- */
137
- cwc . utils . Logger . prototype . warn = function ( ...args ) {
138
- if ( this . enabled_ && this . logLevel >= cwc . utils . LogLevel . WARNING ) {
139
- Function . prototype . apply . apply (
140
- console . warn , [ console , this . displayName . concat ( args ) ] ) ;
141
- }
142
- } ;
131
+ // Error logger
132
+ this . setLogger_ ( 'error' , cwc . utils . LogLevel . ERROR , console . error ) ;
143
133
134
+ // Critical logger
135
+ this . setLogger_ ( 'critical' , cwc . utils . LogLevel . CRITICAL , console . error ) ;
144
136
145
- /**
146
- * Error logger.
147
- * @param {...* } args
148
- */
149
- cwc . utils . Logger . prototype . error = function ( ...args ) {
150
- if ( this . logLevel >= cwc . utils . LogLevel . ERROR ) {
151
- Function . prototype . apply . apply (
152
- console . error , [ console , this . displayName . concat ( args ) ] ) ;
153
- }
137
+ // Critical logger
138
+ this . setLogger_ ( 'alert' , cwc . utils . LogLevel . ALERT , console . error ) ;
154
139
} ;
155
140
156
141
157
142
/**
158
- * Critical logger.
159
- * @param {...* } args
143
+ * @param {!string } name
144
+ * @param {!cwc.utils.LogLevel } logLevel
145
+ * @param {!Function } logger
146
+ * @private
160
147
*/
161
- cwc . utils . Logger . prototype . critical = function ( ...args ) {
162
- if ( this . logLevel >= cwc . utils . LogLevel . CRITICAL ) {
163
- Function . prototype . apply . apply (
164
- console . error , [ console , this . displayName . concat ( args ) ] ) ;
148
+ cwc . utils . Logger . prototype . setLogger_ = function ( name , logLevel , logger ) {
149
+ // Enable logger for all errors and higher by default.
150
+ if ( ( this . enabled_ || this . logLevel <= 3 ) && this . logLevel >= logLevel ) {
151
+ this [ name ] = this . log_ ( logger ) ;
152
+ } else {
153
+ this [ name ] = function ( ) { } ;
165
154
}
166
155
} ;
167
156
168
157
169
158
/**
170
- * Alert logger.
171
- * @param {...* } args
159
+ * @param {!Function } logger
160
+ * @return {Function }
161
+ * @private
172
162
*/
173
- cwc . utils . Logger . prototype . alert = function ( ...args ) {
174
- if ( this . logLevel >= cwc . utils . LogLevel . ALERT ) {
175
- Function . prototype . apply . apply (
176
- console . error , [ console , this . displayName . concat ( args ) ] ) ;
163
+ cwc . utils . Logger . prototype . log_ = function ( logger ) {
164
+ if ( this . displayName . includes ( '%c' ) ) {
165
+ return Function . prototype . bind . call ( logger , console , this . displayName ,
166
+ 'font-weight: bold;' ) ;
167
+ } else {
168
+ return Function . prototype . bind . call ( logger , console , this . displayName ) ;
177
169
}
178
170
} ;
0 commit comments