覆盖console.log(或任何常规功能):是否有适当的方法来做到这一点? [英] Override console.log (or any function in general): Is there a proper way to do this?

查看:111
本文介绍了覆盖console.log(或任何常规功能):是否有适当的方法来做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己经常在浏览器和node.js中执行此操作,现在是时候我想出一种适当的方法来执行此操作,而不必每次都调试它了.

I find myself doing this quite often both on the browser and in node.js and it's about time I came up with a proper way to do this without debugging it for a while each time.

这是我目前正在处理的(这是node.js):

Here's what I'm currently working on (this is node.js):

var log4js = require("log4js");

log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('log.log'));
var logger = log4js.getLogger();
console.log = function () {
    logger.debug.apply(logger, Array.prototype.slice.call(arguments));
};

这基本上是一个猴子补丁,用于获取console.log ging代码并将其转换为正确的日志记录代码.

This is basically a monkey patch to take console.logging code and turn it into proper logging code.

我不确定这是正确的还是完全精明的.

从概念上讲,我正在将发送到console.log的参数强制设置为数组,然后将其传递给apply.所有这些都是为了解决我不知道如何在JS中声明可变参数的事实. (也许这是规范的方法).

Conceptually, I am forcing arguments sent to console.log into an array, which then is passed to apply. All of this is to work around the fact that I don't know how to declare a variadic function in JS. (perhaps this is the canonical way to do so).

也许使用console.log = logger.debug可以实现相同的目的.哦,我想知道如何传递参数.

Perhaps the same can be achieved with console.log = logger.debug. Oh well, I want to know how I can pass the arguments.

例如,由于某些原因,这种类型的某些东西在浏览器中不会起作用(但在节点中会起作用):

For instance, there are certain things of this sort that won't work for whatever reason in the browser (but does in node):

var l = console.log;
l('hello');

当然,这与覆盖console.log不同,只是为了缩短它的名称,但仍然如此.

Granted, this is not the same as overriding console.log, this is just to shorten the name of it, but still.

啊,我实际上意识到这里有什么问题:可以.

Ah, I actually realized what's wrong here: This will work.

l.call(console, 'hello');

这意味着它只是缺少了this的缺点.

Which means it's just missing a bit of finagling with this.

如果您仍在寻找该问题的实际问题,请查看上面的斜体行.

If you are still looking for the actual question of this question look at the italicized line above.

推荐答案

我不确定这是正确的还是完全精明的.

I'm not sure if this is something proper or completely asinine.

对于您自己的项目,应该没问题.不过,对于可能要在NPM上分发的任何项目,我可能会避免修改Node的文档化API(建议修改的部分除外).

For your own projects, it should be fine. Though, I would probably refrain from modifying Node's documented API (beyond the few parts that suggest modification) for any projects you intend to distribute on NPM.

当前,.apply()是将集合指定为函数调用参数的唯一方法.

Currently, .apply() is the only way to specify a collection as the arguments to a function call.

logger.debug.apply(logger, Array.prototype.slice.call(arguments));

尽管如此,ECMAScript 6将引入传播运算符作为替代.

Though, ECMAScript 6 will be introducing the spread operator as an alternative.

logger.debug(...arguments);

但是,slice可能不是必需的,因为它不会在传递集合之前对其进行修改.

However, the slice probably isn't necessary, since it isn't modifying the collection before passing it.

logger.debug.apply(logger, arguments);


对于l('hello'),您可以使用.bind()来确保上下文.


As for l('hello'), you can use .bind() to ensure the context.

var l = console.log.bind(console);
l('hello');

但是,这确实取决于您所使用的特定引擎的版本.对于Node 0.10.15和Chrome 28,console.log已绑定到console.因此,您列出的摘录不会引发错误.

Though, this does depend on what version of a particular engine you're using. With Node 0.10.15 and Chrome 28, console.log is already bound to console. So, the snippet you listed wouldn't throw an error.

这篇关于覆盖console.log(或任何常规功能):是否有适当的方法来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆