Skip to content

Commit

Permalink
move log rotation to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
camilleanne committed Jan 17, 2015
1 parent 86e4d77 commit 4b334c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
41 changes: 41 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var fs = require('fs');
var through = require('through');
var rotate = require('log-rotate');

// Set up app logging to a file with rotation.
module.exports = function(filepath, maxsize, callback) {
if (!filepath) return callback();

fs.stat(filepath, function(err, stat) {
if (err && err.code !== 'ENOENT') return callback(err);
if (stat && !stat.isFile()) return callback(new Error(filepath + ' is not a file'));
if (!err && stat && stat.size > maxsize) {
rotate(filepath, { compress: true }, function(err) {
if (err) return callback(err);
setup(0);
});
} else {
setup(stat && stat.size || 0);
}
});

function setup(offset) {
var logstream = fs.createWriteStream(filepath, {
flags: offset ? 'r+' : 'w',
start: offset
});
logstream.on('error', function (err) {
console.warn('Problem writing to logfile at ' + filepath);
return callback();
})
var pipeout = through();
var pipeerr = through();
pipeout.pipe(logstream);
pipeout.pipe(process.stdout);
pipeerr.pipe(logstream);
pipeerr.pipe(process.stderr);
process.__defineGetter__('stdout', function() { return pipeout; });
process.__defineGetter__('stderr', function() { return pipeerr; });
return callback();
}
};
44 changes: 3 additions & 41 deletions lib/tm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var crypto = require('crypto');
var fstream = require('fstream');
var tilelive = require('tilelive');
var existsSync = require('fs').existsSync || require('path').existsSync;
var rotate = require('log-rotate');
var through = require('through');

var applog = require('./log');

tilelive.protocols['mbtiles:'] = require('mbtiles');

Expand Down Expand Up @@ -45,7 +45,7 @@ tm.config = function(opts, callback) {
mapnik.register_default_input_plugins();

// Set up logging with rotation after 10e6 bytes.
tm.applog(tm.config().log, 10e6, function(err) {
applog(tm.config().log, 10e6, function(err) {
if (err && callback) return callback(err);
if (err) throw err;
// Compact db.
Expand Down Expand Up @@ -131,44 +131,6 @@ tm.dbcompact = function(filepath, callback) {
}
};

// Set up app logging to a file with rotation.
tm.applog = function(filepath, maxsize, callback) {
if (!filepath) return callback();

fs.stat(filepath, function(err, stat) {
if (err && err.code !== 'ENOENT') return callback(err);
if (stat && !stat.isFile()) return callback(new Error(filepath + ' is not a file'));
if (!err && stat && stat.size > maxsize) {
rotate(filepath, { compress: true }, function(err) {
if (err) return callback(err);
setup(0);
});
} else {
setup(stat && stat.size || 0);
}
});

function setup(offset) {
var logstream = fs.createWriteStream(filepath, {
flags: offset ? 'r+' : 'w',
start: offset
});
logstream.on('error', function (err) {
console.warn('Problem writing to logfile at ' + filepath);
return callback();
})
var pipeout = through();
var pipeerr = through();
pipeout.pipe(logstream);
pipeout.pipe(process.stdout);
pipeerr.pipe(logstream);
pipeerr.pipe(process.stderr);
process.__defineGetter__('stdout', function() { return pipeout; });
process.__defineGetter__('stderr', function() { return pipeerr; });
return callback();
}
};

// Set or remove a project id from recent history.
var defaultSources = [
'mapbox:///mapbox.mapbox-streets-v5',
Expand Down

0 comments on commit 4b334c1

Please sign in to comment.