Class: Groonga::QueryLogger

Inherits:
Object
  • Object
show all
Defined in:
ext/groonga/rb-grn-query-logger.c,
lib/groonga/query-logger.rb,
ext/groonga/rb-grn-query-logger.c

Overview

A class for logging query log.

Direct Known Subclasses

CallbackQueryLogger, FileQueryLogger

Defined Under Namespace

Modules: Flags

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log(message, options = {}) ⇒ void

Logs a message.

This method returns an undefined value.

Parameters:

  • message (String)

    The log message.

  • options (::Hash) (defaults to: {})

Options Hash (options):

  • :context (Groonga::Context) — default: Groonga::Context.default

    The context for the message.

  • :flags (nil, Integer, String) — default: 0

    The flags for the message.

    The flags are passed to query logger. You can custom query logger behavior by the flags. For example, you can omit elapsed time by passing Groonga::QueryLogger::COMMAND flag or Groonga::QueryLogger::DESTINATION.

    If :flags value is String, parsed by parse.

    nil equals to 0.

  • :mark (String) — default: ":" The mark for the message. Normally, a character is used as a mark such as `":"`, `"<"` and `">"`.

    ””) The mark for the message.

    Normally, a character is used as a mark such as ":", "<" and ">".

Since:

  • 5.0.2



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/groonga/rb-grn-query-logger.c', line 76

static VALUE
rb_grn_query_logger_s_log (int argc, VALUE *argv, VALUE klass)
{
    VALUE rb_message;
    const char *message;
    VALUE rb_context = Qnil;
    grn_ctx *context;
    VALUE rb_flags;
    unsigned int flags = GRN_QUERY_LOG_NONE;
    VALUE rb_mark;
    const char *mark = "";
    VALUE rb_options;

    rb_scan_args(argc, argv, "11", &rb_message, &rb_options);

    message = StringValueCStr(rb_message);

    rb_grn_scan_options(rb_options,
                        "context", &rb_context,
                        "flags",   &rb_flags,
                        "mark",    &rb_mark,
                        NULL);

    context = rb_grn_context_ensure(&rb_context);

    if (!NIL_P(rb_flags)) {
        flags = rb_funcall(mGrnQueryLoggerFlags, id_parse, 2,
                           rb_flags, UINT2NUM(flags));
    }
    if (!NIL_P(rb_mark)) {
        mark = StringValueCStr(rb_mark);
    }

    grn_query_logger_put(context, flags, mark, "%s", message);

    return Qnil;
}

.pathString or nil

Gets the current query log path that is used the default query logger.

Returns The current query log path

Returns:

  • (String or nil)

    The current query log path

Since:

  • 3.0.1



295
296
297
298
299
300
301
302
303
304
305
306
# File 'ext/groonga/rb-grn-query-logger.c', line 295

static VALUE
rb_grn_query_logger_s_get_path (VALUE klass)
{
    const char *path;
    VALUE rb_path = Qnil;

    path = grn_default_query_logger_get_path();
    if (path) {
        rb_path = rb_str_new_cstr(path);
    }
    return rb_path;
}

.path=(path) ⇒ Object

Sets the query log path that is used the default query logger. If you’re using custom query logger by register, the query log path isn’t used. Because it is for the default query logger.

If you specify nil as path, query logging by the default query logger is disabled.

Examples:

Changes the query log path for the default query logger

Groonga::QueryLogger.path = "/tmp/query.log"

Disables query log by the default query logger

Groonga::QueryLogger.path = nil

Returns void

Parameters:

  • path (String or nil)

    The query log path for the default query logger. If nil is specified, query logging by the default query logger is disabled.

Returns:

  • void

Since:

  • 3.0.1



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'ext/groonga/rb-grn-query-logger.c', line 330

static VALUE
rb_grn_query_logger_s_set_path (VALUE klass, VALUE rb_path)
{
    grn_bool need_reopen = GRN_FALSE;
    const char *old_path = NULL;
    const char *path = NULL;

    rb_path = rb_grn_check_convert_to_string(rb_path);
    if (!NIL_P(rb_path)) {
        path = StringValuePtr(rb_path);
    }

    old_path = grn_default_query_logger_get_path();
    if (!rb_grn_equal_string(old_path, path)) {
        need_reopen = GRN_TRUE;
    }

    grn_default_query_logger_set_path(path);

    if (need_reopen) {
        rb_grn_query_logger_s_reopen(klass);
    }

    return Qnil;
}

.register(logger, options = {}) ⇒ Object .register(options = {}) {|action, flag, timestamp, info, message| ... } ⇒ Object

Registers a query logger or a callback that is called when a query log event is emitted.

Overloads:

  • .register(logger, options = {}) ⇒ Object

    Returns void

    Parameters:

    Returns:

    • void

  • .register(options = {}) {|action, flag, timestamp, info, message| ... } ⇒ Object

    Parameters:

    • options (::Hash) (defaults to: {})

      The options.

    Options Hash (options):

    • :flags (Symbol, String, Integer or nil) — default: :default

      Flags describe what query log should be logged.

      If flags is String, it is parsed by Groonga::QueryLogger::Flags.parse.

    Yields:

    • (action, flag, timestamp, info, message)



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/groonga/rb-grn-query-logger.c', line 179

static VALUE
rb_grn_query_logger_s_register (int argc, VALUE *argv, VALUE klass)
{
    VALUE rb_context = Qnil;
    grn_ctx *context;
    VALUE rb_logger, rb_callback;
    VALUE rb_options, rb_command, rb_result_code, rb_destination;
    VALUE rb_cache, rb_size, rb_score, rb_default, rb_all, rb_flags;
    unsigned int flags = GRN_QUERY_LOG_NONE;

    rb_scan_args(argc, argv, "02&", &rb_logger, &rb_options, &rb_callback);

    if (rb_block_given_p()) {
        rb_logger = rb_funcall(cGrnCallbackQueryLogger, id_new, 1, rb_callback);
    }

    rb_grn_scan_options(rb_options,
                        "command",     &rb_command,
                        "result_code", &rb_result_code,
                        "destination", &rb_destination,
                        "cache",       &rb_cache,
                        "size",        &rb_size,
                        "score",       &rb_score,
                        "default",     &rb_default,
                        "all",         &rb_all,
                        "flags",       &rb_flags,
                        NULL);

    if (RVAL2CBOOL(rb_command)) {
        flags |= GRN_QUERY_LOG_COMMAND;
    }
    if (RVAL2CBOOL(rb_result_code)) {
        flags |= GRN_QUERY_LOG_RESULT_CODE;
    }
    if (RVAL2CBOOL(rb_destination)) {
        flags |= GRN_QUERY_LOG_DESTINATION;
    }
    if (RVAL2CBOOL(rb_cache)) {
        flags |= GRN_QUERY_LOG_CACHE;
    }
    if (RVAL2CBOOL(rb_size)) {
        flags |= GRN_QUERY_LOG_SIZE;
    }
    if (RVAL2CBOOL(rb_score)) {
        flags |= GRN_QUERY_LOG_SCORE;
    }
    if (RVAL2CBOOL(rb_default)) {
        flags |= GRN_QUERY_LOG_DEFAULT;
    }
    if (RVAL2CBOOL(rb_all)) {
        flags |= GRN_QUERY_LOG_ALL;
    }
    if (!NIL_P(rb_flags)) {
        flags = rb_funcall(mGrnQueryLoggerFlags, id_parse, 2,
                           rb_flags, UINT2NUM(flags));
    }

    rb_grn_query_logger.flags     = flags;
    rb_grn_query_logger.user_data = (void *)rb_logger;

    context = rb_grn_context_ensure(&rb_context);
    grn_query_logger_set(context, &rb_grn_query_logger);
    rb_grn_context_check(context, rb_logger);
    rb_cv_set(klass, "@@current_logger", rb_logger);

    return Qnil;
}

.reopenObject

Sends reopen request to the current query logger. It is useful for rotating log file.

Returns:

  • void



274
275
276
277
278
279
280
281
282
283
284
285
# File 'ext/groonga/rb-grn-query-logger.c', line 274

static VALUE
rb_grn_query_logger_s_reopen (VALUE klass)
{
    VALUE rb_context = Qnil;
    grn_ctx *context;

    context = rb_grn_context_ensure(&rb_context);
    grn_query_logger_reopen(context);
    rb_grn_context_check(context, klass);

    return Qnil;
}

.thresholdInteger

Gets the current rotate threshold size that is used by the default query logger.

If the size is larger than 0, log rotate feature is enabled in the default query logger.

Returns The current rotate threshold size

Returns:

  • (Integer)

    The current rotate threshold size

Since:

  • 5.0.2



368
369
370
371
372
# File 'ext/groonga/rb-grn-query-logger.c', line 368

static VALUE
rb_grn_query_logger_s_get_rotate_threshold_size (VALUE klass)
{
    return OFFT2NUM(grn_default_query_logger_get_rotate_threshold_size());
}

.rotate_threshold_size=(size) ⇒ Object

Sets the rotate threshold size that is used by the default query logger. If you’re using custom query logger by register, the rotate threshold size isn’t used. Because it is for the default query logger.

If you specify 0 as size, log rotation by the default query logger is disabled.

The default rotate threshold size is 0. It means that log rotation is disabled by default.

Examples:

Changes the rotate threshold size for the default query logger

Groonga::QueryLogger.rotate_threshold_size = 1 * 1024 * 1024 # 1MiB

Disables log ration by the default query logger

Groonga::QueryLogger.rotate_threshold_size = 0

Returns void

Parameters:

  • size (Integer)

    The log path for the default query logger. If nil is specified, log rotate by the default query logger is disabled.

Returns:

  • void

Since:

  • 5.0.2



400
401
402
403
404
405
406
# File 'ext/groonga/rb-grn-query-logger.c', line 400

static VALUE
rb_grn_query_logger_s_set_rotate_threshold_size (VALUE klass, VALUE rb_size)
{
    grn_default_query_logger_set_rotate_threshold_size(NUM2OFFT(rb_size));

    return Qnil;
}

.unregisterObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/groonga/rb-grn-query-logger.c', line 247

static VALUE
rb_grn_query_logger_s_unregister (VALUE klass)
{
    VALUE current_logger;
    VALUE rb_context = Qnil;
    grn_ctx *context;

    current_logger = rb_cv_get(klass, "@@current_logger");
    if (NIL_P(current_logger))
        return Qnil;

    rb_cv_set(klass, "@@current_logger", Qnil);

    context = rb_grn_context_ensure(&rb_context);
    grn_query_logger_set(context, NULL);
    rb_grn_context_check(context, klass);

    return Qnil;
}

Instance Method Details

#finObject



101
102
# File 'lib/groonga/query-logger.rb', line 101

def fin
end

#log(flag, timestamp, info, message) ⇒ Object



92
93
94
95
96
# File 'lib/groonga/query-logger.rb', line 92

def log(flag, timestamp, info, message)
  guard do
    puts("#{timestamp}|#{info}#{message}")
  end
end

#reopenObject



98
99
# File 'lib/groonga/query-logger.rb', line 98

def reopen
end