现在正在开发一个firefox扩展,想从扩展里面的javascript代码中将比较多的信息输出到console(或者类似的地方)。因为信息比较多,不想用alert。已经尝试了dump,可行,不知道大家一般用的是什么方法?注意:我是想从扩展的js源码中输出信息,不是从页面的js源码中输出信息

解决方案 »

  1.   

    然后怎么查看啊?不会是firebug吧?好像不行
      

  2.   

    IE浏览器按F12,点击脚本Script选项,里面有console输出栏
      

  3.   

    Fan类库:Fan.util.Logger类/**
     * @staticClass Logger
     * 
     * 负责向控制台打印运行日志
     * 
     * @author FuFan
     * 
     * @param level
     *            日志级别,0-4对应:debug/info/warn/error/fatal,默认0
     * 
     * 示例: 设置日志输出级别: 
     * Logger('debug'/0) <==> Logger.level=0;
     * Logger('info'/1) <==> Logger.level=1;
     * Logger('warn'/2) <==> Logger.level=2;
     * Logger('error'/3) <==> Logger.level=3;
     * Logger('off'/-1) <==> Logger.level=-1;
     * Logger('on'/0) <==> Logger.level=0;
     * 
     * Fan.util.Logger.debug('debug msg...');
     * Fan.util.Logger.info('info msg...');
     * 
     */
    Fan.package('Fan.util');Fan.clazz('Fan.util.Logger', function(level) {
        // this.$super(); // 静态类不可以要调用super
    var _levelEnum = {
    debug :0,
    info :1,
    warn :2,
    error :3,
    fatal :4,
    off :-1,
    on :0,
    '0' :0,
    '1' :1,
    '2' :2,
    '3' :3,
    '4' :4,
    '-1' :-1
    };

    /**
     * @staticProperty level
     *   日志级别,0-4对应:debug/info/warn/error/fatal,默认0
     */ 
    Fan.util.Logger.level = _levelEnum[level + ''] || 0;

    /**
     * @staticProperty maxResult
     *   控制台最大日志信息行数,默认1024,超过则自动清空控制台
     */
    Fan.util.Logger.maxResult = 1024;

    /**
     * @staticProperty currResult
     *   当前已经输出的记录数
     */
    Fan.util.Logger.currResult = 0;

    /**
     * @staticProperty format
     *    指定日志输出的格式,默认:'yyyy-MM-dd hh:mm:ss:nnn::{msg}',{msg}表示内容输入部分
     */
    Fan.util.Logger.format = 'yyyy-MM-dd hh:mm:ss:nnn::{msg}';

    // 时间输出格式
    var _formatMsg = function(msg) {
    var format = Fan.util.Logger.format;
    if (format) {
    // 格式化年月日时分秒
    var dt = new Date();
    var M = '0' + (dt.getMonth() + 1), d = '0'
    + dt.getDate(), h = '0' + dt.getHours(), m = '0'
    + dt.getMinutes(), s = '0'
    + dt.getSeconds(), n = '00'
    + dt.getMilliseconds();
    var flg = dt.getTime() + '\n' + dt.getTime();
    return format.replace('{msg}', '{' + flg + '}')
     .replace(/yyyy/gi, dt.getFullYear())
     .replace(/M{1,2}/g, M.substring(M.length - 2))
     .replace(/d{1,2}/gi, d.substring(d.length - 2))
     .replace(/h{1,2}/gi, h.substring(h.length - 2))
     .replace(/m{1,2}/g, m.substring(m.length - 2))
     .replace(/s{1,2}/gi, s.substring(s.length - 2))
     .replace(/n{3}/g, n.substring(n.length - 3))
     .replace('{' + flg + '}', msg);
    } else {
    return new Date();
    }
    };

    // 检测控制台信息数量是否已经超过最大值
    var _checkResultCount = function() {
    if (Fan.util.Logger.maxResult > 0
    && Fan.util.Logger.currResult >= Fan.util.Logger.maxResult - 1) {
    Fan.util.Logger.clear();
    Fan.util.Logger.currResult = 0;
    }
    return true;
    };

    if (Fan.util.Logger.debug && Fan.util.Logger.info) {
    return Fan.util.Logger;
    }

    Fan.util.Logger.debug = function(msg) {
    try {
    Fan.util.Logger.level === 0 && _checkResultCount() && window.console
    && console.info
    && console.info(_formatMsg(msg));
    Fan.util.Logger.currResult++;

    Fan.util.Logger.debug = function(msg){
    window.console && Fan.util.Logger.level === 0 && _checkResultCount() && console.info(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    } catch (_) {}
    };

    Fan.util.Logger.info = function(msg) {
    try {
    if (Fan.util.Logger.level <= 1
    && Fan.util.Logger.level >= 0 && _checkResultCount()) {
    if (Fan.ie) {
    window.console && console.log
    && console.log(_formatMsg(msg));
    Fan.util.Logger.info = function(msg){
    window.console && Fan.util.Logger.level <= 1 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.log(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    } else {
    if (window.console && console.debug) {
    console.debug(_formatMsg(msg));
    Fan.util.Logger.info = function(msg){
    Fan.util.Logger.level <= 1 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.debug(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    } else if (window.console && console.log) {
    console.log(_formatMsg(msg));
    Fan.util.Logger.info = function(msg){
    Fan.util.Logger.level <= 1 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.log(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    }
    }
    Fan.util.Logger.currResult++;
    }
    } catch (_) {
    }
    };

    Fan.util.Logger.warn = function(msg) {
    try {
    if (Fan.util.Logger.level <= 2
    && Fan.util.Logger.level >= 0 && _checkResultCount()) {
    window.console && console.warn
    && console.warn(_formatMsg(msg));
    Fan.util.Logger.currResult++;

    Fan.util.Logger.warn = function(msg){
    window.console && Fan.util.Logger.level <= 2 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.warn(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    }
    } catch (_) {
    }
    };

    Fan.util.Logger.error = function(msg) {
    try {
    if (Fan.util.Logger.level <= 3
    && Fan.util.Logger.level >= 0 && _checkResultCount()) {
    window.console && console.error
    && console.error(_formatMsg(msg));
    Fan.util.Logger.currResult++;

    Fan.util.Logger.error = function(msg){
    window.console && Fan.util.Logger.level <= 3 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.error(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    }
    } catch (_) {
    }
    };

    Fan.util.Logger.fatal = function(msg) {
    try {
    if (Fan.util.Logger.level <= 4
    && Fan.util.Logger.level >= 0 && _checkResultCount()) {
    window.console && console.fatal
    && console.fatal(_formatMsg(msg));
    Fan.util.Logger.currResult++;

    Fan.util.Logger.fatal = function(msg){
    window.console && Fan.util.Logger.level <= 4 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.fatal(_formatMsg(msg));
    Fan.util.Logger.currResult++;
    };
    }
    } catch (_) {
    }
    };

    // Fan.util.Logger.assert = function(msg) {
    // try {
    // if (Fan.util.Logger.level <= 4
    // && Fan.util.Logger.level >= 0 && _checkResultCount()) {
    // window.console && console.assert
    // && console.assert(formatMsg(msg));
    // Fan.util.Logger.currResult++;
    //
    // Fan.util.Logger.assert = function(msg){
    // Fan.util.Logger.level <= 4 && Fan.util.Logger.level >= 0 && _checkResultCount() && console.assert(_formatMsg(msg));
    // Fan.util.Logger.currResult++;
    // };
    // }
    // } catch (_) {
    // }
    // };

    // 清空控制台信息
    Fan.util.Logger.clear = function() {
    window.console && console.clear && console.clear();
    Fan.util.Logger.currResult = 0;
    };

    return Fan.util.Logger;
    });// 初始化,日志界别默认为debug
    Fan.util.Logger(0);使用方式:
    Logger.info(xxx);
    Logger.debug(xxx);
    Logger.warn(xxx);
    Logger.error(xxx);
    Logger.fatal(xxx);
    Logger.clear();