admin管理员组文章数量:1356339
I'm trying to create a custom logger which just wraps a console.log
but always adds something to the start which makes it absolutely clear that the log is ing from my plugin.
I have e up with the following code:
var log = function(param1, param2) {
if (typeof param3 !== 'undefined') {
console.log('[MyPlugin]', param1, param2, param3);
} else if (typeof param2 !== 'undefined') {
console.log('[MyPlugin]', param1, param2);
}
};
This allows the developer to run the following:
log('foo', 'bar');
// Outputs '[MyPlugin] foo bar'
log('foo');
// Outputs '[MyPlugin] foo'
But I hope that this can be improved upon.
The issues with this implementation are:
- The logging function only allows two parameters. It would be better if it could accept many.
- There's lots of repetition (multiple
console.log
calls).
What I have tried.
I thought maybe the ES6 spread-operator would work:
var log = function(...params) {
console.log('[MyPlugin]', params);
};
Which allows the developer to run:
log('foo', 'bar');
// Outputs '[MyPlugin] Array [ "foo", "bar" ]'
log('foo');
// Outputs '[MyPlugin] Array [ "foo" ]'
You can see that the output is not the same as in the original function.
Is there a better solution?
I'm trying to create a custom logger which just wraps a console.log
but always adds something to the start which makes it absolutely clear that the log is ing from my plugin.
I have e up with the following code:
var log = function(param1, param2) {
if (typeof param3 !== 'undefined') {
console.log('[MyPlugin]', param1, param2, param3);
} else if (typeof param2 !== 'undefined') {
console.log('[MyPlugin]', param1, param2);
}
};
This allows the developer to run the following:
log('foo', 'bar');
// Outputs '[MyPlugin] foo bar'
log('foo');
// Outputs '[MyPlugin] foo'
But I hope that this can be improved upon.
The issues with this implementation are:
- The logging function only allows two parameters. It would be better if it could accept many.
- There's lots of repetition (multiple
console.log
calls).
What I have tried.
I thought maybe the ES6 spread-operator would work:
var log = function(...params) {
console.log('[MyPlugin]', params);
};
Which allows the developer to run:
log('foo', 'bar');
// Outputs '[MyPlugin] Array [ "foo", "bar" ]'
log('foo');
// Outputs '[MyPlugin] Array [ "foo" ]'
You can see that the output is not the same as in the original function.
Is there a better solution?
Share Improve this question edited Jul 9, 2017 at 22:13 harvzor asked Jul 9, 2017 at 22:06 harvzorharvzor 2,9081 gold badge26 silver badges41 bronze badges1 Answer
Reset to default 13Spread notation (it's not an operator) would indeed do it, if you were using it. You're using rest notation (to gather arguments into an array parameter), but not spread notation (to spread them out when passing them to console.log
). Here's how you'd use spread:
var log = function(...params) {
// Rest -----------^^^
console.log('[MyPlugin]', ...params);
// Spread ----------------^^^
};
Example:
var log = function(...params) {
// Rest -----------^^^
console.log('[MyPlugin]', ...params);
// Spread ---------------^^^
};
log("testing", 1, 2, 3);
If you need to support pre-ES2015 environments without transpiling, you can do the same thing with arguments
and Function#apply
:
var log = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift('[MyPlugin]');
console.log.apply(console, args);
};
Live Example:
var log = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift('[MyPlugin]');
console.log.apply(console, args);
};
log("testing", 1, 2, 3);
本文标签: javascriptWrap a consolelog with infinite optional paramsStack Overflow
版权声明:本文标题:javascript - Wrap a console.log with infinite optional params - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744026546a2578143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论