Ruby (im_ruby)

This module provides support for collecting log data with methods written in the Ruby language. See also the xm_ruby and om_ruby modules.

The Ruby modules are compatible with Ruby 2 versions only. More recent versions are unlikely to work.
To examine the supported platforms, see the list of installer packages in the Available Modules chapter.

The Nxlog module provides the following classes and methods.

Nxlog.log_debug(msg)

Send the message msg to the internal logger at DEBUG log level. This method does the same as the core log_debug() procedure.

Nxlog.log_info(msg)

Send the message msg to the internal logger at INFO log level. This method does the same as the core log_info() procedure.

Nxlog.log_warning(msg)

Send the message msg to the internal logger at WARNING log level. This method does the same as the core log_warning() procedure.

Nxlog.log_error(msg)

Send the message msg to the internal logger at ERROR log level. This method does the same as the core log_error() procedure.

class Nxlog.Module

This class will be instantiated by NXLog and passed to the method specified by the Call directive.

logdata_new()

This method returns a new LogData object.

set_read_timer(delay)

This method sets a trigger for another read after a specified delay in seconds (float).

class Nxlog.LogData

This class represents an event.

field_names()

This method returns an array with the names of all the fields currently in the event record.

get_field(name)

This method returns the value of the field name in the event.

post()

This method will submit the event to NXLog for processing by the next module in the route.

set_field(name, value)

This method sets the value of field name to value.

Configuration

The im_ruby module accepts the following directives in addition to the common module directives. The RubyCode directive is required.

RubyCode

This mandatory directive specifies a file containing Ruby code. The im_ruby instance will call the method specified by the Call directive. The method must accept an Nxlog.Module object as its only argument.


Call

This optional directive specifies the Ruby method to call. The default is read_data.

Examples

Example 1. Using im_ruby to generate events

In this example, events are generated by a simple Ruby method that increments a counter. Because this Ruby method does not set the $raw_event field, it would be reasonable to use to_json() or some other way to preserve the fields for further processing.

nxlog.conf
<Input in>
    Module      im_ruby
    RubyCode    ./modules/input/ruby/input2.rb
    Call        read_data
</Input>
input2.rb
$index = 0

def read_data(mod)
  Nxlog.log_debug('Creating new event via input.rb')
  $index += 1
  event = mod.logdata_new
  event.set_field('Counter', $index)
  event.set_field('Message', "This is message #{$index}")
  event.post
  mod.set_read_timer 0.3
end