nodejs 封装自己的log模块

var fs = require("fs");

function fix2num(n) {
    return [0, n].join('').slice(-2);
}
function getTime(format) {
    var curdate = new Date();
    if (format == undefined) return curdate;
    format = format.replace(/Y/i, curdate.getFullYear());
    format = format.replace(/m/i, fix2num(curdate.getMonth() + 1));
    format = format.replace(/d/i, fix2num(curdate.getDate()));
    format = format.replace(/H/i, fix2num(curdate.getHours()));
    format = format.replace(/i/i, fix2num(curdate.getMinutes()));
    format = format.replace(/s/i, fix2num(curdate.getSeconds()));
    format = format.replace(/ms/i, curdate.getMilliseconds());
    return format;
}


var options = {
    logLevel: {
        debug: true,
        error: true,
        warn: true,
        info: true
    },
    consoleLevel: {
        debug: true,
        error: true,
        warn: true,
        info: true
    },
    buffer: {
        interval: 1000,
        tag: true
    },
    path: './',
    timeFormat: 'Ymd',
    file: 'log',
    format: 'time: [file] content'
};

var consoleColorTag = {
    "error_prefix": "\u001B[31m",
    "error_suffix": "\u001B[39m",
    "warn_prefix": "\u001B[32m",
    "warn_suffix": "\u001B[39m",
    "info_prefix": "\u001B[1m",
    "info_suffix": "\u001B[22m",
    "debug_prefix": "\u001B[35m",
    "debug_suffix": "\u001B[39m"
};

function Log(options) {
    this.options = options;
    this.buf = [];

    if (this.options.buffer.tag) {
        var self = this;
        setInterval(function () {
            if (self.buf.length) {
                for (var i = 0, len = self.buf.length; i < len; ++i) {
                    self.write(self.buf[i][0], self.buf[i][1]);
                }
                self.buf.length = 0;
            }
        }, self.options.buffer.interval);
    }
}

Log.prototype.debug = function (str) {
    this._log("debug", str);
};
Log.prototype.error = function (str) {
    this._log("error", str);
};
Log.prototype.warn = function (str) {
    this._log("warn", str);
};
Log.prototype.info = function (str) {
    this._log("info", str);
};

Log.prototype.write = function (level, str) {
    if (str) {
        fs.appendFile(this.getFilename(level), str, {flat: 'a'});
    }
};

Log.prototype.console = function (l, str) {
    if (this.options.consoleLevel[l]) {
        var l = l.toLowerCase();
        var lPrefix = consoleColorTag[l + "_prefix"];
        var lSuffix = consoleColorTag[l + "_suffix"];
        var cStr = [lPrefix, getTime("Y-m-d h:i:s"), "[", l, "]", "[", this.options.file, "]", str, lSuffix, "\n"].join("");
        console.log(cStr);
    }
};

Log.prototype._log = function (level, str) {
    var fStr = [getTime("Y-m-d h:i:s"), "[", this.options.file, "]", str, "\n"].join("");
    if (!this.options.buffer.tag) {
        this.write(level, fStr);
    } else {
        this.buf.push([level, fStr]);
    }
    this.console(level, str);
};

Log.prototype.getFilename = function (level) {
    var format = getTime(this.options.timeFormat);
    return [this.options.path, level, this.options.file, format, '.log'].join('');
};


var logs = new Log(options);
logs.debug("haha");
logs.warn("haha");
logs.info("haha");
logs.error("haha");

你可能感兴趣的:(node.js技术)