หน้าเว็บ

วันเสาร์ที่ 8 ธันวาคม พ.ศ. 2555

log js (javascript logger)

/*
 * author redcrow
 * www.na5cent.blogspot.com
 */

var na5cent = {};

na5cent.log = (function(window) {

    var _enableDebug = true;
    var _enableInfo = true;
    var _enableWarn = true;
    var _enableError = true;

    function _getDate() {
        var date = new Date();

        var hours = (date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours());
        var minutes = (date.getMinutes() < 10 ? ("0" + date.getMinutes()) : date.getMinutes());
        var seconds = (date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date.getSeconds());
        var milliseconds = (date.getMilliseconds() < 10 ? ("00" + date.getMilliseconds()) : (date.getMilliseconds() < 100) ? ("0" + date.getMilliseconds()) : date.getMilliseconds());

        return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
    }

    var _log = function(name) {
        name = "  : [" + name + "] ";

        this.debug = function(message, escape) {
            if (_enableDebug) {
                if (escape === undefined || escape === true) {
                    window.console.log("DEBUG  " + name + _getDate() + ' - ' + message);
                } else {
                    window.console.log("DEBUG  " + name + _getDate());
                    window.console.log(message);
                }
            }
        };

        this.info = function(message, escape) {
            if (_enableInfo) {
                if (escape === undefined || escape === true) {
                    window.console.log("INFO   " + name + _getDate() + ' - ' + message);
                } else {
                    window.console.log("INFO   " + name + _getDate());
                    window.console.log(message);
                }
            }
        };

        this.warn = function(message, escape) {
            if (_enableWarn) {
                if (escape === undefined || escape === true) {
                    window.console.warn("WARN   " + name + _getDate() + ' - ' + message);
                } else {
                    window.console.warn("WARN   " + name + _getDate());
                    window.console.warn(message);
                }
            }
        };

        this.error = function(message, escape) {
            if (_enableError) {
                if (escape === undefined || escape === true) {
                    window.console.error("ERROR" + name + _getDate() + ' - ' + message);
                } else {
                    window.console.error("ERROR" + name + _getDate());
                    window.console.error(message);
                }
            }
        };
    };

    return {
        getLogger: function(name) {
            return new _log(name);
        }
    };
})(window);


/*
* example to use
*/

//var log = na5cent.log.getLogger("Menu.js");
//log.debug("test logger debug");
//log.info("test logger info", false);
//log.warn("test logger warn");
//log.error("test logger error");

ไม่มีความคิดเห็น:

แสดงความคิดเห็น