Skip to content

Commit 909ee82

Browse files
committed
Support the devtools console.log api (multiple strings) for polymer logging
Fixes #3574
1 parent 1b02e96 commit 909ee82

2 files changed

Lines changed: 83 additions & 8 deletions

File tree

‎src/lib/base.html‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,40 @@
156156
}
157157
},
158158

159-
_log: console.log.apply.bind(console.log, console),
160-
_warn: console.warn.apply.bind(console.warn, console),
161-
_error: console.error.apply.bind(console.error, console),
159+
_logger: function(level, args) {
160+
// accept ['foo', 'bar'] and [['foo', 'bar']]
161+
if (args.length === 1 && Array.isArray(args[0])) {
162+
args = args[0];
163+
}
164+
// only accept logging functions
165+
switch(level) {
166+
case 'log':
167+
case 'warn':
168+
case 'error':
169+
console[level].apply(console, args);
170+
break;
171+
}
172+
},
173+
_log: function() {
174+
var args = Array.prototype.slice.call(arguments, 0);
175+
this._logger('log', args);
176+
},
177+
_warn: function() {
178+
var args = Array.prototype.slice.call(arguments, 0);
179+
this._logger('warn', args);
180+
},
181+
_error: function() {
182+
var args = Array.prototype.slice.call(arguments, 0);
183+
this._logger('error', args);
184+
},
162185
_logf: function(/* args*/) {
163-
return this._logPrefix.concat([this.is]).concat(Array.prototype.slice.call(arguments, 0));
186+
return this._logPrefix.concat(this.is).concat(Array.prototype.slice.call(arguments, 0));
164187
}
165-
166188
};
167189

168190
Polymer.Base._logPrefix = (function(){
169-
var color = window.chrome || (/firefox/i.test(navigator.userAgent));
191+
// only Firefox, Chrome, and Safari support colors in console logging
192+
var color = (window.chrome && !(/edge/i.test(navigator.userAgent))) || (/firefox/i.test(navigator.userAgent));
170193
return color ? ['%c[%s::%s]:', 'font-weight: bold; background-color:#EEEE00;'] : ['[%s::%s]:'];
171194
})();
172195

‎test/unit/base.html‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</head>
2424
<body>
2525
<script>
26-
(function() {
26+
suite('Polymer.Base', function() {
2727

2828
var Child;
2929
var instance;
@@ -139,7 +139,59 @@
139139

140140
});
141141

142-
})();
142+
suite('Logging', function() {
143+
var logStub;
144+
setup(function() {
145+
logStub = sinon.stub(console, 'log');
146+
});
147+
teardown(function() {
148+
logStub.restore();
149+
});
150+
151+
test('logger is an entry point for console', function() {
152+
Polymer.Base._logger('log', ['hi']);
153+
assert.equal(logStub.callCount, 1);
154+
});
155+
156+
test('only allow logging methods', function() {
157+
var warnStub = sinon.stub(console, 'warn');
158+
var errorStub = sinon.stub(console, 'error');
159+
var tableStub = sinon.stub(console, 'table');
160+
var args = ['hi'];
161+
Polymer.Base._logger('warn', args);
162+
Polymer.Base._logger('error', args);
163+
Polymer.Base._logger('table', args);
164+
warnStub.restore();
165+
errorStub.restore();
166+
tableStub.restore();
167+
assert.equal(warnStub.callCount, 1);
168+
assert.equal(errorStub.callCount, 1);
169+
assert.equal(tableStub.callCount, 0);
170+
});
171+
172+
test('logging methods accept strings or an array', function() {
173+
var expected = ['hello', 'world'];
174+
Polymer.Base._log('hello', 'world');
175+
assert.deepEqual(logStub.args[0], expected, 'strings');
176+
Polymer.Base._log(['hello', 'world']);
177+
assert.deepEqual(logStub.args[1], expected, 'array');
178+
});
179+
180+
test('logf will print the element name and function', function() {
181+
Child.created = function() {
182+
this._log(this._logf('ready', 'ready now!'));
183+
}
184+
instance.createdCallback();
185+
assert.equal(logStub.callCount, 1);
186+
var expected = Polymer.Base._logPrefix.concat([
187+
'x-child',
188+
'ready',
189+
'ready now!'
190+
]);
191+
assert.deepEqual(logStub.args[0], expected);
192+
});
193+
});
194+
});
143195
</script>
144196
</body>
145197
</html>

0 commit comments

Comments
 (0)