Skip to content

Instantly share code, notes, and snippets.

@StuPig
Created March 28, 2012 09:09
Show Gist options
  • Save StuPig/2224966 to your computer and use it in GitHub Desktop.
Save StuPig/2224966 to your computer and use it in GitHub Desktop.
winston logger : questions about maxsize and multi exceptions handler
var winston = require('winston');
var fs = require('fs');
var dateUtil = require('date-utils');
var LOGPATH = './log/';
var DOMAIN = 'www.labi.com';
// 测试用对日志文件的切割,所以`maxsize`设的很小
var MAXSIZE = 100;
var latest_ex_filename = null;
var latest_if_filename = null;
var exception_filename = getLogFilename(true);
var info_filename = getLogFilename(false);
// 自定义 `error` 的 `transport`
var exceptionFileTransport = new (winston.transports.File)({
timestamp: true
, silent: false
, level: 'error'
, filename: exception_filename
, handleExceptions: true
, exitOnError: false
// 经过测试 这里的 `maxsize`在输出很多日志时(见下面的 setTimeout) 并不会切割日志
, maxsize: MAXSIZE
});
// 自定义 `debug` 的 `transport`
var infoFileTransport = new (winston.transports.File)({
timestamp: function() {
return (new Date).toFormat('YYYY-MM-DD HH:MI:SS');
}
, silent: false
, level: 'debug'
, filename: info_filename
, exitOnError: false
, maxsize: MAXSIZE
});
// 实例化 `winston`
var logger = new (winston.Logger)({
transports: [
infoFileTransport
]
});
// ??? 这样不能去处理`error`,为什么,应该怎么给winston的同一个实例添加两个`file transport`?
logger.handleExceptions(exceptionFileTransport);
winston.exitOnError = true;
logger.cli();
// 因为winston无法满足我的自定义日志文件名的需求,
// 我就自己实现了一个根据已经存在的日志文件的大小来生成新的日志的文件名的方法
/*
* 这个方法会获取/log/目录下的所有.log文件,找到最新被修改的文件,
* 并根据该文件是否超过maxsize来返回对应的文件名
* 但是,实测这个方法跟winston的maxsize一样,存在 如果一次输入太多日志,并不会切分该日志 的问题
*/
function getLogFilename(isException) {
var now = new Date();
var milliSeconds = ('00' + now.getUTCMilliseconds()).slice(-3);
var logFilename = now.toFormat('YYYY-MM-DD_HH:MI:SS') + ':' + milliSeconds + '.log';
var ex_filename = 'exception.' + DOMAIN + '.' + logFilename;
var if_filename = DOMAIN + '.' + logFilename;
var filename = isException ? ex_filename : if_filename;
var latest_filename = isException ? latest_ex_filename : latest_if_filename;
var dirArr = fs.readdirSync(LOGPATH);
var isHaveLogs = false;
if (!latest_filename) {
if (dirArr.length > 0 && !(dirArr.length === 1 && dirArr[0].indexOf('.svn') > -1)) {
latest_filename = filename;
var i = 0;
var tmp_filename = null;
var tmp_fileMtime = null;
var latest_fileMtime = null;
while(dirArr[i] && (dirArr[i].indexOf('.svn') < 0)) {
if (isException) {
if (dirArr[i].indexOf('exception') < 0) {
i++;
continue;
}
} else {
if (dirArr[i].indexOf('exception') > -1) {
i++;
continue;
}
};
tmp_filename = dirArr[i];
tmp_fileMtime = fs.statSync(LOGPATH + tmp_filename).mtime;
winston.debug('tmp_fileMtime', tmp_fileMtime, 'latest_fileMtime', latest_fileMtime);
if (!latest_fileMtime || (tmp_fileMtime > latest_fileMtime)) {
isHaveLogs = true
latest_filename = tmp_filename;
latest_fileMtime = tmp_fileMtime;
}
i++;
}
}
winston.debug(LOGPATH + latest_filename);
}
winston.debug('isHaveLogs', isHaveLogs);
if (dirArr.length > 0 && !(dirArr.length === 1 && dirArr[0].indexOf('.svn') > -1 ) && isHaveLogs) {
var latest_file_size = fs.statSync(LOGPATH + latest_filename).size;
winston.debug('latest_file_size', latest_file_size);
if (latest_file_size > MAXSIZE) {
latest_filename = filename;
} else {
filename = latest_filename;
}
}
isException ? latest_ex_filename = latest_filename : latest_if_filename = latest_filename;
winston.debug('output filename', filename);
return LOGPATH + filename;
}
// 测试logger
var error = new Error('this is an test error message');
for (var i = 0; i < 30; i++) {
setTimeout(function() {
logger.debug(logger, null, null, function() {
// winston.debug(arguments);
});
logger.error(error);
}, 1000);
}
module.exports = logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment