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:
parent
da23c96e98
commit
bd6c1e7164
33
app.js
33
app.js
@ -9,7 +9,9 @@
|
|||||||
* environments: [development, test, production]
|
* environments: [development, test, production]
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
var _ = require('underscore');
|
var _ = require('underscore'),
|
||||||
|
fs = require('fs'),
|
||||||
|
path = require('path');
|
||||||
|
|
||||||
if ( process.argv[2] ) ENV = process.argv[2];
|
if ( process.argv[2] ) ENV = process.argv[2];
|
||||||
else if ( process.env['NODE_ENV'] ) ENV = process.env['NODE_ENV'];
|
else if ( process.env['NODE_ENV'] ) ENV = process.env['NODE_ENV'];
|
||||||
@ -31,12 +33,28 @@ _.extend(global.settings, env);
|
|||||||
|
|
||||||
global.log4js = require('log4js')
|
global.log4js = require('log4js')
|
||||||
log4js_config = {
|
log4js_config = {
|
||||||
appenders: [
|
appenders: [],
|
||||||
{ type: "console", layout: { type:'basic' } }
|
|
||||||
],
|
|
||||||
replaceConsole:true
|
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 ) {
|
if ( global.settings.rollbar ) {
|
||||||
log4js_config.appenders.push({
|
log4js_config.appenders.push({
|
||||||
type: __dirname + "/app/models/log4js_rollbar.js",
|
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();
|
global.logger = log4js.getLogger();
|
||||||
|
|
||||||
|
|
||||||
@ -64,3 +82,8 @@ app.listen(global.settings.node_port, global.settings.node_host, function() {
|
|||||||
process.on('uncaughtException', function(err) {
|
process.on('uncaughtException', function(err) {
|
||||||
logger.error('Uncaught exception: ' + err.stack);
|
logger.error('Uncaught exception: ' + err.stack);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
process.on('SIGHUP', function() {
|
||||||
|
log4js.configure(log4js_config);
|
||||||
|
console.log('Log files reloaded');
|
||||||
|
});
|
||||||
|
@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
|
|||||||
// steps taken for producing the response.
|
// steps taken for producing the response.
|
||||||
module.exports.useProfiler = true;
|
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])';
|
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
|
// Regular expression pattern to extract username
|
||||||
// from hostname. Must have a single grabbing block.
|
// from hostname. Must have a single grabbing block.
|
||||||
module.exports.user_from_host = '^(.*)\\.localhost';
|
module.exports.user_from_host = '^(.*)\\.localhost';
|
||||||
|
@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
|
|||||||
// steps taken for producing the response.
|
// steps taken for producing the response.
|
||||||
module.exports.useProfiler = true;
|
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])';
|
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
|
// Regular expression pattern to extract username
|
||||||
// from hostname. Must have a single grabbing block.
|
// from hostname. Must have a single grabbing block.
|
||||||
module.exports.user_from_host = '^(.*)\\.cartodb\\.com$';
|
module.exports.user_from_host = '^(.*)\\.cartodb\\.com$';
|
||||||
|
@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
|
|||||||
// steps taken for producing the response.
|
// steps taken for producing the response.
|
||||||
module.exports.useProfiler = true;
|
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])';
|
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
|
// Regular expression pattern to extract username
|
||||||
// from hostname. Must have a single grabbing block.
|
// from hostname. Must have a single grabbing block.
|
||||||
module.exports.user_from_host = '^(.*)\\.cartodb\\.com$';
|
module.exports.user_from_host = '^(.*)\\.cartodb\\.com$';
|
||||||
|
@ -4,6 +4,9 @@ module.exports.base_url = '/api/:version';
|
|||||||
// steps taken for producing the response.
|
// steps taken for producing the response.
|
||||||
module.exports.useProfiler = true;
|
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])';
|
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
|
// Regular expression pattern to extract username
|
||||||
// from hostname. Must have a single grabbing block.
|
// from hostname. Must have a single grabbing block.
|
||||||
module.exports.user_from_host = '^([^.]*)\\.';
|
module.exports.user_from_host = '^([^.]*)\\.';
|
||||||
|
Loading…
Reference in New Issue
Block a user