Kouhei Sutou
null+****@clear*****
Wed Sep 11 17:39:59 JST 2013
Kouhei Sutou 2013-09-11 17:39:59 +0900 (Wed, 11 Sep 2013) New Revision: 5611e14e710ad92ee86926c343c29931dcc99995 https://github.com/groonga/groonga/commit/5611e14e710ad92ee86926c343c29931dcc99995 Message: httpd: support groonga_query_log_path directive Support query log with the same format as groonga server's one. TODO: Document it. Modified files: data/groonga-httpd.conf.in groonga-httpd-conf.sh.in src/httpd/nginx-module/config src/httpd/nginx-module/ngx_http_groonga_module.c Modified: data/groonga-httpd.conf.in (+2 -0) =================================================================== --- data/groonga-httpd.conf.in 2013-09-09 23:44:30 +0900 (9e95944) +++ data/groonga-httpd.conf.in 2013-09-11 17:39:59 +0900 (fb3c816) @@ -38,6 +38,8 @@ http { groonga on; # You can disable log for groonga. # groonga_log_path off; + # You can disable query log for groonga. + # groonga_query_log_path off; # You can custom database path. # groonga_database /path/to/groonga/db; } Modified: groonga-httpd-conf.sh.in (+1 -0) =================================================================== --- groonga-httpd-conf.sh.in 2013-09-09 23:44:30 +0900 (c30ddf4) +++ groonga-httpd-conf.sh.in 2013-09-11 17:39:59 +0900 (22a90d8) @@ -17,6 +17,7 @@ export GROONGA_HTTPD_CONF_PATH="${pkgsysconfdir}/httpd/groonga-httpd.conf" export GROONGA_HTTPD_ERROR_LOG_PATH="${localstatedir}/log/groonga/httpd/error.log" export GROONGA_HTTPD_HTTP_LOG_PATH="${localstatedir}/log/groonga/httpd/access.log" export GROONGA_HTTPD_GROONGA_LOG_PATH="${localstatedir}/log/groonga/httpd/groonga.log" +export GROONGA_HTTPD_GROONGA_QUERY_LOG_PATH="${localstatedir}/log/groonga/httpd/groonga-query.log" export GROONGA_HTTPD_PID_PATH="${localstatedir}/run/groonga/groonga-httpd.pid" export GROONGA_HTTPD_DEBUG="@grn_debug@" export GROONGA_HTTPD_WITH_PCRE="@GRN_WITH_PCRE@" Modified: src/httpd/nginx-module/config (+1 -0) =================================================================== --- src/httpd/nginx-module/config 2013-09-09 23:44:30 +0900 (aefb2fb) +++ src/httpd/nginx-module/config 2013-09-11 17:39:59 +0900 (fc1fe16) @@ -9,6 +9,7 @@ groonga_strip_switch() if [ "$GROONGA_HTTPD_IN_TREE" = yes ]; then groonga_cflags="-I${GROONGA_HTTPD_IN_TREE_INCLUDE_PATH}" groonga_cflags="${groonga_cflags} -DNGX_HTTP_GROONGA_LOG_PATH=\\\"\"${GROONGA_HTTPD_GROONGA_LOG_PATH}\"\\\"" + groonga_cflags="${groonga_cflags} -DNGX_HTTP_GROONGA_QUERY_LOG_PATH=\\\"\"${GROONGA_HTTPD_GROONGA_QUERY_LOG_PATH}\"\\\"" groonga_libs="-L${GROONGA_HTTPD_IN_TREE_LINK_PATH} -lgroonga" if [ -n "${GROONGA_HTTPD_RPATH}" ]; then groonga_libs="$groonga_libs -Wl,-rpath -Wl,${GROONGA_HTTPD_RPATH}" Modified: src/httpd/nginx-module/ngx_http_groonga_module.c (+156 -8) =================================================================== --- src/httpd/nginx-module/ngx_http_groonga_module.c 2013-09-09 23:44:30 +0900 (bbcd695) +++ src/httpd/nginx-module/ngx_http_groonga_module.c 2013-09-11 17:39:59 +0900 (0c54aaa) @@ -37,6 +37,8 @@ typedef struct { ngx_str_t log_path; ngx_open_file_t *log_file; grn_log_level log_level; + ngx_str_t query_log_path; + ngx_open_file_t *query_log_file; size_t cache_limit; char *config_file; int config_line; @@ -63,6 +65,12 @@ typedef struct { ngx_open_file_t *file; } ngx_http_groonga_logger_data_t; +typedef struct { + ngx_pool_t *pool; + ngx_open_file_t *file; + ngx_str_t *path; +} ngx_http_groonga_query_logger_data_t; + typedef void (*ngx_http_groonga_loc_conf_callback_pt)(ngx_http_groonga_loc_conf_t *conf, void *user_data); ngx_module_t ngx_http_groonga_module; @@ -142,12 +150,87 @@ static grn_logger ngx_http_groonga_logger = { ngx_http_groonga_logger_fin }; +static void +ngx_http_groonga_query_logger_log(grn_ctx *ctx, unsigned int flag, + const char *timestamp, const char *info, + const char *message, void *user_data) +{ + ngx_http_groonga_query_logger_data_t *data = user_data; + u_char buffer[NGX_MAX_ERROR_STR]; + u_char *last; + + last = ngx_slprintf(buffer, buffer + NGX_MAX_ERROR_STR, + "%s|%s%s\n", + timestamp, info, message); + ngx_write_fd(data->file->fd, buffer, last - buffer); +} + +static void +ngx_http_groonga_query_logger_reopen(grn_ctx *ctx, void *user_data) +{ + ngx_http_groonga_query_logger_data_t *data = user_data; + + GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_DESTINATION, " ", + "query log will be closed: <%.*s>", + (int)(data->path->len), data->path->data); + ngx_reopen_files((ngx_cycle_t *)ngx_cycle, -1); + GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_DESTINATION, " ", + "query log is opened: <%.*s>", + (int)(data->path->len), data->path->data); +} + +static void +ngx_http_groonga_query_logger_fin(grn_ctx *ctx, void *user_data) +{ + ngx_http_groonga_query_logger_data_t *data = user_data; + + ngx_pfree(data->pool, data); +} + +static grn_query_logger ngx_http_groonga_query_logger = { + GRN_QUERY_LOG_DEFAULT, + NULL, + ngx_http_groonga_query_logger_log, + ngx_http_groonga_query_logger_reopen, + ngx_http_groonga_query_logger_fin +}; + +static ngx_int_t +ngx_http_groonga_context_init_query_logger(grn_ctx *context, + ngx_http_groonga_loc_conf_t *location_conf, + ngx_pool_t *pool, + ngx_log_t *log) +{ + ngx_http_groonga_query_logger_data_t *query_logger_data; + + if (!location_conf->query_log_file) { + return NGX_OK; + } + + query_logger_data = ngx_pcalloc(pool, + sizeof(ngx_http_groonga_query_logger_data_t)); + if (!query_logger_data) { + ngx_log_error(NGX_LOG_ERR, log, 0, + "http_groonga: failed to allocate memory for query logger"); + return NGX_ERROR; + } + + query_logger_data->pool = pool; + query_logger_data->file = location_conf->query_log_file; + query_logger_data->path = &(location_conf->query_log_path); + ngx_http_groonga_query_logger.user_data = query_logger_data; + grn_query_logger_set(context, &ngx_http_groonga_query_logger); + + return NGX_OK; +} + static ngx_int_t ngx_http_groonga_context_init(grn_ctx *context, ngx_http_groonga_loc_conf_t *location_conf, ngx_pool_t *pool, ngx_log_t *log) { + ngx_int_t status; ngx_http_groonga_logger_data_t *logger_data; logger_data = ngx_pcalloc(pool, sizeof(ngx_http_groonga_logger_data_t)); @@ -159,17 +242,23 @@ ngx_http_groonga_context_init(grn_ctx *context, grn_ctx_init(context, GRN_NO_FLAGS); - if (!location_conf->log_file) { - return NGX_OK; + if (location_conf->log_file) { + logger_data->pool = pool; + logger_data->file = location_conf->log_file; + ngx_http_groonga_logger.max_level = location_conf->log_level; + ngx_http_groonga_logger.user_data = logger_data; + grn_logger_set(context, &ngx_http_groonga_logger); } - logger_data->pool = pool; - logger_data->file = location_conf->log_file; - ngx_http_groonga_logger.max_level = location_conf->log_level; - ngx_http_groonga_logger.user_data = logger_data; - grn_logger_set(context, &ngx_http_groonga_logger); + status = ngx_http_groonga_context_init_query_logger(context, + location_conf, + pool, + log); + if (status == NGX_ERROR) { + grn_ctx_fin(context); + } - return NGX_OK; + return status; } static void @@ -787,6 +876,41 @@ ngx_http_groonga_conf_set_log_level_slot(ngx_conf_t *cf, ngx_command_t *cmd, return status; } +static char * +ngx_http_groonga_conf_set_query_log_path_slot(ngx_conf_t *cf, + ngx_command_t *cmd, + void *conf) +{ + char *status; + ngx_http_groonga_loc_conf_t *groonga_location_conf = conf; + + status = ngx_conf_set_str_slot(cf, cmd, conf); + if (status != NGX_CONF_OK) { + return status; + } + + if (!groonga_location_conf->query_log_path.data) { + return NGX_CONF_OK; + } + + if (strncmp((const char *)(groonga_location_conf->query_log_path.data), + "off", + groonga_location_conf->query_log_path.len) == 0) { + return NGX_CONF_OK; + } + + groonga_location_conf->query_log_file = + ngx_conf_open_file(cf->cycle, &(groonga_location_conf->query_log_path)); + if (!groonga_location_conf->query_log_file) { + ngx_log_error(NGX_LOG_ERR, cf->cycle->log, 0, + "http_groonga: failed to open groonga query log file: <%V>", + &(groonga_location_conf->query_log_path)); + return NGX_CONF_ERROR; + } + + return NGX_CONF_OK; +} + static void * ngx_http_groonga_create_loc_conf(ngx_conf_t *cf) { @@ -807,6 +931,9 @@ ngx_http_groonga_create_loc_conf(ngx_conf_t *cf) conf->log_path.len = 0; conf->log_file = NULL; conf->log_level = GRN_LOG_DEFAULT_LEVEL; + conf->query_log_path.data = NULL; + conf->query_log_path.len = 0; + conf->query_log_file = NULL; conf->cache_limit = NGX_CONF_UNSET_SIZE; conf->config_file = NULL; conf->config_line = 0; @@ -842,6 +969,20 @@ ngx_http_groonga_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) conf->log_file = NULL; #endif + ngx_conf_merge_str_value(conf->query_log_path, prev->query_log_path, + NGX_HTTP_GROONGA_QUERY_LOG_PATH); + if (!conf->query_log_file && conf->query_log_path.data && conf->enabled) { + conf->query_log_file = ngx_conf_open_file(cf->cycle, + &(conf->query_log_path)); + if (!conf->query_log_file) { + ngx_log_error(NGX_LOG_ERR, cf->cycle->log, 0, + "http_groonga: " + "failed to open the default groonga query log file: <%V>", + &(conf->query_log_path)); + return NGX_CONF_ERROR; + } + } + return NGX_CONF_OK; } @@ -1120,6 +1261,13 @@ static ngx_command_t ngx_http_groonga_commands[] = { 0, NULL }, + { ngx_string("groonga_query_log_path"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_groonga_conf_set_query_log_path_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_groonga_loc_conf_t, query_log_path), + NULL }, + { ngx_string("groonga_cache_limit"), NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, ngx_conf_set_size_slot, -------------- next part -------------- HTML����������������������������...Download