如何根据我的要求更改显示的浏览器日志?

时间:2017-12-21 06:57:07

标签: javascript html

以前,我使用了浏览器日志(console.log和debug.log),我以前用日志来获取日志:

Dec 21 05:55:56 GALIO|NORMAL|package://ATFF-58C2A778/js/ATFF.js at line 49 FIPAPP::Setting config: ATF_ENABLED_TESTS_COUNT=4
Dec 21 05:55:56 GALIO|NORMAL|package://ATFF-58C2A778/js/ATFF.js at line 49 FIPAPP::Setting config: ATF_REST_API_URL=http://64.100.106.75:1503/restlet/dhct/sercureServices
Dec 21 05:55:56 GALIO|NORMAL|package://ATFF-58C2A778/js/ATFF.js at line 49 FIPAPP::Setting config: Test Config =undefined
Dec 21 05:55:56 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...
Dec 21 05:55:59 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...
Dec 21 05:56:02 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...
Dec 21 05:56:05 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...
Dec 21 05:56:08 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...
Dec 21 05:56:11 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...
Dec 21 05:56:12 GALIO|NORMAL|package://library-8EEB287C/js/channellistprocessor.js at line 579 RTNUI : ReplicaChannel : DEBUG : IdleTimer : EXPIRED : 560
Dec 21 05:56:12 GALIO|NORMAL|package://library-8EEB287C/js/channellistprocessor.js at line 579 RTNUI : ReplicaChannel : DEBUG : stopIdleTimer : 560
Dec 21 05:56:14 GALIO|NORMAL|package://callid-9C03D013/js/callid.js at line 44 There was an unexpected error while connecting to the EventSource...

在上面的日志中,您可以看到“/js/callid.js”,第44行,依此类推。这意味着我已将浏览器登录到第44行的文件“callid.js”。

现在,我已经使用我的“console.js”库覆盖了浏览器日志,以便我收到错误,警告,信息日志。以下是我的图书馆文件:

(function () {
    /* dependencies */
    TVLib.Include.module('Utility');
    /* declare this module */
    TVLib.Core.declare('Console', false);

    var isMomDefined = typeof window.mom !== 'undefined';

    /**
     * Console constructor
     *
     * @param {String} prefix to add to any logging
     * @param {String=} prefix to namespace to add app name to logging (optional)
     * @return {TVLib.Console} console logging object
     */
    TVLib.Console = function (namespace, appName) {
        if (typeof namespace !== 'string') {
            throw new Error('TVLib.Console expects a string for the _namespace parameter');
        }
        this.appName = appName ? appName + ' ' : '';
        this.namespace = namespace;
        this.perfLogger = {};
        this.resetTimer = undefined;
    };
    TVLib.Console.prototype.namespace = '';

    function _log(namespace, level, args) {

        // Arguments don't behave like a normal array
        args = Array.prototype.slice.call(args);
        args.unshift('[' + this.appName + namespace + '] [' + level + '][' + Date.now() + ']');
        var force = false;
        // If last argument is boolean and true, force calling the debug.log function
        if (PVR.Utilities.isArray(args) && typeof args[args.length - 1] === 'boolean') {
            force = args.pop();
        }
        if (isMomDefined && window && window.console) {
            // Webkit on STB
            var str = args.join(' ');
            switch (level) {
            case 'DEBUG':
                window.console.debug.call(window.console, str);
                break;
            case 'INFO':
                window.console.info.call(window.console, str);
                break;
            case 'WARN':
                window.console.warn.call(window.console, str);
                break;
            case 'ERROR':
            case 'PERF':
                window.console.error.call(window.console, str);
                break;
            }
        } else {
            if (isMomDefined) {
                // MOM enabled browsers
                if (force) {
                    debug.log(args.join(' '));
                } else {
                    debug.log(args.join(' '));
                }
            } else {
                // Browsers with a regular console support
                switch (level) {
                case 'DEBUG':
                    window.console.debug.apply(window.console, args);
                    break;
                case 'INFO':
                    window.console.info.apply(window.console, args);
                    break;
                case 'WARN':
                    window.console.warn.apply(window.console, args);
                    break;
                case 'ERROR':
                case 'PERF':
                    window.console.error.apply(window.console, args);
                    break;
                }
            }
        }
    }

    TVLib.Console.prototype.log = function () {
        _log.apply(this, [this.namespace, 'DEBUG', arguments]);
    };
    TVLib.Console.prototype.debug = function () {
        _log.apply(this, [this.namespace, 'DEBUG', arguments]);
    };
    TVLib.Console.prototype.info = function () {
        _log.apply(this, [this.namespace, 'INFO', arguments]);
    };
    TVLib.Console.prototype.warn = function () {
        _log.apply(this, [this.namespace, 'WARN', arguments]);
    };
    TVLib.Console.prototype.error = function () {
        _log.apply(this, [this.namespace, 'ERROR', arguments]);
    };
})();

使用此库后,我得到如下所示的日志:

Dec 21 06:32:12 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [CallID] [DEBUG] There was an unexpected error while connecting to the EventSource...
Dec 21 06:32:14 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [PVR PVR.PosterManager] [DEBUG] delayed bulk fetch timeout complete, starting bulk fetch...
Dec 21 06:32:14 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [PVR PVR.PosterManager] [DEBUG] Preparing bulk fetch map...
Dec 21 06:32:14 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [PVR PVR.PosterManager] [DEBUG] No posters to fetch.  No bulk fetch request will be performed.
Dec 21 06:32:15 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [CallID] [DEBUG] There was an unexpected error while connecting to the EventSource...
Dec 21 06:32:18 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [CallID] [DEBUG] There was an unexpected error while connecting to the EventSource...
Dec 21 06:32:21 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [CallID] [DEBUG] There was an unexpected error while connecting to the EventSource...
Dec 21 06:32:24 GALIO|NORMAL|package://library-8EEB287C/js/console.js at line 52 [CallID] [DEBUG] There was an unexpected error while connecting to the EventSource...

在上面的日志中你可以看到,所有的日志(debug,warn,info)都是从我的console.js库中被激活而不是从callid.js文件中被激活而且所有的行号都显示相同的文件。

现在我想要的是,我想通过使用我的“console.js”库来显示第一个日志记录系统(具有正确的文件名和行号)。

我的console.js库文件中是否有任何更改?

0 个答案:

没有答案