YUKI Hiroshi
null+****@clear*****
Thu Nov 1 18:36:50 JST 2012
YUKI Hiroshi 2012-11-01 18:36:50 +0900 (Thu, 01 Nov 2012) New Revision: f3ff23ba460c96c30c38c5b6fcbcfd9498355d6b https://github.com/groonga/gcs/commit/f3ff23ba460c96c30c38c5b6fcbcfd9498355d6b Log: Implement error logger Modified files: lib/api/2011-02-01/batch.js lib/api/2011-02-01/configuration.js lib/api/2011-02-01/index.js lib/api/2011-02-01/search.js lib/server.js Modified: lib/api/2011-02-01/batch.js (+7 -1) =================================================================== --- lib/api/2011-02-01/batch.js 2012-11-01 17:18:17 +0900 (afb1d0a) +++ lib/api/2011-02-01/batch.js 2012-11-01 18:36:50 +0900 (10e084d) @@ -38,7 +38,7 @@ function handleInvalidContentLength(request, response) { return true; } -exports.createHandler = function(context) { +exports.createHandler = function(context, config) { return function(request, response) { if (handleInvalidContentType(request, response)) return; if (handleInvalidContentLength(request, response)) return; @@ -53,6 +53,9 @@ exports.createHandler = function(context) { try { processor.validate(batches); } catch (error) { + if (config.errorLogger) + config.errorLogger.log(error); + return response.send(JSON.stringify(error.result)); } @@ -62,6 +65,9 @@ exports.createHandler = function(context) { response.send(JSON.stringify(result)); }) .error(function(error) { + if (config.errorLogger) + config.errorLogger.log(error); + response.send(error.message + '\n' + error.stack, 502); }); }; Modified: lib/api/2011-02-01/configuration.js (+3 -0) =================================================================== --- lib/api/2011-02-01/configuration.js 2012-11-01 17:18:17 +0900 (e6cc909) +++ lib/api/2011-02-01/configuration.js 2012-11-01 18:36:50 +0900 (020615f) @@ -472,6 +472,9 @@ exports.createHandler = function(context, config) { try { handler(context, request, response, config); } catch (error) { + if (config.errorLogger) + config.errorLogger.log(error); + var body = createCommonErrorResponse('InternalFailure', error); response.contentType('application/xml'); response.send(body, 400); Modified: lib/api/2011-02-01/index.js (+4 -4) =================================================================== --- lib/api/2011-02-01/index.js 2012-11-01 17:18:17 +0900 (5c841ad) +++ lib/api/2011-02-01/index.js 2012-11-01 18:36:50 +0900 (4cd9689) @@ -13,12 +13,12 @@ exports.registerHandlers = function(application, database, config) { exports.configuration.createHandler(database, config)); application.post('/' + exports.version + '/documents/batch', - exports.batch.createHandler(database)); + exports.batch.createHandler(database, config)); application.get('/gcs/:domain/' + exports.version + '/documents/batch', - exports.batch.createHandler(database)); + exports.batch.createHandler(database, config)); application.get('/' + exports.version + '/search', - exports.search.createHandler(database)); + exports.search.createHandler(database, config)); application.get('/gcs/:domain/' + exports.version + '/search', - exports.search.createHandler(database)); + exports.search.createHandler(database, config)); }; Modified: lib/api/2011-02-01/search.js (+7 -1) =================================================================== --- lib/api/2011-02-01/search.js 2012-11-01 17:18:17 +0900 (c60ecc4) +++ lib/api/2011-02-01/search.js 2012-11-01 18:36:50 +0900 (3a0fc71) @@ -131,7 +131,7 @@ function createErrorBody(options) { }; } -exports.createHandler = function(context) { +exports.createHandler = function(context, config) { return function(request, response) { var startedAt = new Date(); @@ -139,6 +139,9 @@ exports.createHandler = function(context) { try { selectQuery = new SelectQuery(request, context); } catch(error) { + if (config.errorLogger) + config.errorLogger.log(error); + var errorData = { rid: dummyRid }; if (error.message == 'validation error') { errorData.messages = [error.data]; @@ -151,6 +154,9 @@ exports.createHandler = function(context) { select(context, selectQuery, function(error, data, numFoundRecords, facets) { + if (error && config.errorLogger) + config.errorLogger.log(error); + var finishedAt = new Date(); var elapsedTime = finishedAt.getTime() - startedAt.getTime(); if (error) { Modified: lib/server.js (+43 -5) =================================================================== --- lib/server.js 2012-11-01 17:18:17 +0900 (bbf78cd) +++ lib/server.js 2012-11-01 18:36:50 +0900 (e2f99da) @@ -1,23 +1,48 @@ var express = require('express'); +var fs = require('fs'); +var path = require('path'); var nroonga = require('./wrapped-nroonga'); var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface; +var logger = require(__dirname + '/../lib/logger'); +var Logger = logger.Logger; +var ErrorLogger = logger.ErrorLogger; var api = require('./api'); -exports.createServer = function (config) { +function prepareConfigurations(config) { config = config || {}; if (!config.databasePath) config.databasePath = CLI.defaultDatabasePath; + if (!config.accessLogPath) + config.accessLogPath = CLI.defaultAccessLogPath; + if (!config.queryLogPath) + config.queryLogPath = CLI.defaultQueryLogPath; + if (!config.errorLogPath) + config.errorLogPath = CLI.defaultErrorLogPath; if (!config.privilegedRanges) config.privilegedRanges = CLI.defaultPrivilegedRanges; if (!config.port) config.port = CLI.defaultPort; - var context = config.context || new nroonga.Context(config.databasePath); - var application = express.createServer(); - application.use(express.bodyParser()); - application.set('views', __dirname + '/../views'); + return config; +} + +function prepareLoggers(application, config) { + if (config.accessLogPath) { + var accessLogPath = CLI.resolve(config.accessLogPath); + var accessLogFlags = path.existsSync(accessLogPath) ? 'a' : 'w'; + var accessLogStream = fs.createWriteStream(accessLogPath, { flags: accessLogFlags }); + application.use(express.logger({ stream: accessLogStream })); + } + if (config.queryLogPath) { + config.queryLogger = new Logger(config.queryLogPath); + } + if (config.errorLogPath) { + config.errorLogger = new ErrorLogger(config.errorLogPath); + } +} +function registerHandlers(application, context, config) { application.configure(function() { application.enable('jsonp callback'); }); @@ -33,6 +58,19 @@ exports.createServer = function (config) { api.versions.forEach(function(version) { api[version].registerHandlers(application, context, config); }); +} + +exports.createServer = function (config) { + config = prepareConfigurations(config); + + var context = config.context || new nroonga.Context(config.databasePath); + var application = express.createServer(); + application.use(express.bodyParser()); + application.set('views', __dirname + '/../views'); + + prepareLoggers(application, config); + + registerHandlers(application, context, config); return application; }; -------------- next part -------------- HTML����������������������������...Download