nodejs 封装自己的log模块
来源:互联网 发布:windows7安装sqlserver 编辑:程序博客网 时间:2024/06/01 08:31
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");
0 0
- nodejs 封装自己的log模块
- 封装自己的LOG类
- nodejs 自己的第一个模块
- 在Meteor中调用NodeJS模块或者自己的模块
- 自己封装的mongodb操作的模块
- 尝试封装一个自己的模块
- 自己封装的Mysql数据库操作模块
- 我自己用的Android-Log封装,功能算好用吧...
- 自己写的c语言可变长封装log函数
- 【086】部署nodejs程序时,如何把自己用到的npm模块封装成Docker镜像,方便在离线的时候使用?
- nodejs colors模块 设置控制台log颜色
- 运算符的重载-封装自己的字符串模块
- 自己对volley http模块的二次封装的使用
- 简单的封装log
- Android Log的封装
- NodeJS的模块原理
- nodejs的模块机制
- nodejs的模块引用
- BZOJ 1036 [ZJOI2008]树的统计Count==树剖裸题
- SpringMVC简单配置
- javaWeb后台版登陆注册
- MATLAB模拟布丰投针实验
- [Warning] IP address'10.0.9.121' could not be resolved: Temporary failure in name resolution
- nodejs 封装自己的log模块
- Jetty for Mac的安装及Myeclipse下的配置(Myeclipse for mac 插件安装link方法详述)
- 解决ntp的错误 no server suitable for synchronization found
- 通过金矿模型介绍动态规划
- C语言中%d,%o,%f,%e,%x的意义
- js实现冒泡排序
- 自动识别图片验证码
- Android奇怪问题-----Button的setText()方法都是大写
- Xamarin Android设置界面提示类型错误