log-rotator-util.js
6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
var fs = require('fs');
var util = require('util');
var zlib = require('zlib');
var events = require('events');
/**
* Performs scheduled and on demand log rotation on files
*/
function Logrotator() {
events.EventEmitter.call(this);
this.timers = {};
}
util.inherits(Logrotator, events.EventEmitter);
/**
* Schedules a file for rotation. emits a 'rotate' event whenever the file has been rotated.
* @param file full file path to rotate
* @param options rotation options
* - schedule - how often to check for file rotation conditions. possible values are '1s', '1m', '1h'. default is 5m.
* - size - size of the file to trigger rotation. possible values are '1k', '1m', '1g'. default is 10m.
* - count - number of files to keep. default is 3.
* - compress - whether to gzip rotated files. default is true.
* - format - a function to build the name of a rotated file. the function receives the index of the rotated file.
* default format is the index itself.
*/
Logrotator.prototype.register = function(file, options) {
options = util._extend({schedule: '5m'}, options);
var match = options.schedule.match(/^([0-9]+)(s|m|h)$/);
if (!match) {
this.emit('error', 'incorrect schedule format ' + options.schedule);
return;
}
if (this.timers[file]) {
this.unregister(file);
}
// calculate the schedule
var multi = this._timeMultiplier(match[2]);
var schedule = parseInt(match[1]) * multi;
var scheduleMinute = parseInt(match[1]) ;
var _this = this;
// perform rotation
function _doRotate() {
_this.rotate(file, options, function(err, rotated) {
if (err) {
_this.emit('error', err);
return;
}
if (rotated) {
_this.emit('rotate', file);
}
});
}
// register the rotation timer
this.timers[file] = setInterval(function() {
var d = new Date();
var n = d.getMinutes();
if( (n % scheduleMinute) == 0 ){
_doRotate();
}
}, 60 * 1000 );
// immediately rotate
// _doRotate(); //ignore
};
/**
* Remove the scheduled rotation of a file
* @param file the file to stop rotating
*/
Logrotator.prototype.unregister = function(file) {
if (!this.timers[file]) {
return;
}
clearInterval(this.timers[file]);
delete this.timers[file];
};
/**
* Stop all schedulers
*/
Logrotator.prototype.stop = function() {
var _this = this;
Object.keys(this.timers).forEach(function(name) {
clearInterval(_this.timers[name]);
});
this.timers = {};
};
Logrotator.prototype._timeMultiplier = function(multi) {
switch (multi) {
case 's':
return 1000;
case 'm':
return 60*1000;
case 'h':
return 60*60*1000;
}
};
Logrotator.prototype._sizeMultiplier = function(multi) {
switch (multi) {
case 'k':
return 1024;
case 'm':
return 1024*1024;
case 'g':
return 1024*1024*1024;
}
};
/**
* Rotate a file now if size conditions are met.
* @param file full file path to rotate
* @param options rotation options
* - size - size of the file to trigger rotation. possible values are '1k', '1m', '1g'. default is 10m.
* - count - number of files to keep. default is 3.
* - compress - gzip rotated files. default is true.
* - format - a function to build the name of a rotated file. the function receives the index of the rotated file.
* default format is the index itself.
* @param cb - invoked on completion, receives 'err' on error
*/
Logrotator.prototype.rotate = function(file, options, cb) {
if (!cb) {
cb = options;
options = null;
}
options = util._extend({size: '10m', count: 3, compress: true}, options);
var match = options.size.match(/^([0-9]+)(k|m|g)$/);
if (!match) {
cb('incorrect size format ' + options.size);
return;
}
var multi = this._sizeMultiplier(match[2]);
var size = parseInt(match[1]) * multi;
// check if the file reached the trigger size
var _this = this;
fs.stat(file, function(err, stats) {
if (err) {
var message = null;
// if file does not exist, ignore
if (err.code !== 'ENOENT') {
// other errors
message = file + ' stat failed: ' + err.message;
}
cb(message);
return;
}
// this isn't a file
if (!stats.isFile()) {
cb(file + ' is not a file');
return;
}
// check file size to see if rotation is needed
if (stats.size >= size) {
_this._rotate(file, options.count, options, cb);
} else {
cb(null, false);
}
});
};
/**
* Get the correct file name based on params
* @param file
* @param index
* @param options
* @private
*/
Logrotator.prototype._filename = function(file, index, options) {
var format = index;
if (typeof options.format === 'function') {
format = options.format(index);
}
var fileName = file + '.' + format;
if (options.compress) {
fileName += '.gz';
}
return fileName;
};
/**
* The log rotation brains
* @param file
* @param index
* @param options
* @param cb
* @private
*/
Logrotator.prototype._rotate = function(file, index, options, cb) {
// rotate all existing files
// 1. delete last file
// 2. rename all files to with +1
// 3. read + compress current log into 1
// 4. truncate file to size 0
var _this = this;
var fileName = this._filename(file, index, options);
// delete last file
if (index === options.count) {
fs.unlink(fileName, function(err) {
if (err && err.code !== 'ENOENT') {
cb('error deleting file ' + fileName + ': ' + err.message);
return;
}
_this._rotate(file, --index, options, cb);
});
return;
}
// rename all files to with +1
if (index > 0) {
var renameTo = this._filename(file, index+1, options);
fs.rename(fileName, renameTo, function(err) {
if (err && err.code !== 'ENOENT') {
cb('error renaming file ' + fileName + ': ' + err.message);
return;
}
_this._rotate(file, --index, options, cb);
});
return;
}
// read (and compress) the file log into index 1
var fis = fs.createReadStream(file);
var fos = fs.createWriteStream(this._filename(file, 1, options));
var pipe;
if (options.compress) {
pipe = fis.pipe(zlib.createGzip()).pipe(fos);
} else {
pipe = fis.pipe(fos);
}
var error;
pipe.on('finish', function() {
if (error) {
return;
}
// truncate log file to size 0
fs.truncate(file, 0, function(err) {
if (err) {
cb && cb('error truncating file ' + file + ': ' + err.message);
return;
}
cb && cb(null, true);
})
});
pipe.on('error', function(err) {
error = true;
cb('error compressing file ' + file + ': ' + err.message);
cb = null;
});
};
// create a new log rotator
module.exports.create = function() {
return new Logrotator();
};
// global log rotator
module.exports.rotator = new Logrotator();