求node日志记录模块
发布于 13 年前 作者 linmomo02 6659 次浏览 最后一次编辑是 8 年前

node中有没有想java中的log4j那样的模块啊。可以按天进行日志归档

3 回复
// origin author :fengyin <fengyin.zym@taobao.com>
// modified by : sumory <sumory.wu@gmail.com>

var fs = require('fs');
var config = require('../config.js').config;

var FILE_SPLIT_INTERVAL = 'DAY';// 1d
var BUFFER_CHECK_INTERVAL = 2000;// 2s
var BUFFER_FLUSH_LEN = 512;
var LOG_DIRECTORY = config.logDirectory;

var fileMap = {
	'info' : {
		pathPrefix : 'info.log.'
	},
	'error' : {
		pathPrefix : 'error.log.'
	},
	'exception' : {
		pathPrefix : 'exception.log.'
	}
};

// initial setting
var curDay = (new Date()).getDate();

function genFilePostfix() {
	var dnow = new Date();
	return dnow.format('yyyy-mm-dd');
}

function genTimeStamp() {
	var dnow = new Date();
	return dnow.format('HH:MM:ss');
}

function debug(str) {
	console.log(str);
}

function inspect(obj) {
	console.log(require('util').inspect(obj, false, 10));
}

function LogFile(options) {
	this.pathPrefix = options.pathPrefix;

	this.buffers = [];
	this.bufferCheckInterval = options.bufferCheckInterval
			|| BUFFER_CHECK_INTERVAL;

	this.init();
}

LogFile.prototype.push = function(str) {
	this.buffers.push(str);
	if (this.buffers.length >= BUFFER_FLUSH_LEN) {
		this._flush();
	}
};

LogFile.prototype._flush = function() {
	if (this.buffers.length > 0 && this.stream) {
		this.buffers.push('');
		var str = this.buffers.join('\n');
		this.stream.write(str);
		// debug('buffers length: ' + this.buffers.length);
		this.buffers = [];
	}
};

LogFile.prototype.destroy = function() {
	this._flush();

	if (this.bufferCheckTimer) {
		clearInterval(this.bufferCheckTimer);
		this.bufferCheckTimer = null;
	}
	if (this.stream) {
		this.stream.end();
		this.stream.destroySoon();
		this.stream = null;
	}
};

LogFile.prototype.init = function() {
	// debug('log init ' + this.pathPrefix);
	var self = this;
	var path = LOG_DIRECTORY + this.pathPrefix + genFilePostfix();
	// debug(path);
	// inspect(conf);
	this.stream = fs.createWriteStream(path, {
		flags : 'a'
	});

	this.bufferCheckTimer = setInterval(function() {
		self._flush();
	}, this.bufferCheckInterval);
};

LogFile.prototype.restart = function() {
	this.destroy();
	this.init();
};

var logMap = {};

// exports.init = function(){
for ( var id in fileMap) {
	logMap[id] = new LogFile(fileMap[id]);
}
// }

function push2File(str, id) {
	var logFile = logMap[id];

	var dnow = new Date();
	if (dnow.getDate() != curDay) {
		// if(dnow.getMinutes() != curMinute){
		logFile.restart();
		curDay = dnow.getDate();
		// curMinute = dnow.getMinutes();
	}
	logFile.push(dnow.format('HH:MM:ss') + '\t' + str);
}

exports.info = function(str) {
	push2File(str, 'info');
};

exports.error = function(str) {
	push2File(str, 'error');
};

exports.exception = function(str) {
	push2File(str, 'exception');
};

文件是按天记录的,按自己的业务稍微改下就可以用了,

var log = require('log.js');
log.error('...');
log.exception('...');
log.info('...');

我考虑把这个加到我的tracer里面去,没问题吧?

@baryon 这个基本上是我从itier上挖的,然后根据需要改造了下(具体与业务联系的已经被我删了才粘上来的),已经标明原作者了。代码本来就是开放的,小东西,随便用。

回到顶部