Adds support to append logs to file with log4js. It listens to HUP signal to re-open the log file. In case the specified log filename is not writeable the server will not start.

This commit is contained in:
Raul Ochoa 2014-05-27 11:51:44 +02:00 committed by Luis Bosque
parent da23c96e98
commit bd6c1e7164
5 changed files with 40 additions and 5 deletions

33
app.js
View File

@ -9,7 +9,9 @@
* environments: [development, test, production]
*
*/
var _ = require('underscore');
var _ = require('underscore'),
fs = require('fs'),
path = require('path');
if ( process.argv[2] ) ENV = process.argv[2];
else if ( process.env['NODE_ENV'] ) ENV = process.env['NODE_ENV'];
@ -31,12 +33,28 @@ _.extend(global.settings, env);
global.log4js = require('log4js')
log4js_config = {
appenders: [
{ type: "console", layout: { type:'basic' } }
],
appenders: [],
replaceConsole:true
};
if ( env.log_filename ) {
var logdir = path.dirname(env.log_filename);
// See cwd inlog4js.configure call below
logdir = path.resolve(__dirname, logdir);
if ( ! fs.existsSync(logdir) ) {
console.error("Log filename directory does not exist: " + logdir);
process.exit(1);
}
console.log("Logs will be written to " + env.log_filename);
log4js_config.appenders.push(
{ type: "file", filename: env.log_filename }
);
} else {
log4js_config.appenders.push(
{ type: "console", layout: { type:'basic' } }
);
}
if ( global.settings.rollbar ) {
log4js_config.appenders.push({
type: __dirname + "/app/models/log4js_rollbar.js",
@ -44,7 +62,7 @@ if ( global.settings.rollbar ) {
});
}
log4js.configure(log4js_config);
log4js.configure(log4js_config, { cwd: __dirname });
global.logger = log4js.getLogger();
@ -64,3 +82,8 @@ app.listen(global.settings.node_port, global.settings.node_host, function() {
process.on('uncaughtException', function(err) {
logger.error('Uncaught exception: ' + err.stack);
});
process.on('SIGHUP', function() {
log4js.configure(log4js_config);
console.log('Log files reloaded');
});

View File

@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
// steps taken for producing the response.
module.exports.useProfiler = true;
module.exports.log_format = '[:date] :req[X-Real-IP] :method :req[Host]:url :status :response-time ms -> :res[Content-Type] (:res[X-SQLAPI-Profiler])';
// If log_filename is given logs will be written there, in append mode. Otherwise stdout is used (default).
// Log file will be re-opened on receiving the HUP signal
module.exports.log_filename = 'logs/cartodb-sql-api.log';
// Regular expression pattern to extract username
// from hostname. Must have a single grabbing block.
module.exports.user_from_host = '^(.*)\\.localhost';

View File

@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
// steps taken for producing the response.
module.exports.useProfiler = true;
module.exports.log_format = '[:date] :req[X-Real-IP] :method :req[Host]:url :status :response-time ms -> :res[Content-Type] (:res[X-SQLAPI-Profiler])';
// If log_filename is given logs will be written there, in append mode. Otherwise stdout is used (default).
// Log file will be re-opened on receiving the HUP signal
module.exports.log_filename = 'logs/cartodb-sql-api.log';
// Regular expression pattern to extract username
// from hostname. Must have a single grabbing block.
module.exports.user_from_host = '^(.*)\\.cartodb\\.com$';

View File

@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
// steps taken for producing the response.
module.exports.useProfiler = true;
module.exports.log_format = '[:date] :req[X-Real-IP] :method :req[Host]:url :status :response-time ms -> :res[Content-Type] (:res[X-SQLAPI-Profiler])';
// If log_filename is given logs will be written there, in append mode. Otherwise stdout is used (default).
// Log file will be re-opened on receiving the HUP signal
module.exports.log_filename = 'logs/cartodb-sql-api.log';
// Regular expression pattern to extract username
// from hostname. Must have a single grabbing block.
module.exports.user_from_host = '^(.*)\\.cartodb\\.com$';

View File

@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
// steps taken for producing the response.
module.exports.useProfiler = true;
module.exports.log_format = '[:date] :req[X-Real-IP] :method :req[Host]:url :status :response-time ms -> :res[Content-Type] (:res[X-SQLAPI-Profiler])';
// If log_filename is given logs will be written there, in append mode. Otherwise stdout is used (default).
// Log file will be re-opened on receiving the HUP signal
// module.exports.log_filename = 'logs/cartodb-sql-api.log';
// Regular expression pattern to extract username
// from hostname. Must have a single grabbing block.
module.exports.user_from_host = '^([^.]*)\\.';