-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlogger.js
58 lines (45 loc) · 1.81 KB
/
logger.js
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
var fs = require('fs');
var path = require('path');
var util = require('util');
var datetime = require('datetime');
var express = require('express');
var mkdirsSync = require('mkdir').mkdirsSync;
// *************************************************************************************************
var defaultInterval = 60*60*24*1000;
var defaultDateFormat = '%Y-%m-%d';
// *************************************************************************************************
module.exports = function(options) {
mkdirsSync(path.dirname(options.path));
var logStream = fs.createWriteStream(options.path, {flags: 'a'});
var logger = express.logger({stream: logStream, format: options.format});
// XXXjoe Need to expose a way to cancel this timer
var archiveInterval = setInterval(function() {
archiveLogs(logStream, options.path, options.dateFormat);
}, options.interval || defaultInterval);
return logger;
}
function archiveLogs(logStream, logPath, dateFormat) {
var logDate = datetime.format(new Date(), dateFormat || defaultDateFormat);
var ext = path.extname(logPath);
var name = path.basename(logPath, ext);
var dir = path.dirname(logPath);
var newPath = path.join(dir, name + '-' + logDate + ext);
copyFile(logPath, newPath, function(err) {
if (!err) {
fs.truncateSync(logStream.fd, 0);
} else {
console.error("Unable to archive logs: " + err);
}
});
}
function copyFile(src, dst, cb) {
fs.stat(dst, function(err) {
if (!err) { cb(new Error(dst + " already exists.")); return; }
fs.stat(src, function (err) {
if (err) { cb(err); return }
var is = fs.createReadStream(src);
var os = fs.createWriteStream(dst);
util.pump(is, os, cb);
});
});
}