Class: Groonga::Logger

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

Overview

groongaから出力されるログを記録するためのクラス。

Direct Known Subclasses

CallbackLogger, FileLogger

Defined Under Namespace

Modules: Flags

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flagsInteger

Gets the current log flags that is used by the default logger.

Returns The current log flags.

Returns:

  • (Integer)

    The current log flags.

Since:

  • 6.1.3



559
560
561
562
563
564
565
566
567
568
569
# File 'ext/groonga/rb-grn-logger.c', line 559

static VALUE
rb_grn_logger_s_get_flags (VALUE klass)
{
    int flags = 0;
    VALUE rb_flags;

    flags = grn_default_logger_get_flags();
    rb_flags = INT2NUM(flags);

    return rb_flags;
}

.flags=(flags) ⇒ Object

Sets the log flags that is used by the default logger.

Returns void

Parameters:

  • flags (Integer)

    The log flags for the default logger.

Returns:

  • void

Since:

  • 6.1.3



580
581
582
583
584
585
586
# File 'ext/groonga/rb-grn-logger.c', line 580

static VALUE
rb_grn_logger_s_set_flags (VALUE klass, VALUE rb_flags)
{
    grn_default_logger_set_flags(NUM2INT(rb_flags));

    return Qnil;
}

.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.

  • :level (nil, :none, :emergency, :alert, :critical, :error, :warning, :notice, :info, :debug, :dump) — default: :notice

    The level for the message.

    nil equals to :notice.

  • :file (nil, String) — default: nil

    The file name where the message is occurred.

    If all of :file, :line and :function are nil, these values are guessed from Kernel.#caller_locations result.

  • :line (nil, Integer) — default: nil

    The line number where the message is occurred.

  • :function (nil, String) — default: nil

    The function or related name such as method name where the message is occurred.

Since:

  • 5.0.2



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
# File 'ext/groonga/rb-grn-logger.c', line 158

static VALUE
rb_grn_logger_s_log (int argc, VALUE *argv, VALUE klass)
{
    VALUE rb_message;
    const char *message;
    VALUE rb_context = Qnil;
    grn_ctx *context;
    VALUE rb_level;
    grn_log_level level = GRN_LOG_DEFAULT_LEVEL;
    VALUE rb_file;
    const char *file = NULL;
    VALUE rb_line;
    int line = 0;
    VALUE rb_function;
    const char *function = NULL;
    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,
                        "level",    &rb_level,
                        "file",     &rb_file,
                        "line",     &rb_line,
                        "function", &rb_function,
                        NULL);

    context = rb_grn_context_ensure(&rb_context);

    if (!NIL_P(rb_level)) {
        level = RVAL2GRNLOGLEVEL(rb_level);
    }

    if (NIL_P(rb_file) && NIL_P(rb_line) && NIL_P(rb_function)) {
        VALUE rb_locations;
        VALUE rb_location;
        rb_locations = rb_funcall(rb_cObject,
                                  id_caller_locations,
                                  2,
                                  INT2NUM(1), INT2NUM(1));
        rb_location = RARRAY_PTR(rb_locations)[0];
        rb_file = rb_funcall(rb_location, id_path, 0);
        rb_line = rb_funcall(rb_location, id_lineno, 0);
        rb_function = rb_funcall(rb_location, id_label, 0);
    }

    if (!NIL_P(rb_file)) {
        file = StringValueCStr(rb_file);
    }
    if (!NIL_P(rb_line)) {
        line = NUM2INT(rb_line);
    }
    if (!NIL_P(rb_function)) {
        function = StringValueCStr(rb_function);
    }

    grn_logger_put(context, level, file, line, function, "%s", message);

    return Qnil;
}

.log_pathObject

Deprecated.

since 3.0.1. Use path instead.



49
50
51
# File 'lib/groonga/logger.rb', line 49

def log_path
  path
end

.log_path=(path) ⇒ Object

Deprecated.

since 3.0.1. Use path= instead.



55
56
57
# File 'lib/groonga/logger.rb', line 55

def log_path=(path)
  self.path = path
end

.max_levelSymbol

Returns The max level of the current logger.

Returns:

  • (Symbol)

    The max level of the current logger.

Since:

  • 5.0.5



448
449
450
451
452
453
454
455
456
457
458
459
# File 'ext/groonga/rb-grn-logger.c', line 448

static VALUE
rb_grn_logger_s_get_max_level (VALUE klass)
{
    VALUE rb_context = Qnil;
    grn_ctx *context;
    grn_log_level max_level;

    context = rb_grn_context_ensure(&rb_context);
    max_level = grn_logger_get_max_level(context);

    return GRNLOGLEVEL2RVAL(max_level);
}

.max_level=(max_level) ⇒ void

This method returns an undefined value.

Sets the max level of the current logger.

Parameters:

  • max_level (Symbol, String)

    The max level.

Since:

  • 5.0.5



471
472
473
474
475
476
477
478
479
480
481
# File 'ext/groonga/rb-grn-logger.c', line 471

static VALUE
rb_grn_logger_s_set_max_level (VALUE klass, VALUE rb_max_level)
{
    VALUE rb_context = Qnil;
    grn_ctx *context;

    context = rb_grn_context_ensure(&rb_context);
    grn_logger_set_max_level(context, RVAL2GRNLOGLEVEL(rb_max_level));

    return Qnil;
}

.pathString or nil

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

Returns The current log path

Returns:

  • (String or nil)

    The current log path

Since:

  • 3.0.1



491
492
493
494
495
496
497
498
499
500
501
502
# File 'ext/groonga/rb-grn-logger.c', line 491

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

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

.path=(path) ⇒ Object

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

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

Examples:

Changes the log path for the default logger

Groonga::Logger.path = "/tmp/groonga.log"

Disables log by the default logger

Groonga::Logger.path = nil

Returns void

Parameters:

  • path (String or nil)

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

Returns:

  • void

Since:

  • 3.0.1



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'ext/groonga/rb-grn-logger.c', line 525

static VALUE
rb_grn_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_logger_get_path();
    if (!rb_grn_equal_string(old_path, path)) {
        need_reopen = GRN_TRUE;
    }

    grn_default_logger_set_path(path);

    if (need_reopen) {
        rb_grn_logger_s_reopen_with_related_object(klass, rb_path);
    }

    return Qnil;
}

.query_log_pathObject

Deprecated.

since 3.0.1. Use QueryLogger.path instead.



61
62
63
# File 'lib/groonga/logger.rb', line 61

def query_log_path
  QueryLogger.path
end

.query_log_path=(path) ⇒ Object

Deprecated.

since 3.0.1. Use QueryLogger.path= instead.



67
68
69
# File 'lib/groonga/logger.rb', line 67

def query_log_path=(path)
  QueryLogger.path = path
end

.register(options = {}) {|event, level, time, title, message, location| ... } ⇒ Object

groongaがログを出力する度に呼び出されるブロックを登録する。

Parameters:

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

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :max_level (Object) — default: :notice

    ログのレベルを +:none+ , +:emergency+ , +:alert+ , +:critical+ , +:error+ , +:warning+ , +:notice+ , +:info+ , +:debug+ , +:dump+ のいずれかで指定する。それより重要度が 低いログはブロックに渡されなくなる。デフォルトでは +:notice+ 。

  • :time (Object)

    ログが出力された時間をブロックに渡したいなら +true+ を指 定する。デフォルトでは渡す。

  • :title (Object)

    ログのタイトルをブロックに渡したいなら +true+ を指定す る。デフォルトでは渡す。 (FIXME: groongaで実装されていない?)

  • :message (Object)

    ログのメッセージをブロックに渡したいなら +true+ を指定す る。デフォルトでは渡す。

  • :location (Object)

    ログの発生元のプロセスIDとgroongaのソースコードのファイ ル名、行番号、関数名をブロックに渡したいなら +true+ を指 定する。デフォルトでは渡す。

Yields:

  • (event, level, time, title, message, location)

    eventlevel はSymbol、それ以外は全て文字列で渡される。 eventlevel 以外 の4つについては options で +false+ を指定することでブロックに 渡さないようにすることができ、その場合は空文字列が実際には渡される。



319
320
321
322
323
324
325
326
327
328
329
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'ext/groonga/rb-grn-logger.c', line 319

static VALUE
rb_grn_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_max_level;
    VALUE rb_time, rb_title, rb_message, rb_location;
    VALUE rb_flags;
    grn_log_level max_level = GRN_LOG_DEFAULT_LEVEL;
    int flags = 0;

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

    if (rb_block_given_p()) {
        if (!NIL_P(rb_logger)) {
            rb_options = rb_logger;
        }
        rb_logger = rb_funcall(rb_cGrnCallbackLogger, id_new, 1, rb_callback);
    }

    rb_grn_scan_options(rb_options,
                        "max_level", &rb_max_level,
                        "time",      &rb_time,
                        "title",     &rb_title,
                        "message",   &rb_message,
                        "location",  &rb_location,
                        "flags",     &rb_flags,
                        NULL);
    if (!NIL_P(rb_max_level)) {
        max_level = RVAL2GRNLOGLEVEL(rb_max_level);
    }

    if (NIL_P(rb_time) || CBOOL2RVAL(rb_time)) {
        flags |= GRN_LOG_TIME;
    }
    if (NIL_P(rb_title) || CBOOL2RVAL(rb_title)) {
        flags |= GRN_LOG_TITLE;
    }
    if (NIL_P(rb_message) || CBOOL2RVAL(rb_message)) {
        flags |= GRN_LOG_MESSAGE;
    }
    if (NIL_P(rb_location) || CBOOL2RVAL(rb_location)) {
        flags |= GRN_LOG_LOCATION;
    }
    if (!NIL_P(rb_flags)) {
        flags = rb_funcall(rb_mGrnLoggerFlags, id_parse, 2,
                           INT2NUM(flags), rb_flags);
    }

    rb_grn_logger.max_level = max_level;
    rb_grn_logger.flags = flags;
    rb_grn_logger.user_data = (void *)rb_logger;

    context = rb_grn_context_ensure(&rb_context);
    grn_logger_set(context, &rb_grn_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 logger. It is useful for rotating log file.

Returns void

Returns:

  • void



429
430
431
432
433
434
435
436
437
438
439
440
# File 'ext/groonga/rb-grn-logger.c', line 429

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

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

    return Qnil;
}

.thresholdInteger

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

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

Returns The current rotate threshold size

Returns:

  • (Integer)

    The current rotate threshold size

Since:

  • 5.0.2



600
601
602
603
604
# File 'ext/groonga/rb-grn-logger.c', line 600

static VALUE
rb_grn_logger_s_get_rotate_threshold_size (VALUE klass)
{
    return OFFT2NUM(grn_default_logger_get_rotate_threshold_size());
}

.rotate_threshold_size=(size) ⇒ Object

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

If you specify 0 as size, log rotation by the default 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 logger

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

Disables log ration by the default logger

Groonga::Logger.rotate_threshold_size = 0

Returns void

Parameters:

  • size (Integer)

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

Returns:

  • void

Since:

  • 5.0.2



630
631
632
633
634
635
636
# File 'ext/groonga/rb-grn-logger.c', line 630

static VALUE
rb_grn_logger_s_set_rotate_threshold_size (VALUE klass, VALUE rb_size)
{
    grn_default_logger_set_rotate_threshold_size(NUM2OFFT(rb_size));

    return Qnil;
}

.unregisterObject

Unregister the registered logger. The default logger is used after unregistering.

Returns void

Returns:

  • void



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'ext/groonga/rb-grn-logger.c', line 388

static VALUE
rb_grn_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_logger_set(context, NULL);
    rb_grn_context_check(context, klass);

    return Qnil;
}

Instance Method Details

#finObject



81
82
# File 'lib/groonga/logger.rb', line 81

def fin
end

#log(level, timestamp, title, message, location) ⇒ Object



72
73
74
75
76
# File 'lib/groonga/logger.rb', line 72

def log(level, timestamp, title, message, location)
  guard do
    puts("#{timestamp}|#{mark(level)}|#{title} #{message} #{location}")
  end
end

#reopenObject



78
79
# File 'lib/groonga/logger.rb', line 78

def reopen
end