javascript 抽象控制台日志记录

发布于 2024-11-25 19:14:14 字数 388 浏览 3 评论 0原文

我想做一个函数,像这样。

例如:

function Logger() {
    this.log = function(msg) {
        console.log(msg);
    }
}

我想在函数/模块等中使用它,并且一切正常。 但我的浏览器中的默认控制台通常给出文件名+行号。

现在,当我抽象此功能时,fileNamelineNumber 不是我放置 instance.log() 的位置。因为它会说明从哪里调用 console.log,而不是函数本身。

所以我的问题是:

如何从我想要使用记录器的地方获取正确的信息? 或者请给我任何改进此功能的提示。

I want to make a function, like this.

For example:

function Logger() {
    this.log = function(msg) {
        console.log(msg);
    }
}

And I want to use it in functions/modules etc, and that all works fine.
But the default console in my browser normally give the fileName + lineNumber.

Now when I abstract this functionality, the fileName and lineNumber is not where I put my instance.log(). Because it will say from where the console.log is being called, not the function itself.

So my question:

How can I get the correct information from where I want to use my logger?
Or give me, please, any tips to improve this functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Saygoodbye 2024-12-02 19:14:14
function Logger() {
    this.log = console.log.bind(console);
}

我前段时间问过这个问题: 在 Chrome 中创建 console.log() 的快捷方式

function Logger() {
    this.log = console.log.bind(console);
}

I asked about this some time ago: Create shortcut to console.log() in Chrome.

情仇皆在手 2024-12-02 19:14:14

尝试使用像这样的 backtrace 函数:

function printStackTrace() {
    var callstack = [];
    var isCallstackPopulated = false;
    try {
        i.dont.exist += 0; //doesn't exist- that's the point
    } catch (e) {
        if (e.stack) { //Firefox
            var lines = e.stack.split('\n');
            for (var i = 0, len = lines.length; i & lt; len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    callstack.push(lines[i]);
                }
            }
            //Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
        else if (window.opera & amp; & amp; e.message) { //Opera
            var lines = e.message.split('\n');
            for (var i = 0, len = lines.length; i & lt; len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    var entry = lines[i];
                    //Append next line also since it has the file info
                    if (lines[i + 1]) {
                        entry += ' at ' + lines[i + 1];
                        i++;
                    }
                    callstack.push(entry);
                }
            }
            //Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
    }
    if (!isCallstackPopulated) { //IE and Safari
        var currentFunction = arguments.callee.caller;
        while (currentFunction) {
            var fn = currentFunction.toString();
            var fname = fn.substring(fn.indexOf( & amp; quot;

            function & amp; quot;) + 8, fn.indexOf('')) || 'anonymous';
            callstack.push(fname);
            currentFunction = currentFunction.caller;
        }
    }
    output(callstack);
}

function output(arr) {
    //Optput however you want
    alert(arr.join('\n\n'));
}

Try using backtrace function like this one :

function printStackTrace() {
    var callstack = [];
    var isCallstackPopulated = false;
    try {
        i.dont.exist += 0; //doesn't exist- that's the point
    } catch (e) {
        if (e.stack) { //Firefox
            var lines = e.stack.split('\n');
            for (var i = 0, len = lines.length; i & lt; len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    callstack.push(lines[i]);
                }
            }
            //Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
        else if (window.opera & amp; & amp; e.message) { //Opera
            var lines = e.message.split('\n');
            for (var i = 0, len = lines.length; i & lt; len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    var entry = lines[i];
                    //Append next line also since it has the file info
                    if (lines[i + 1]) {
                        entry += ' at ' + lines[i + 1];
                        i++;
                    }
                    callstack.push(entry);
                }
            }
            //Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
    }
    if (!isCallstackPopulated) { //IE and Safari
        var currentFunction = arguments.callee.caller;
        while (currentFunction) {
            var fn = currentFunction.toString();
            var fname = fn.substring(fn.indexOf( & amp; quot;

            function & amp; quot;) + 8, fn.indexOf('')) || 'anonymous';
            callstack.push(fname);
            currentFunction = currentFunction.caller;
        }
    }
    output(callstack);
}

function output(arr) {
    //Optput however you want
    alert(arr.join('\n\n'));
}
谜泪 2024-12-02 19:14:14

尝试分配该函数:

(function () {
    window.log = (console && console.log 
        ? console.log 
        : function () { 
              // Alternative log
          });
})();

稍后只需在代码中调用 log('Message') 即可。

Try assigning the function:

(function () {
    window.log = (console && console.log 
        ? console.log 
        : function () { 
              // Alternative log
          });
})();

Later just call log('Message') in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文