diff --git a/app.js b/app.js index d8c8c8d1..3bd03412 100755 --- a/app.js +++ b/app.js @@ -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'); +}); diff --git a/config/environments/development.js.example b/config/environments/development.js.example index bc0365c0..a2f6f497 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -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'; diff --git a/config/environments/production.js.example b/config/environments/production.js.example index 772b5584..032bfd6d 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -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$'; diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 07332b72..1841c368 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -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$'; diff --git a/config/environments/test.js.example b/config/environments/test.js.example index e87969f3..264833e2 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -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 = '^([^.]*)\\.';